Xamarin Forms navigating to a MasterDetail Page after a Content Page - xamarin

I'm creating an app using Xamarin Forms where when a user launches an app for the first time, they are sent to a login page. Once they log in, they're redirected to the MasterPage, which is a MasterDetail Page. However, when I try to navigate to the new MasterDetail Page, I only get the Detail Page, and the button to show the Master Portion is missing. The only thing appearing on the app bar is the title I set. I'm currently going to the MasterPage like this:
App.Current.MainPage = new NavigationPage(new MasterPage())
{
Title = "Welcome " + user.firstName
};
Every other run, this isn't an issue, since when a user opens the app it automatically logs them in and goes right to the MasterPage, except during their first use. However, this is a very important issue for me since it happens the first time a user uses my app and it will be part of their first impression.

When you use a MasterDetailpage, you should not wrap it into a Navigationpage (like your code-example). Because the MasterDetailPage should everytime be on top and not inside a NavigationPage.
To navigate between different pages you should wrap your DetailPage from the MasterDetailPage inside a NavigationPage.
Here some code for clarification:
var view = new MyFirstPage();
Application.Current.MainPage = new MasterPage(view);
And inside the MasterPage constructor, do the following:
public MasterPage(Page detailpage)
{
// Set the master-part
Master = new MasterContentPage();
// Set detail-part (with a navigation page)
Detail = new NavigationPage(detailpage);
}
And after that, you can navigate inside your app with this navigation:
var mdp = Application.Current.MainPage as MasterDetailPage;
await mdp.Detail.Navigation.PushAsync(myNextPage);
I usually create a static helper class AppNavigator to do this stuff.
For further information, look at the MasterDetailPage instructions on the Xamarin developer guides.

Related

Xamarin Forms - Navigation.PopAsync() not working

I have a tab page i've set to the main page in xamarin forms:
App.Current.MainPage = new MainPage();
MainPage is a tabbed page:
public partial class MainPage : TabbedPage
{....
Within the tabbedPage there is a content page, which has a button click to load a MasterDetail View:
await Navigation.PushAsync(new AttendMasterPage(new AttendanceViewModel(item)));
This works ok, but the masterdetail view is loaded within the tabbed page. so when i use Navigation.PopAsync(); on the Detail page once finished, nothing happens.
I need the page to go back to the original content page on the tabbed control using a button idealy.
Hope this makes sense, any more info needed please let me know
A MasterDetailPage is designed to be a root page, and using it as a
child page in other page types could result in unexpected and
inconsistent behavior. In addition, it's recommended that the master
page of a MasterDetailPage should always be a ContentPage instance,
and that the detail page should only be populated with TabbedPage,
NavigationPage, and ContentPage instances. This will help to ensure a
consistent user experience across all platforms.
Source: official doc
Please get familiar with the official documentation in order to prevent such problems.

How to Remove page from stack in prism xamarin forms

I have two pages when user open application first page get data ,
I created another page and when click add in first one it navigate to the second page , I want when adding the required data in the second page and click submit first page is removed and created again with the newest data
first page is CustomerAddressesPage and second one is AddNewAddressPage
this is my code
await _navigationService.NavigateAsync("/CustomerAddressesPage/AddNewAddressPage/CustomerAddressesPage");
It got me error
Removing views using the relative '../' syntax while navigating is only supported within a NavigationPage
The error tells you exactly how you can fix the issue.
_navigationService.NavigateAsync("/NavigationPage/CustomerAddressesPage/AddNewAddressPage/CustomerAddressesPage");
This of course assumes that you have registered the base NavigationPage from Xamarin.Forms for Navigation or have a custom NavigationPage that was registered with that key.

Replace Xamarin forms page without pushasync

I've searched through numerous online posts and can't find a suitable answer for this scenario. It's a pretty common requirement. I have a Xamarin.Forms app with a login page and listing page. When the app is started, the login page is displayed. When they login successfully, I want to display the listing page and prevent them from backing up by clicking on the toolbar back button or the hardware back button.
Change the MainPage
when you start the app you should have
Application.Current.MainPage = new LoginPage();
after login, something like
Application.Current.MainPage = new MyFirstPageAfterLogin();

Absolute URI won't work

Visual Studio 2017, Latest Xamarin.Forms, Latest Prism.
No matter what I try, I can't get my absolute URI to work. I have my app launch from app.xaml to a login page. In the loginpageviewmodel, I do some checks to see if the user is authenticated and if so, then I want to navigate to the mainpage.
I can navigate to the mainpage when not using absolute uri and it works - but the back button goes back to the login page - not wanted.
Code:
if (user.isAuthenticated)
{
navigationService.NavigateAsync("/AppMasterDetailPage/NavigationPage/MainPage"));
}
I've added all the http:// and the myapp.com, etc. ... nothing I try works.
How do I change the uri so absolute will navigate to proper main page and disallow the back button to the login page?
You can check out the following sample project which uses a "Login Screen" and a Hamburger Menu.
I haven't tested an attempted Navigation from OnNavigatingTo, but I wouldn't expect that to work properly as the page hasn't actually been pushed to the stack yet. That said if you did your check to see if the user is authenticated in OnNavigatedTo you could perform the navigation without a problem. If you do not want your user to see the LoginPage then the best I could tell you would be to have a SplashScreen that you first navigate to, and then do your logic from there to decide whether to go to your LoginPage or MasterDetailPage
I have a solution ...
I have app.xaml navigate to the loginpage. In the OnNavigatedTo method, I check for user authentication. If authenticated, then I call
NavigatgeAsync("/masterpage/navpage/mainpage");
and this gets me to the proper mainpage of the app and the back button DOES NOT go back to the login page - it exits app like expected. So the "/masterpage..." is the absolute uri I was looking for.
Next time you should do this instead _navigationService.NavigateAsync( new Uri("http://www.xxxx.com/Index/Navigation/Home", UriKind.Absolute)); this is after you have authenticated and have injected the NavigationService.

How to close the current Activity when user click Back button

In my project, I have implemented the MvvmCross to display fragment as page.
I have a Main Menu page which has Left drawer. It is defaulted to show Main Page as fragment.
In the main page, I have a drop-down menu in its action-bar. It will launch the respective page when user clicks an item
User clicks an item and it launches the Company info page.
Now, the Company info page is the current page being displayed.
Problem :
How to close the current display page which is company info page when user click the back button? When this page is closed, the main page will be seen.
where to write the logic to handle the back button ?
In Main Menu.cs or Main Page.cs or Company info.cs
public override void OnBackPressed()
{
}
You can call the finish(); method of the activity. Do this in your 'Company' activity. That will close the current activity and returns to the previous (as far as I know).
More Infos about the method: Xamarin Developer

Resources