Navigate to a Master details Page in Xamarin forms - c#

Hello guys I am navigating to a master details page with this code:-
await Navigation.PushAsync(new MasterPage_Jobseeker());
But it is showing the back button instead the hamburger menu.
What should I do now?
Thank you!

Try turning the back button off on the NavigationPage.
var masterPage = new MasterPage_Jobseeker();
NavigationPage.SetHasBackButton(masterPage, false);
await Navigation.PushAsync(masterPage);
Although, I think if you're navigating to a MasterDetailPage you probably should be replacing the entire application main page with it to control the rest of your navigation from your MasterDetailPage.
Application.Current.MainPage = new MasterPage_Jobseeker();

Related

Xamarin Forms - Menu issue when navigating from Content Page to MasterDetail Page

When trying to navigate from a Content Page to a MasterDetail Page the menu icon does not appear. Although, if i first load my MasterDetail Page the menu icon is there as tis supposed to be. The menu functionality is not affected. Working on Xamarin Forms but the problem occurs only when debugging in Android device.
Any idea about the icon?
Load instantly MasterDetail Page using MainPage = new NavigationPage(new MainPage())
Load from Content Page (LoginPage) using MainPage = new NavigationPage(new Login()) then when a button is pressed navigate to MasterDetail Page using await Navigation.PushAsync(new MainPage(), true);
Menus works on both cases
Changing from a content page to a master detail page is achieved by changing your navigation stack. You need to replace the MainPage to do this.
It's two different types of navigation.
You usually see this type of stack swap when navigating from a login page which is usually a simple content page to the main app landing page which can be a master detail page.
So rather than this
await Navigation.PushAsync(new MainPage(), true);
Do this
Application.MainPage = new MainPage();
If you are using an MVVM framework, this can best be achieved with a StackService injected into the View Model.
The MVVM framework I use, FreshMVVM, has the ability to swap out navigation stacks built in.
Follow this link then jump to the "Switching out NavigationStacks on the Xamarin.Forms MainPage" section for more details.

open new page from nav drawer xamarin forms

I'm working on a Xamarin Forms App. This App has a ContentPage as MainPage, this ContenPage has 2 buttons. One of them, after being clicked, shows a new Page (I'm using App.Current.MainPage = page;) and this new page has a Nav Drawer.
The Nav Drawer shows perfectly, and all its pages work fine, but what I want to do is open a new page (lets call it "events") from one of this nav drawer pages. Every time a Nav Drawer Item is clicked, I use the code above:
if (menu == null)
return;
Page displayPage = (Page)Activator.CreateInstance(menu.TargetType); //the error shows here after trying to open my "events" page
Detail = new NavigationPage (displayPage);
menuPage.Menu.SelectedItem = null;
IsPresented = false;
But then, on my Nav Drawer Page, I'm trying to open my "events" page, but I don't know how to open it. I tried to use Navigation.PushAsyng(page) or App.Current.MainPage = page; but none of those work. Everytime I try to open it, this Unhandled Exception shows
System.Reflection.TargetInvocationException: Exception has been thrown
by the target of an invocation
I use this reference for creating my nav drawer https://syntaxismyui.com/xamarin-forms-masterdetail-page-navigation-recipe/
So, does someone know how to solve this problem and open my "events" page? I've been trying to do this for a long time without any results.
Thanks!

Prevent user from returning to last activity

I am working on a Xamarin.Forms project, and in my PCL i created startup page called "Login.xaml". On this page a user has to fill in their credentials and sends it to an WebAPI. When the API returns "1"/true it opens a new page called "Home.xaml".
How can i prevent the user from returning to the login page when pressing on the back button on the phone? ie: The user logs in on the first page, webapi validates it and returns a "1" so the new page ("Home") gets opened, but when the user presses the Back button it returns to the login screen. This should not be possible until the app gets closed down.
You can remove the login page from the navigation stack when you're pushing your Home.xaml:
await Navigation.PushAsync(new Home()); // or whatever your page is called
Navigation.RemovePage(this);
This way there's nothing to go back to after your user gets to the homepage.
For this to work, your login page needs to be a NavigationPage. For this, you'll have to wrap it with the NavigationPage ctor:
// this goes into your App.cs where you enter your app:
public App()
{
MainPage = new NavigationPage(new Login()); // or whatever your login page is called
}
I suggest you have a look at the documentation: Introduction to Xamarin Forms - Navigation
As for me, calling RemovePage() was giving me all sorts of problems.
What I had to do is shift my thinking. If you do Pop instead of Push, you're actually removing the page for good. So what do you need to do in order to be able to do Pop instead of Push? Insert the next page before the current page first, and then Pop:
var newRootPage = new NewRootPage();
this.Navigation.InsertPageBefore(newRootPage, this);
await this.Navigation.PopAsync();
Note: for this to work you will also need to do wrap your initial root page in a NavigationPage like #germi says in the last part of his answer:
// this goes into your App.cs where you enter your app:
public App()
{
// or whatever your login page is called
MainPage = new NavigationPage(new Login());
}
PS: FWIW this was my fix in my F# open source project.

Set Root Page as Modal in Xamarin

I am building a Xamarin app, where in my portable logic, I have my app.cs file, launching a HomePage. At the moment this is done ith the following code:
public App ()
{
MainPage = new NavigationPage (new HomePage (Adapter, IsLoggedIn));
}
However, I don't want the "title" bar to show at the top of this page, so would like to launch this page with a PushModalAsync command, so that HomePage is a Modal Page. However, the following line doesn't work, as in app.cs, Navigation is not recognised.
Navigation.PushModalAsync(new HomePage());
Is this possible and if so, how do I get around this?
Thanks.
I don't believe you can set the root page of your App to be a modal. However, if you just want to hide the navigation bar, you can do this in the page you want it hidden on:
NavigationPage.SetHasNavigationBar(this, false);
Since it's your main page you can just set the page itself. Say "HomePage" inherits from "ContentPage":
MainPage = new HomePage (Adapter, IsLoggedIn);

Navigating between pages contained in a Window

My question is if there's a more elegant solution for navigating between pages that are contained in a Window than searching that Window and changing it's content.
This's how I did it:
Page1 a = new Page1(param);
Window parent = Window.GetWindow(this);
parent.Content = a;
My thought is that if there are Navigation tools already implemented I should be using them, instead of doing that.
Thanks.
I think you can use Navigation Service of the page as follows:
GoTo Page2 From Page1.
Page2 p2 = new Page2();
this.NavigationService.Navigate(p2);
Also the better way is to use Page Function instead of Page.
See My answer for the Page Function here
or refer this msdn page

Categories