Changer de page après la connexion dans Xamarin Forms
J'ai une application de formulaire xamarin qui a une page de connexion. Lorsque l'utilisateur se connecte avec succès, l'application accède à MainPageMenu
une page principale de détails. J'ai le même problème. Voici mon code dans app.cs:
public App ()
{
InitializeComponent();
if (ApplicationSettings.NotLogin()) // Method to check if use if logged in or not
MainPage = new LoginPage();
else
MainPage = new NavigationPage(new MainPageMenus());
}
Dans la page de connexion, j'écris ce code:
//Some code for login .....
MasterDetailPage fpm = new MasterDetailPage
{
Master = new MainPageMenus(),
Detail = new NavigationPage(new MainPage())
};
Application.Current.MainPage = fpm;
L'application accède correctement à la page fpm, mais le problème est que lorsque j'appuie sur l'icône de menu, j'obtiens la page de détails et non la page maître.
Ce problème est similaire à celui décrit dans cet article . Et le code ci-dessus est sélectionné comme réponse à la question. Mais le code ne fonctionne pas pour moi. Dans stack Overflow, il y a une question similaire et la réponse était d'utiliser ceci:
Application.Current.MainPage = new NavigationPage(new MainPageMenus());
But in this case the menu icon is hidden.
Here is the Xml of MainPageMenus:
<?xml version="1.0" encoding="utf-8" ?>
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="XProject.Menus.MainPageMenus"
xmlns:local="clr-namespace:XProject"
Title="E-Clinic"
>
<MasterDetailPage.Master>
<ContentPage Title="Menu">
<StackLayout Orientation="Vertical">
<Button Clicked="GoToApplicationSettingsPage" Text="Application Settings"></Button>
<Button Clicked="GoToHxSettingsPage" Text="Medical History Settings"></Button>
<Button Clicked="GoToVisitsSettingsPage" Text="Visits Settings"></Button>
<Button Clicked="GoToCustomFieldsPage" Text="Custom Fields"></Button>
<Button Clicked="GoToAddCustomFieldsPage" Text="Add Custom Fields"></Button>
<Button Clicked="GoToAddCommonInfoPage" Text="Common Info"></Button>
<Button Clicked="GoToAddStatisticsPage" Text="Statistics"></Button>
<Button Clicked="GoToBackupPage" Text="Create Backup"></Button>
<Button Clicked="GoToRestoreBackupPage" Text="Restore Backup"></Button>
</StackLayout>
</ContentPage>
</MasterDetailPage.Master>
<MasterDetailPage.Detail>
<local:MainPage></local:MainPage>
</MasterDetailPage.Detail>
</MasterDetailPage>
This:
Application.Current.MainPage = new NavigationPage(new MainPageMenus());
is definitely wrong. Here you are assigning a NavigationPage with a MasterDetailPage as a root page. This is not gonna work, because only the detail page of the MasterDetailPage can be a navigation page. The Master Page should not be affected by the navigation.
What you should actually do is having the DETAIL PAGE of your MasterDetailPage a NavigationPage with child Pages (ContentPages).
This code is actually looking well, but the only problem is: You are setting "MainPageMenus" (which is in fact your MasterDetailPage) as the master page of ANOTHER MasterDetailPage.
//Some code for login .....
MasterDetailPage fpm = new MasterDetailPage
{
Master = new MainPageMenus(),
Detail = new NavigationPage(new MainPage())
};
Application.Current.MainPage = fpm;
What you should do is: Create a ContentPage that is the same as MainPageMenus and lets name it as MainPageMenusAsContentPage. The Xaml of MainPageMenusAsContentPage is:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="XProject.Menus.MainPageMenusAsContentPage"
Title="Menu"
>
<ContentPage.Content>
<StackLayout Orientation="Vertical">
<Button Clicked="GoToApplicationSettingsPage" Text="Application Settings"></Button>
<Button Clicked="GoToHxSettingsPage" Text="Medical History Settings"></Button>
<Button Clicked="GoToVisitsSettingsPage" Text="Visits Settings"></Button>
<Button Clicked="GoToCustomFieldsPage" Text="Custom Fields"></Button>
<Button Clicked="GoToAddCustomFieldsPage" Text="Add Custom Fields"></Button>
<Button Clicked="GoToAddCommonInfoPage" Text="Common Info"></Button>
<Button Clicked="GoToAddStatisticsPage" Text="Statistics"></Button>
<Button Clicked="GoToBackupPage" Text="Create Backup"></Button>
<Button Clicked="GoToRestoreBackupPage" Text="Restore Backup"></Button>
</StackLayout>
</ContentPage.Content>
</ContentPage>
Then use this code:
var masterDetailPage = new MasterDetailPage()
{
Master =new MainPageMenusAsContentPage(),
Detail = new NavigationPage(new MainPage())
};
Application.Current.MainPage = masterDetailPage;
EDIT: For navigation:
either masterDetailPage.Detail.PushAsync(new CoolPage()); or masterDetailPage.Detail = new NavigationPage(new CoolPage());
They behave different, the first pushes a page to the stack, the second replaces the current detail page