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);
Related
The root of a .NET MAUI application is the class App : Application.
This class has a property Page? MainPage that contains the current root page.
I try to use this property for navigation. I wrote a method App.ShowPage(Page page) that sets the property MainPage to the given page.
This works like a charm! I start my application and show a login page - works.
When the user clicks login on my login page I show the main view (a TabbedPage) - works.
When the user clicks on an item on that main view a details view gets shown - works.
Now when the user clicks cancel on that detail view, I try to navigate back to the main view. As always with my method App.ShowPage(Page page). Now I get an exception:
IllegalStateException; The specified child already has a parent. You
must call removeView() on the child's parent first.
So the error message tries to tell me what to do. And I do find properties like Element Parent or Element RealParent on the Page page I try to open - but they are all null. So how do I remove the page from that parent? What is the removeView() method in the .NET MAUI world?
Thanks in advance for any help!
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();
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.
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.
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