天天看点

NET MAUI 预览版最新发布

作者:离开了编程我会死

最新的.NET MAUI 预览版已发布,看看又带来了哪些新功能:

  • 应用程序图标、应用程序生命周期、画笔、控件和单个项目的新文档
  • 在 Android 上实现的 FlyoutView 处理程序
  • RelativeLayout为和AbsoluteLayout添加了兼容性处理程序
  • 添加了 Z 索引属性
  • .NET 6 统一iOS 类型
  • Windows 扩展工具栏 - 非 Shell
NET MAUI 预览版最新发布

.NET MAUI 中的导航:专注于 Shell

Shell 是一种应用程序支架,可简化常见的弹出菜单和选项卡设计。在Shell中,添加应用程序页面并将它们排列在导航结构中。例如,

<Shell xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
       xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
       xmlns:pages="clr-namespace:Microsoft.NetConf2021.Maui.Pages"
       xmlns:root="clr-namespace:Microsoft.NetConf2021.Maui"
       xmlns:viewmodels="clr-namespace:Microsoft.NetConf2021.Maui.ViewModels"
       x:DataType="viewmodels:ShellViewModel"
       x:Class="Microsoft.NetConf2021.Maui.Pages.MobileShell">
    <TabBar>
        <Tab Title="{Binding Discover.Title}"
             Icon="{Binding Discover.Icon}">
            <ShellContent ContentTemplate="{DataTemplate pages:DiscoverPage}" />
        </Tab>
        <Tab Title="{Binding Subscriptions.Title}"
             Icon="{Binding Subscriptions.Icon}">
            <ShellContent ContentTemplate="{DataTemplate pages:SubscriptionsPage}" />
        </Tab>
        <Tab Title="{Binding ListenLater.Title}"
             Icon="{Binding ListenLater.Icon}">
            <ShellContent ContentTemplate="{DataTemplate pages:ListenLaterPage}" />
        </Tab>
        <Tab Title="{Binding ListenTogether.Title}"
             Icon="{Binding ListenTogether.Icon}"
             IsVisible="{x:Static root:Config.ListenTogetherIsVisible}">
            <ShellContent 
                ContentTemplate="{DataTemplate pages:ListenTogetherPage}" />
        </Tab>
        <Tab Title="{Binding Settings.Title}"
             Icon="{Binding Settings.Icon}">
            <ShellContent ContentTemplate="{DataTemplate pages:SettingsPage}" />
        </Tab>
    </TabBar>
</Shell>           
NET MAUI 预览版最新发布

Shell 上下文中的导航是使用基于 URI 的路由完成的,例如:

Routing.RegisterRoute(nameof(DiscoverPage), typeof(DiscoverPage));
Routing.RegisterRoute(nameof(ShowDetailPage), typeof(ShowDetailPage));
Routing.RegisterRoute(nameof(EpisodeDetailPage), typeof(EpisodeDetailPage));
Routing.RegisterRoute(nameof(CategoriesPage), typeof(CategoriesPage));
Routing.RegisterRoute(nameof(CategoryPage), typeof(CategoryPage));           

Shell和依赖注入

在 DI 容器中定义的依赖项,通常在 MauiProgram.cs 中:

public static class MauiProgram
    {
        public static MauiApp CreateMauiApp()
        {
            var builder = MauiApp.CreateBuilder();
            builder
                .UseMauiApp<App>();
            builder.Services
                .AddSingleton<MainViewModel>();
            return builder.Build();
        }
    }           

然后在要注入的页面中:

public partial class MainPage
{
    readonly MainViewModel _viewModel;
    public MainPage(MainViewModel viewModel)
    {
        InitializeComponent();
        BindingContext = _viewModel = viewModel;
    }
}           

Shell 提供了许多模板功能,可以快速实现最常见的需求。

继续阅读