在Xamarin Forms中登录后更改页面

Ahm*_*mel 6 c# xaml xamarin xamarin.forms

我有xamarin表单应用程序,具有登录页面.当用户成功登录时,应用程序将导航到MainPageMenu主详细信息页面.我也有同样的问题.这是我在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());
}
Run Code Online (Sandbox Code Playgroud)

在登录页面中,我写了这段代码:

//Some code for login .....

MasterDetailPage fpm = new MasterDetailPage
{
      Master = new MainPageMenus(),
      Detail = new NavigationPage(new MainPage())
};
Application.Current.MainPage = fpm;
Run Code Online (Sandbox Code Playgroud)

应用程序正确导航到fpm页面,但问题是,当我按下菜单图标时,我得到的详细信息页面不是母版页面.

这个问题类似于一个描述,在此职位.并且上面的代码被选为问题的答案.但代码对我不起作用.在堆栈溢出中,有一个类似的问题,答案是使用这个:

Application.Current.MainPage = new NavigationPage(new MainPageMenus());
Run Code Online (Sandbox Code Playgroud)

但在这种情况下,菜单图标被隐藏.

这是MainPageMenus的Xml:

<?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>
Run Code Online (Sandbox Code Playgroud)

Csh*_*est 6

This:

Application.Current.MainPage = new NavigationPage(new MainPageMenus());
Run Code Online (Sandbox Code Playgroud)

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;
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

Then use this code:

var masterDetailPage = new MasterDetailPage()
{
    Master =new MainPageMenusAsContentPage(), 
    Detail = new NavigationPage(new MainPage())
};

Application.Current.MainPage = masterDetailPage;
Run Code Online (Sandbox Code Playgroud)

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