我正在尝试使用MasterDetail进行测试和示例。您可以在以下位置查看代码:
https://github.com/jrariasf/MD8/tree/master/MD8
Master具有5个按钮,可访问4个详细信息页面(主页,主页,ViewA,ViewB和ViewC)。
从ViewA,使用2个按钮,我可以加载ViewB和ViewC
但是我不能按hambubrger菜单中的按钮然后加载足够的详细信息页面。
仅当我在“ PrismMasterDetailPage.xaml”的CommandParameter中放置绝对路径时,此方法才起作用:
<?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:prism="http://prismlibrary.com"
prism:ViewModelLocator.AutowireViewModel="True"
x:Class="MD8.Views.PrismMasterDetailPage">
<MasterDetailPage.Master>
<ContentPage Title="Menu">
<StackLayout Padding="20">
<!-- TODO: // Update the Layout and add some real menu items -->
<Button Text="Home" Command="{Binding NavigateCommand}" CommandParameter="/PrismMasterDetailPage/NavigationPage/ViewA" />
<Button Text="MainPage" Command="{Binding NavigateCommand}" CommandParameter="/NavigationPage/MainPage" />
<Button Text="ViewA" Command="{Binding NavigateCommand}" CommandParameter="../ViewA" />
<Button Text="ViewB" Command="{Binding NavigateCommand}" CommandParameter="./ViewB" />
<Button Text="ViewC" Command="{Binding NavigateCommand}" CommandParameter="ViewC" />
</StackLayout>
</ContentPage>
</MasterDetailPage.Master>
</MasterDetailPage>
然后,在“ PrismMasterDetailPageViewModel.cs”中
void ExecuteCommandName(string page)
{
Console.WriteLine("PrismMasterDetailPageViewModel - ExecuteCommandName() Vamos a {0}", page);
_navigationService.NavigateAsync(page);
}
如果我在“ / PrismMasterDetailPage / NavigationPage / ViewA”中,则我要卸载ViewA和加载ViewB要做的事情?
例如,在App.xaml.cs中,代码为:
await NavigationService.NavigateAsync("PrismMasterDetailPage/NavigationPage/ViewA");
然后,在android模拟器上执行该应用程序,汉堡菜单的按钮执行,结果结果与我预期的不同。按下主页按钮,_navigationService.GetNavigationUriPath()返回:/ PrismMasterDetailPage / NavigationPage / ViewA / NavigationPage?useModalNavigation = true / ViewA
为什么?
如果我按ViewA或ViewB或ViewC按钮,则不会显示任何内容。但是在每个View * ViewModel.cs
上都会调用OnNavigatedFrom()方法。怎么了?
谢谢!
var mdp = new MyMasterDetailPage
{
Detail = new NavigationPage(new ViewA)
};
为了使用Prism实现汉堡菜单,通常需要将MasterDetailPage作为应用程序的主页。导航Uri中的下一个段必须是NavigationPage,下一个页面通常是ContentPage。
<Button Text="Home" Command="{Binding NavigateCommand}" CommandParameter="/PrismMasterDetailPage/NavigationPage/ViewA" />
好吧,看这第一个,通常是从PrismApplication导航时要使用的,这就是它起作用的原因。
<Button Text="MainPage" Command="{Binding NavigateCommand}" CommandParameter="/NavigationPage/MainPage" />
看这一点,它确实接近您想要的内容,只不过您执行的是绝对导航,这意味着您要重置Application.MainPage
。您实际需要的是相对的Uri,因为您要从MasterDetailPage导航。
<Button Text="ViewA" Command="{Binding NavigateCommand}" CommandParameter="../ViewA" />
这是完全错误的,因为仅在NavigationPage中支持../{path}
,而您在MasterDetailPage中...
<Button Text="ViewB" Command="{Binding NavigateCommand}" CommandParameter="./ViewB" />
这根本不被棱镜支持。
<Button Text="ViewC" Command="{Binding NavigateCommand}" CommandParameter="ViewC" />
这是设置MasterDetailPage.Detail,如:
mdp.Detail = new ViewC()
当然,正如我上面提到的,您需要做到这一点
mdp.Detail = new NavigationPage(new ViewC());
一个月前,我刚刚看到您的评论,对不起。
[我是C#和Xamarin和Prism的新手,我仍然是新手,但更少:-)
过去几周我学到了很多东西,并进行了测试,我对概念有了更好的了解。
阅读后,我知道../{path}仅对NavigationPage有效。
[还有,我遇到的一个问题是,我正在检查OnNavigatedTo方法中的“ _navigationService.GetNavigationUriPath()”的值,正如我稍后阅读的那样,那时UriPath并不是真正的最终UriPath。
在我的代码中,我写道:
public void OnNavigatedTo(INavigationParameters parameters)
{
Console.WriteLine("DEBUG - ViewAVM: We are in {0}",
_navigationService.GetNavigationUriPath());
}
我将GetNavigationUriPath()调用移至另一个方法,结果与预期的一样。谢谢!