PRISM Xamarin.Forms INavigationService 理解 [英] PRISM Xamarin.Forms INavigationService understanding

查看:75
本文介绍了PRISM Xamarin.Forms INavigationService 理解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力理解 PRISM INavigationService 如何映射到 Xamarin Forms 提供的标准 Navigation.PushAsync.

I am struggling to understand how the PRISM INavigationService maps to the standard Navigation.PushAsync provided by Xamarin Forms.

我有一个已开始使用 XF 的应用程序.它由作为主页的 MasterDetailPage 组成,Detail"ContentPage 包含许多用于导航到其他功能的按钮.在 App.xaml.cs 中,我将 Application.MainPage 设置为包含在 NavigationPageMainPage = new NavigationPage(new HomePage())"中的主页实例.当我想显示一个功能页面时,我调用Navigation.PushAsync(new NewPage())",这会显示新页面以及一个包含后退按钮的工具栏;然后我可以使用设备上的后退按钮或使用工具栏中的后退按钮导航回我的主页.请注意,该应用程序在 Android 上运行.

I have an application that I have started using XF. It consists of a MasterDetailPage that is the home page and the "Detail" ContentPage contains a number of buttons that are used to then navigate to other functionality. Within the App.xaml.cs, I set the Application.MainPage to an instance of my home page wrapped within a NavigationPage "MainPage = new NavigationPage(new HomePage())". When I then want to display a functionality page, I call "Navigation.PushAsync(new NewPage())" and this shows the new page along with a toolbar containing a back button; I can then navigate back to my home page using either the back button on the device or using the back button in the toolbar. Note, the application is running on Android.

然后我研究了集成 PRISM.Forms;我想使用它,因为我多年来一直将 PRISM 用于 WPF,并认为它应该证明是有益的.我使用的是最新版本 (6.3).

I then looked at integrating PRISM.Forms; I wanted to use this as I have used PRISM for WPF for many years and felt that it should prove beneficial. I am using the latest version (6.3).

我基于PRISM 模板"创建了一个新应用程序,然后将差异复制到我现有的项目中...

I created a new application based upon the "PRISM Templates", and then replicated the differences into my existing project...

  • App"类现在基于PrismApplication"而不是PrismApplication.RegisterTypes"中的应用程序"
  • 我为我想要的所有表单调用Container.RegisterForNavigation"能够导航到PrismApplication.OnInitialized"的末尾
  • 我调用"INavigationService.NavigateAsync($"/{nameof(NavigationPage)}/{nameof(HomeMasterDetailPage)}");"
  • 我之前称Navigation.PushAsync"的地方,我称INavigationService.NavigateAsync(nameof(NewPage))"内MainActivity"我调用App"构造函数"IPlatformInitializer"

到目前为止,一切都很好.我按预期看到了我的主页.但是,当我导航到NewPage"时,工具栏中没有后退按钮(尽管我仍然有 MasterDetailPage 中的汉堡包"菜单),如果我使用设备上的后退按钮让我回到操作系统.该页面似乎已作为 MasterDetailPage 的内容"部分插入,而不是作为新的子页面导航到它.

So far, so good. I see my home page as expected. However when I then navigate to "NewPage", I have no back button in the toolbar (though I still have my "hamburger" menu from the MasterDetailPage) and if I use the back button on the device it returns me to the operating system. It seems that the page has been inserted as the "Content" section of the MasterDetailPage instead of navigating to it as a new child page.

我确信这不是设计使然,因此我误解了一些东西,但有人可以指出我正确的方向,否则我觉得我将不得不放弃 PRISM 并寻找替代方案.

I am sure that this is not by design and therefore something that I am misunderstanding but can somebody point me back in the right direction as otherwise I feel I will have to discard PRISM and look at an alternative.

在尝试将登录屏幕引入混合时,我也遇到了问题.我首先调用 "INavigationService.NavigateAsync($"/{nameof(NavigationPage)}/{nameof(LoginPage)}");" 然后如果登录成功我调用 "INavigationService.NavigateAsync($"/{nameof(NavigationPage)}/{nameof(HomeMasterDetailPage)}");" 进入首页;这里的问题是,当显示主页(MasterDetailPage)时,我不再看到用于打开页面Master"部分的hamburger"按钮.我怀疑这可能也与我错误地使用 INavigationService 有关,但如果不是,我可以将其作为单独的问题提出.

I also have a problem when trying to introduce a login screen into the mix. I start by calling "INavigationService.NavigateAsync($"/{nameof(NavigationPage)}/{nameof(LoginPage)}");" and then if login succeeds I call "INavigationService.NavigateAsync($"/{nameof(NavigationPage)}/{nameof(HomeMasterDetailPage)}");" to get to the home page; problem here is that when displaying the home page (a MasterDetailPage) I no longer see the "hamburger" button to open the "Master" portion of the page. I suspect that this is probably also related to my using the INavigationService incorrectly but if not I can raise it as a separate issue.

推荐答案

Prism 的 INavigationService 基本上以这种方式映射到 Xamarin Forms INavigation:INavigationService.NavigateAsync => INavigation.PushAsync(或 PushModalAsync 如果您将 useModal 标志设置为 true),&INavigationService.GoBackAsync => INavigation.PopAsync.

Prism's INavigationService essentially maps to the Xamarin Forms INavigation in this way: INavigationService.NavigateAsync => INavigation.PushAsync (or PushModalAsync if you set the useModal flag to true), & INavigationService.GoBackAsync => INavigation.PopAsync.

INavigationService 使用容器解析您在传递给它的 Uri 中指定的任何页面,然后将它们推送到导航堆栈上.您可能想查看各种可用的示例,包括HamburgerMenu"项目显示了 MasterDetailPage 的使用,它使用了 LoginPage.

The INavigationService uses the container to resolve any pages that you specify in the Uri that you pass it and then pushes them onto the Navigation Stack. You may want to check out the various samples available, including the "HamburgerMenu" project which shows the use of a MasterDetailPage and it uses a LoginPage.

这篇关于PRISM Xamarin.Forms INavigationService 理解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆