How to do Dynamic page navigation in xamarin forms? - c#

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));

Related

Navigation between pages in Xamarin Forms

I have the following command inside a VM:
ICommand LaunchGameCommand => new Command(() =>
{
//Navigation.PushAsync(...
});
According to the answers here I should be able to use something akin to the navigation in the commented code; however, the Navigation object seems to reside in Android.Content.Res... which seems to be something else entirely.
Is this still the correct method of navigating between views / viewmodels in Xamarin Forms, or has this now been superseded with an alternate method?
Navigation is part of a page, you can’t find navigation property if you don’t have the reference to a some page, you need to have access to your current page in your view model to see this property, you can have access to your current page using
Application.Current.MainPage.Navigation.Push...
Are you using any particular MVVM framework? Most of these include a way of navigating from VM to VM.
I use FreshMvvm. It allows you to perform the following to navigate between VMs and also pass data:
CoreMethods.PushPageModel<MyNextPageModel>(DataToPass);
More details here

how to get the information of Phone Applicationpage while deactivating the application

When i am deactivating my application,is there any way to find out the Last Phone Application Page ,such as name of the page ,its information on the deactivate method of App.xaml.cs while deactivating.
First:
Each page can override OnNavigatedFrom method. And you can store important info into ApplicationSettings.
On Application.Deactivated event handler you can read this info from settings.
Also, have a look at this page. You can find there few usefull ideas.
Second:
You can use this trick to store the Uri of last page. So, you'll get page name and passed parameters.
I am afraid the Application_Deactivated function can not tell you that infomation with its argument .
But I think you can keep a record of the lastest navigated to page's info in a global environment .

Error using 'this' in a cs page trying to access control

I have a page called test.aspx with test.cs.
However, i want to access my control called mbResult
Which is my custom messagebox control, from a sepearate CS page.
I know many people have asked this question and i have found that this is a method to access my controls.
MessageBoxControl mbox1 = this.FindControl("mbResult") as MessageBoxControl;
But I keep getting this error
Error 5 Keyword 'this' is not valid in a static property, static method, or static field initializer
Any ideas on how to access this control all i am trying to do is make it visible.
Thanks
You need to move the code into a non-shared method. You need to be operating in an instance of the page.
Update for clarification in comments
Unfortunately, your application is going to need some restructuring.
If the messageboxcontrol is shown in a new window, then you will need to pass the value from your source page to the new window in the query string.
However, if you want the messagebox control to be displayed on the source page, then you will need to convert it from a page to a UserControl, add a reference to the user control to your source page, and then add an instance of the usercontrol directly to the source page.
Statics don't have instance-based contexts, so using this is not applicable. You'll need a reference to the control for which you want to use .FindControl (possibly by passing it as a parameter).

Click Gmail Refresh button

I am writing a simple personal app that has a browser control and I want it to automatically "Refresh" gmail to check it more often than it does by default. There are monkey scripts that do this but I'm trying to add my personal style to it.
Anyhow, I've looked around and found everything but what I can do in csharp using the browser control.
I found this:
// Link the ID from the web form to the Button var
theButton = webBrowser_Gmail.Document.GetElementById("Refresh");
// Now do the actual click.
theButton.InvokeMember("click");
But it comes back with null in 'theButton' so it doesn't invoke anything.
Anyone have any suggestions?
It's been awhile since I've used JavaScript, but given the other answers and comments that there is no real ID associated with the element, could you do something like the following:
Search all Div's with an attribute of Role == 'Button' and an InnerHtml == 'Refresh'.
Once the correct InnerHtml is found, get the Element.
Invoke the click on the found Element.
Again, this may be blowing smoke, but thought I'd throw it out there.
edit: Just realized you are doing this with C# and a browser control; however, the concept would still be the same.
The best suggestion I could give you at this point involves an existing API that is used for .NET web browser based automation:
http://watin.org/
Since the div tag with the desired button really only seems to identify itself with the class name, you could use the Find.BySelector(“”) code included with the most recent version of watin.

Accessing a UserControl from different Usercontrol

I am trying to use the already written code which accesses a control from an other control in the following code.
Controls_SearchFeaturedMerchants UCMerchant = (Controls_SearchFeaturedMerchants)this.Parent.FindControl("UCSearchFeaturedMerchants1");
I am wondering what this statement says. Can someone please give me some idea :)
Thanks in advance!
If I understood your question correctly here is the brief:
On a page (ContentPage) is hosting two user controls (UsrCtrl1, UsrCtrl2).
Now "UsrCtrl1" wanted to access some data in the "UsrCtrl2".
For that I'll write code like - "UsrCtrl1" parent is the "ContentPage" that page hosts "UsrCtrl2", so I'll first get the "UsrCtrl2" from the parent page with the following code:
this.Parent.FindControl("UsrCtrl2Name") -> this means current user control which is "UsrCtrl1" and Parent means is the "ContentPage" (it may be parent control or page) in that you are trying to find a contrl with Id "UsrCtrl2Name" (this is the id of the 2nd user control in the content page).
When you use FindContrl method it always returns base type UserControl and you need to cast it to your usercontrol in this case it is of type "UsrCtrl2".
I hope this is clear now.
in plain text it is saying:
Get the Parent Control of the current control and then find UCSearchFeaturedMerchants1 on said control. Cast the result into a Controls_SearchFeaturedMerchants.
Hope that sums it up for you

Categories

Resources