We delivered the latest developments in the mobile and desktop development of the .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 introduced some new semantic properties for accessibility. When we discuss these in detail, we invite you to use dotnet new to create new applications with us and share your feedback.
Support Windows desktop
Project Reunion 0.5 has been released! Now, Windows has joined Android, iOS and macOS, becoming the target platform you can reach using .NET MAUI! You can start from the Project Reunion installation instructions . In this version, we created a sample project , you can start to explore from Visual Studio 2019 16.10 Preview.
Once we have the necessary .NET 6 infrastructure for Project Reunion, we will add Windows to our single project template.
getting Started
Since we are still in the early stages of the preview, the process of installing all the dependencies required for mobile and desktop development still relies on manual operations. To help you and ourselves, Jonathan Dick (Jonathan Dick) put together a useful tool dotnet tool, it can evaluate your system and collect as many components as possible. To use this tool, you need to install maui-check globally from the command line
dotnet tool install -g Redth.Net.Maui.Check
Source: https://github.com/Redth/dotnet-maui-check
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 uses Microsoft.Extensions HostBuilder start each application. This provides a consistent pattern for application developers and library maintainers for rapid application development. Each platform has a different starting point, but your application entry is consistently located in 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 Xamarin.Forms renderer or custom renderer compatibility. This is also where you introduce your App, that is, implement 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();
}
}
In order to present your content, a view will be added to MainWindow:
public class MainWindow : IWindow
{
public MainWindow()
{
Page = new MainPage();
}
public IPage Page { get; set; }
public IMauiContext MauiContext { get; set; }
}
That's it! You now have a window with content.
Native life cycle events
Preview 3 further improves the startup extension, and introduces ConfigureLifecycleEvents to easily connect to native platform lifecycle events. This is an important introduction, especially for a single project experience, which can simplify the initialization and configuration required by many libraries.
The following is a simple example that associates the program to the Android back button event and handles 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 us look at how other libraries can use these methods to simplify their platform initialization. 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 content required for 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 how to link to the platform lifecycle events, which greatly simplifies the native configuration across platforms.
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 dotnet/maui We look forward to seeing more libraries make use of this model to simplify their use.
Update of controls and layout
In addition to the existing compatible Renderers introduced from Xamarin.Forms, we will continue to add more controls, attributes and layouts to .NET MAUI. If you use the above code as the startup program, then you will only be able to use the currently implemented handlers. To see what has been implemented currently, you can check the Handlers folder at dotnet/maui .
In order to track our next work, we have provided a Project Board for all handlers that we have accepted Pull request, you can check it out.
The layout has also been updated in Preview 3. Grid now supports absolute size and automatic size (same as content size). The LayoutAlignment option is now also available for Grid and StackLayout, so you can start using the HorizontalLayoutAlignment and VerticalLayoutAlignment properties to position views.
Semantic attributes of accessibility
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 for this 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 this DOTNET / maui Issue original illustrated and discussed.
Share your feedback
We are very excited about this release and look forward to your feedback. Please join our dotnet/maui and let us know your thoughts on these improvements.
If you have any questions about .NET, please feel free to ask questions on the Microsoft Q&A forum:
https://docs.microsoft.com/en-us/answers/products/dotnet
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。