简体   繁体   English

如何在 xamarin 表单中进行动态页面导航?

[英]How to do Dynamic page navigation in xamarin forms?

I am working on xamarin.forms.我正在研究 xamarin.forms。 Where I need to do navigation based on menu clicked.我需要根据点击的菜单进行导航的地方。 It's a dynamic menu, order of menus might change or based on the user logged in the menus will be changing.这是一个动态菜单,菜单的顺序可能会改变,或者根据登录的用户,菜单会发生变化。 What I need to do is I will get from API to which page I should navigate, I need to pass that page name dynamically.我需要做的是从 API 获取我应该导航到的页面,我需要动态传递该页面名称。 Is it possible or not?可不可以? If yes please help me out.如果是,请帮助我。

var dynamicpage = "MenuPage()";
new NavigationPage (new dynamicpage );// Like this

It is much easier with Shell , you can define your pages based on your hierarchical design inside <Shell> thanks to Route .使用Shell更容易,你可以根据<Shell>中的层次结构设计来定义你的页面,这要归功于Route Also you may benefit from efficient page loading by using DataTemplate which will load the page only when required (navigated to).您还可以通过使用DataTemplate高效的页面加载中受益,它只会在需要时(导航到)加载页面。

Regarding navigation you can explicitly navigate from code behind to any page by specifying the route of that page that you have already registered when you defined your <Shell> or in the code behind.关于导航,您可以通过指定您在定义<Shell>时或在代码隐藏中已经注册的页面的路由,从代码隐藏显式导航到任何页面。

If you encounter the exception如果遇到异常

System.ArgumentException: 'unable to figure out route for: //RegisterPage Parameter name: uri' System.ArgumentException:“无法找出路线://RegisterPage 参数名称:uri”

Then you probably didn't register the route before it got invoked or you made a typo on the route.那么你可能在调用之前没有注册路由,或者你在路由上输入了错误。 You can refer to Bug?可以参考Bug吗? System.ArgumentException: 'unable to figure out route for: System.ArgumentException: '无法找出路线:

For more details you can always check the linked Microsoft official documentation.有关更多详细信息,您可以随时查看链接的 Microsoft 官方文档。

Have you tried using Activator.CreateInstance() ?您是否尝试过使用Activator.CreateInstance()

var pageType = typeof(MenuPage);
var navPage = new NavigationPage((Page)Activator.CreateInstance(pageType));

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

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