简体   繁体   English

Xamarin.Forms Prism-使用导航服务从一个详细信息页面导航到另一个

[英]Xamarin.Forms Prism - Using navigation service to navigate from one detail page to another

In my Xamarin.Forms Prism app, I am using a MasterDetailPage for navigation. 在我的Xamarin.Forms Prism应用中,我正在使用MasterDetailPage进行导航。

While I am on one detail page, I would like to navigate to another detail page, as if I had selected it from the master page. 当我在一个详细信息页面上时,我想导航到另一个详细信息页面,就像我是从母版页中选择它一样。

Initial navigation in App.xaml.cs: App.xaml.cs中的初始导航:

protected override void OnInitialized()
{
   ...
   NavigationService.NavigateAsync("MainPage/RootNavigation/MyFirstPage");
}

When I click a shortcut button on MyFirstPage, I would like to go to MainPage/RootNavigation/MySecondPage. 当我单击MyFirstPage上的快捷方式按钮时,我想转到MainPage / RootNavigation / MySecondPage。 The closest that I have been able to achieve has been using an absolute Uri. 我能够达到的最接近的结果是使用绝对Uri。

private async void OnShortcutTapped(MyModel sender)
{
  ...
  await _navigationService.NavigateAsync(new Uri("http://myapp.com/MainPage/RootNavigation/MySecondPage", UriKind.Absolute), navigationParams, null, false);
}

This basically gets me what I want, but after navigating in this manner, if I make the Master visible and select the menu item for MySecondPage, it refreshes the detail page as if it is navigating to the page. 这基本上可以满足我的需要,但是以这种方式导航后,如果使“母版”可见并为MySecondPage选择菜单项,它将刷新详细信息页面,就像在导航至该页面一样。

Is there a better way to maintain this navigation, so that the master page knows that MySecondPage is already being displayed and it doesn't try to reload it? 有没有更好的方法来维护此导航,以便母版页知道MySecondPage已被显示并且不尝试重新加载它?

While your navigation pattern doesn't make a lot of sense to me, you can achieve what you want by invoking a navigate command in the MasterDetailPageViewModel. 虽然您的导航模式对我而言没有多大意义,但是您可以通过在MasterDetailPageViewModel中调用导航命令来实现所需的功能。 You have a number of ways to do this. 您可以通过多种方法来执行此操作。 You could use the IEventAggregator to send a message to the MasterDetailPageViewModel to navigate, or you can use a CompositeCommand that invokes a DelegateCommand that exists on the MasterDetailPageViewModel. 您可以使用IEventAggregator将消息发送到MasterDetailPageViewModel进行导航,也可以使用CompositeCommand调用存在于MasterDetailPageViewModel上的DelegateCommand。

You can see a sample of using a CompositeCommand here: https://github.com/PrismLibrary/Prism-Samples-Forms/tree/master/UsingCompositeCommands 您可以在此处查看使用CompositeCommand的示例: https : //github.com/PrismLibrary/Prism-Samples-Forms/tree/master/UsingCompositeCommands

You can also see how to send messages in this sample that I gave at the Xamarin Evolve conference: https://github.com/brianlagunas/Evolve2016SamplesAndSlides 您还可以在我在Xamarin Evolve会议上提供的此示例中查看如何发送消息: https : //github.com/brianlagunas/Evolve2016SamplesAndSlides

Another option would be to just call a navigate command off the App.Current.MainPage ViewModel from with your MyFirstPage code behind. 另一个选择是从后面的MyFirstPage代码中调用App.Current.MainPage ViewModel的导航命令。

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

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