简体   繁体   中英

How to do Dynamic page navigation in xamarin forms?

I am working on 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. 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 . Also you may benefit from efficient page loading by using DataTemplate which will load the page only when required (navigated to).

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.

If you encounter the exception

System.ArgumentException: 'unable to figure out route for: //RegisterPage Parameter name: 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? System.ArgumentException: 'unable to figure out route for:

For more details you can always check the linked Microsoft official documentation.

Have you tried using Activator.CreateInstance() ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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