为什么 Prism 的主从页面结构与 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>
这是来自微软的官方网站。上面的Master Page其实就是一个ContentPage,Detail page应该是NavigationPage,这个我能看懂,因为有道理。
但是,在我的 Prism 应用程序中,MasterPage 也必须是 NavigaionPage,这是为什么呢?有人可以帮忙吗?
谢谢。
你可以看看这篇关于 How to make Master-Detail Page Navigation menu in Xamarin Forms with Prism 的精彩博客。
如果你向下滚动到 post 的底部,你会看到一些关于它为什么这样工作的描述,引用:
Note: Navigation in Prism is all based on this url format where we can define to which pages we want to go, and it can be something like this: ViewA/ViewB/ViewC/ViewD
... and if you remember we are inside of MasterDetail page so navigating from MasterDetail will get result that those pages will go to Detail part of it... and that is great for us, we have menu in side of master page and choosing menu items will open pages as Detail pages.
Prism Navigation 的设计方式,将 DetaiPage 放入 MasterDetail 中是有意义的,因此基本上它保留了进行导航的 Prism 逻辑。
的基础知识
Navigating in a Prism application is conceptually different than standard navigation in Xamarin.Forms. While Xamarin.Forms navigation relies on a Page class instance to navigate, Prism removes all dependencies on Page types to achieve loosely coupled navigation from within a ViewModel. In Prism, the concept of navigating to a View or navigating to a ViewModel does not exist. Instead, you simply navigate to an experience, or a unique identifier, which represents the target view you wish to navigate to in your application.
我大约几个月前用 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>
这是来自微软的官方网站。上面的Master Page其实就是一个ContentPage,Detail page应该是NavigationPage,这个我能看懂,因为有道理。
但是,在我的 Prism 应用程序中,MasterPage 也必须是 NavigaionPage,这是为什么呢?有人可以帮忙吗?
谢谢。
你可以看看这篇关于 How to make Master-Detail Page Navigation menu in Xamarin Forms with Prism 的精彩博客。 如果你向下滚动到 post 的底部,你会看到一些关于它为什么这样工作的描述,引用:
Note: Navigation in Prism is all based on this url format where we can define to which pages we want to go, and it can be something like this: ViewA/ViewB/ViewC/ViewD
... and if you remember we are inside of MasterDetail page so navigating from MasterDetail will get result that those pages will go to Detail part of it... and that is great for us, we have menu in side of master page and choosing menu items will open pages as Detail pages.
Prism Navigation 的设计方式,将 DetaiPage 放入 MasterDetail 中是有意义的,因此基本上它保留了进行导航的 Prism 逻辑。
的基础知识
Navigating in a Prism application is conceptually different than standard navigation in Xamarin.Forms. While Xamarin.Forms navigation relies on a Page class instance to navigate, Prism removes all dependencies on Page types to achieve loosely coupled navigation from within a ViewModel. In Prism, the concept of navigating to a View or navigating to a ViewModel does not exist. Instead, you simply navigate to an experience, or a unique identifier, which represents the target view you wish to navigate to in your application.