繁体   English   中英

为什么 Prism 的 Master Detail Page 结构与 Xamarin.Forms 不同?

[英]Why Prism's Master Detail Page structure is different from Xamarin.Forms?

大约几个月前,我用 Prism 开发了一款应用。 这是一个很棒的工具,可以为我的应用程序构建完美的结构。 然而,今天我试图拿起那个应用程序并修复一些悬而未决的事情。 我在我的应用程序中使用了 Master Detail 页面,今天我突然发现实际上我如何使用 Prism 开发这个 Master Detail 页面的结构有点超出我的知识范围。

所以,在这里。

基本上我的 MasterDetailPage 是这样的:

<?xml version="1.0" encoding="utf-8" ?>
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:b="clr-namespace:Prism.Behaviors;assembly=Prism.Forms"
             mc:Ignorable="d"
             x:Class="JapaneseLearnPrism.Views.MenuPage">
    <MasterDetailPage.Master>
        <NavigationPage Title="Menu" Icon="ic_hamburger.png">
            <x:Arguments>
                <ContentPage Title="{Binding Title}">
                    <ListView ItemsSource="{Binding MenuItems}"
                              SelectedItem="{Binding SelectedMenuItem, Mode=TwoWay}"
                              SeparatorColor="LightGray"
                              RowHeight="60"
                              SeparatorVisibility="Default"
                              BackgroundColor="#e8e8e8">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <ViewCell>
                                    <StackLayout VerticalOptions="FillAndExpand"
                                                 Orientation="Horizontal"
                                                 Padding="20,10,0,10"
                                                 Spacing="20">
                                        <Label Text="{Binding PageIconText}" FontFamily="{StaticResource FontAwesomeSolid}" FontSize="Medium" VerticalOptions="Center" />
                                        <Label Text="{Binding Title}" FontSize="Medium" VerticalOptions="Center" TextColor="Black" />
                                    </StackLayout>
                                </ViewCell>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                        <ListView.Behaviors>
                            <b:EventToCommandBehavior EventName="ItemTapped" Command="{Binding NavigateCommand}" />
                        </ListView.Behaviors>
                    </ListView>
                </ContentPage>
            </x:Arguments>
        </NavigationPage>
    </MasterDetailPage.Master>
</MasterDetailPage>

导航路径就是 App.xaml.cs 文件中的这个。

await NavigationService.NavigateAsync(nameof(MenuPage) + "/" + nameof(NavigationPage) + "/" + nameof(Views.MainPage));

但是如果我查看很多 Xamarin.Forms 示例,MasterDetailPage 实际上是不同的。 他们将执行以下操作:

<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
                  xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                  xmlns:local="clr-namespace:MasterDetailPageNavigation;assembly=MasterDetailPageNavigation"
                  x:Class="MasterDetailPageNavigation.MainPage">
    <MasterDetailPage.Master>
        <local:MasterPage x:Name="masterPage" />
    </MasterDetailPage.Master>
    <MasterDetailPage.Detail>
        <NavigationPage>
            <x:Arguments>
                <local:ContactsPage />
            </x:Arguments>
        </NavigationPage>
    </MasterDetailPage.Detail>
</MasterDetailPage>

这是来自微软官方网站。 上面的母版页实际上只是一个内容页,但是,详细信息页应该是导航页,我可以理解,因为它很有意义。

但是,在我的 Prism 应用程序中,MasterPage 也必须是 NavigaionPage,这是为什么呢? 任何人都可以帮助解决这个问题?

谢谢。

你可以看看这个关于如何在 Xamarin Forms with Prism 中制作主从页面导航菜单的精彩博客。 如果您向下滚动到帖子底部,您将看到一些关于为什么它会以这种方式工作的说明,引用:

注意:Prism 中的导航都是基于这种 url 格式,我们可以在其中定义我们想要去的页面,它可以是这样的:ViewA/ViewB/ViewC/ViewD


...如果您还记得我们在 MasterDetail 页面内,所以从 MasterDetail 导航将得到结果,这些页面将转到其中的 Detail 部分...这对我们来说很棒,我们在母版页侧面有菜单并选择菜单项将打开页面作为详细信息页面。

Prism Navigation 的设计方式是,DetaiPage 位于 MasterDetail 内部是有意义的,因此基本上它保留了进行导航的 Prism 逻辑。


引用棱镜导航的基础知识

在 Prism 应用程序中导航在概念上不同于 Xamarin.Forms 中的标准导航。 虽然 Xamarin.Forms 导航依赖于 Page 类实例进行导航,但 Prism 删除了对 Page 类型的所有依赖关系,以从 ViewModel 中实现松散耦合的导航。 在 Prism 中,不存在导航到 View 或导航到 ViewModel 的概念。 相反,您只需导航到一个体验或唯一标识符,它代表您希望在应用程序中导航到的目标视图。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM