首页 文章

在Xamarin Forms中登录后更改页面

提问于
浏览
4

我有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());
}

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

//Some code for login .....

MasterDetailPage fpm = new MasterDetailPage
{
      Master = new MainPageMenus(),
      Detail = new NavigationPage(new MainPage())
};
Application.Current.MainPage = fpm;

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

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

Application.Current.MainPage = new NavigationPage(new MainPageMenus());

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

这是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>

2 回答

  • 1

    这个:

    Application.Current.MainPage = new NavigationPage(new MainPageMenus());
    

    绝对是错的 . 在这里,您将使用MasterDetailPage作为根页面分配NavigationPage . 这不会起作用,因为只有MasterDetailPage的详细页面可以是导航页面 . 主页面不应受导航影响 .

    您实际应该做的是让您的MasterDetailPage的详细页面具有子页面(ContentPages)的NavigationPage .

    这段代码实际上看起来很好,但唯一的问题是:您将“MainPageMenus”(实际上是您的MasterDetailPage)设置为另一个MasterDetailPage的母版页 .

    //Some code for login .....
    
    MasterDetailPage fpm = new MasterDetailPage
    {
          Master = new MainPageMenus(),
          Detail = new NavigationPage(new MainPage())
    };
    Application.Current.MainPage = fpm;
    

    你应该做的是:创建一个与MainPageMenus相同的ContentPage,并将其命名为MainPageMenusAsContentPage . MainPageMenusAsContentPage的Xaml是:

    <?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>
    

    然后使用此代码:

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

    EDIT: 导航:

    要么masterDetailPage.Detail.PushAsync(new CoolPage());或者masterDetailPage.Detail = new NavigationPage(new CoolPage());

    它们的行为不同,第一个将页面推送到堆栈,第二个替换当前的详细信息页面

  • 3

    供参考,来自official documentation

    MasterDetailPage被设计为根页面,并且将其用作其他页面类型中的子页面可能会导致意外和不一致的行为 . 此外,建议MasterDetailPage的母版页应始终为ContentPage实例,并且只应使用TabbedPage,NavigationPage和ContentPage实例填充详细信息页面 . 这有助于确保跨所有平台的一致用户体验 .

    通常,移动应用程序使用身份验证令牌与API通信,该令牌可能会在任何时候到期或被拒绝 . 这意味着您还必须随时为用户重新身份验证做好准备 . 在这种情况下,您的 MainPage 可以自由地 MasterDetailPage ,并且当它或任何其他页面需要时,登录页面可以作为其顶部的模态页面 .

相关问题