在 Xamarin Forms 中登录后更改页面

6
我是一位有用的助手,可以为您翻译文本。

我有一个使用Xamarin Forms编写的应用程序,其中包含登录页面。当用户成功登录时,应用程序会导航到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页面,但问题在于当我点击菜单图标时,我得到的是详细页面而不是主页面。

这个问题与此帖子中描述的问题类似。上面的代码被选为答案,但对我来说不起作用。在Stack Overflow上,有一个类似的问题,答案是使用以下内容:
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>

你尝试过使用Navigation.InsertPageBefore而不是手动操作MainPage吗?这里有一个例子:https://github.com/xamarin/xamarin-forms-samples/blob/master/Navigation/LoginFlow/LoginNavigation/LoginPageCS.cs - EvZ
请见相关链接:https://dev59.com/EZffa4cB1Zd3GeqP6VaR。 - Felix D.
@FelixD。如果主页面不是MasterDetialPage,则链接中的解决方案有效。在我的情况下,菜单图标被隐藏,但当我从左侧滑到右侧时,会出现主页面。因此,我为问题和答案给出+1。 - Ahmed Shamel
2个回答

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

编辑:关于导航:

要么使用masterDetailPage.Detail.PushAsync(new CoolPage()); 要么使用masterDetailPage.Detail = new NavigationPage(new CoolPage());

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


哦,是的,我修复了这个方法错误,括号错了。而且你好像误解了主控和详细概念。主控=从侧面滑入的菜单。详细:呈现的页面。你的“MainPageMenus”本身就是一个MasterDetailPage,并且你正在将它设置为MasterDetailPage的MasterPage。明白了吗?你的Master和DetailPage是两个不同的ContentPages,你需要将它们添加到MasterDetailPage.Master和MasterDetailPage.Detail中。 - Csharpest
AnotherDetailPage()可以是你的"ApplicationSettingsPage"或者你的"HxSettingsPage"。 - Csharpest
我想我明白了,你的意思是我应该创建一个与 MainPageMenus 相同的内容页面吗? - Ahmed Shamel
1
我希望你创建一个主页面(侧边菜单页面)和一个详细页面(实际要显示的页面,例如ApplicationSettingsPage)。然后创建一个新的MasterDetailPage,并将其DetailPage设置为ApplicationSettingsPage实例,将主页面设置为您的侧边菜单页面实例。 - Csharpest
感谢你的解决方案。我编辑了你的答案,确切地解决了我的问题。我给你点赞。请接受编辑,使你的答案正确无误。 - Ahmed Shamel
显示剩余5条评论

3
作为参考,以下内容来自官方文档

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

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

在提问之前,我尝试了你的解决方案,它可以正常工作。但是在我的应用程序中,我无法使用这个解决方案。所以我给了一个加1,因为它实际上是一个很好的解决方案。 - Ahmed Shamel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接