Guanxuan Maui’s latest progress in. Net preview 3

Time:2021-10-18

We delivered the latest progress in mobile and desktop development of. Net multi platform application UI in. Net   6   preview   3. This version adds the windows platform and winui   3, improves the basic application and startup builder, adds native lifecycle events, and adds more UI controls and layouts. We also introduce some new semantic attributes for accessibility. When we discuss these in detail, we invite you to create new applications with us using dotnet   new and share your feedback.

Support Windows Desktop

Project reunion 0.5 has been published! Now, windows has joined Android, IOS and MacOS to become the target platform you can reach using. Net Maui! You can start with the project reunion installation instructions. In this release, we have created a sample project that you can explore from visual studio 2019 16.10 preview.

Guanxuan Maui's latest progress in. Net preview 3

Once we have the necessary. Net 6 infrastructure for project reunion, we will add windows to our single project template.

introduction

Since we are still in the early stages of preview, the process of installing all dependencies required for mobile and desktop development still depends on manual operation. To help you and ourselves, Jonathan Dick has compiled a useful tool, dotnet tool, which can evaluate your system and collect as many components as possible. Using this tool requires a global installation of Maui check from the command line
dotnet tool install -g Redth.Net.Maui.Check

Source: https://github.com/Redth/dotn…

Guanxuan Maui's latest progress in. Net preview 3

Now run >   Maui check and follow the instructions. After success, you can create your first application:
dotnet new maui -n HelloMaui

For step-by-step instructions on installation and getting started, you can also refer to:
https://github.com/dotnet/maui/wiki/Getting-Started。

Your first application

. net Maui launches each application using microsoft.extensions hostbuilder. This provides a consistent pattern for application developers and library maintainers to develop applications quickly. Each platform has a different starting point, but your application portal is consistently located at startup.cs. Here is a simple example:

public class Startup : IStartup 
{ 
    public void Configure(IAppHostBuilder appBuilder) 
    { 
        appBuilder 
            .UseMauiApp(); 
    } 
}

Here, you can perform operations such as registering fonts and registering the xamarin. Forms renderer or customizing renderer compatibility. This is also where you introduce your app, that is, implement the application and (at least) be responsible for creating a new window:

public partial class App : Application 
{ 
    public override IWindow CreateWindow(IActivationState activationState) 
    { 
        return new MainWindow(); 
    } 
} 

To render your content, a view is added to MainWindow:

public class MainWindow : IWindow 
{ 
    public MainWindow() 
    { 
        Page = new MainPage(); 
    } 

    public IPage Page { get; set; } 
 
    public IMauiContext MauiContext { get; set; } 
} 

this is it! You now have a window with content.

Native lifecycle events

Preview 3 further improves the startup extension program and introduces configurelife cycle events to facilitate easy connection to native platform life cycle events. This is an important introduction, especially for the single project experience, which can simplify the initialization and configuration of many libraries.

The following is a simple example of associating a program with an Android back button event and processing it as needed:

public class Startup : IStartup 
{ 
    public void Configure(IAppHostBuilder appBuilder) 
    { 
        appBuilder 
            .UseMauiApp() 
            .ConfigureLifecycleEvents(lifecycle => { 
                #if ANDROID 
                lifecycle.AddAndroid(d => { 
                    d.OnBackPressed(activity => { 
                        System.Diagnostics.Debug.WriteLine("Back button pressed!"); 
                    }); 
                }); 
                #endif 
            }); 
    } 
} 

Now let’s look at how other libraries can use these methods to simplify their platform initialization. The essentials (Microsoft. Maui. Essentials) library is a part of. Net Maui. It provides cross platform non UI services. We can use this library to configure all the contents required by all platforms in a unified location:

public class Startup : IStartup 
{ 
    public void Configure(IAppHostBuilder appBuilder) 
    { 
        appBuilder 
            .UseMauiApp() 
            .ConfigureEssentials(essentials => 
            { 
                essentials 
                    .UseVersionTracking() 
                    .UseMapServiceToken("YOUR-KEY-HERE"); 
            }); 
    } 
} 

In the essentials code, you can see how the configureessentials extension method is created and linked to platform lifecycle events, greatly simplifying the cross platform native configuration.

public static IAppHostBuilder ConfigureEssentials(this IAppHostBuilder builder, Action configureDelegate = null) 
{ 
    builder.ConfigureLifecycleEvents(life => 
    { 
#if __ANDROID__ 
        Platform.Init(MauiApplication.Current); 
 
        life.AddAndroid(android => android 
            .OnRequestPermissionsResult((activity, requestCode, permissions, grantResults) => 
            { 
                Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults); 
            }) 
            .OnNewIntent((activity, intent) => 
            { 
                Platform.OnNewIntent(intent); 
            }) 
            .OnResume((activity) => 
            { 
                Platform.OnResume(); 
            })); 
#elif __IOS__ 
        life.AddiOS(ios => ios 
            .ContinueUserActivity((application, userActivity, completionHandler) => 
            { 
                return Platform.ContinueUserActivity(application, userActivity, completionHandler); 
            }) 
            .OpenUrl((application, url, options) => 
            { 
                return Platform.OpenUrl(application, url, options); 
            }) 
            .PerformActionForShortcutItem((application, shortcutItem, completionHandler) => 
            { 
                Platform.PerformActionForShortcutItem(application, shortcutItem, completionHandler); 
            })); 
#elif WINDOWS 
        life.AddWindows(windows => windows 
            .OnLaunched((application, args) => 
            { 
                Platform.OnLaunched(args); 
            })); 
#endif 
    }); 
 
    if (configureDelegate != null) 
        builder.ConfigureServices(configureDelegate); 
 
    return builder; 
} 

You can view the complete class in dotnet / Maui. We look forward to seeing more libraries use this pattern to simplify their use.

Update of controls and layouts

In addition to the existing compatible renderers introduced from xamarin.forms, we will continue to add more controls, properties and layouts in. Net Maui. If you use the above code as the launcher, you will only be able to use the currently implemented handlers. To view the currently implemented content, you can view the handlers folder at dotnet / Maui.

In order to track our next work, we have provided a project board for all handlers who have accepted the pull request, which you can refer to.

The layout has also been updated in preview 3. Grid now supports absolute size and automatic size (the same as content size). The layoutalignment option is now also available for grid and stacklayout, so you can start positioning views using the horizontallayoutalignment and verticallayoutalignment properties.

Semantic attributes of auxiliary functions

We have been working with many customers to better understand the common difficulties encountered in achieving accessibility across multiple native platforms and how to make it easier in. Net Maui. One of the measures taken to this end is to add new semantic attributes to map cross platform attributes to native accessibility attributes.

<Label 
    Text="Welcome to .NET MAUI!"
    SemanticProperties.HeadingLevel="Level1"
    FontSize="32"
    HorizontalOptions="CenterAndExpand" />

<Label 
    Style="{DynamicResource Glyph}" 
    Text="" 
    SemanticProperties.Description="Heart" />

<Label 
    Text="Click the button. You know you want to!" 
    FontSize="18"
    x:Name="CounterLabel"
    HorizontalOptions="CenterAndExpand" />

<Button 
    Text="Click Me!" 
    Clicked="OnButtonClicked"
    SemanticProperties.Hint="Counts the number of times you click"/>

For more information, see the original description and discussion of this dotnet / Maui issue.

Share your feedback

We are very excited about this version and look forward to your feedback. Please join us at dotnet / Maui and let us know what you think of these improvements.

If you have any questions about. Net, please also welcome to ask questions in the Microsoft Q & a forum:
https://docs.microsoft.com/en-us/answers/products/dotnet

Recommended Today

OC basis

IOS development interview essential skills chart.png What are objects and what are the objects in OC? An object is an instance of a class; Is an instance created through a class, which is generally called an instance object; Common objects in OC include instance objects, class objects, and metaclass objects; What is a class? What […]