首页 > 解决方案 > Xamarin.Forms MasterDetailPage 关闭 MasterPageItem

问题描述

我有一个 MasterDetailPage 并且我有侧边栏菜单?与 MasterPageItem。现在我正在尝试关闭呈现的 MasterPageItem。我该怎么做?

这是我的 MasterDetailPage:

<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms" 
                  xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
                  xmlns:local="clr-namespace:MyApp;assembly=MyApp"
                  x:Class="MyApp.MainPage">
  <MasterDetailPage.Master>
    <local:MasterPage x:Name="masterPage" />
  </MasterDetailPage.Master>
    <MasterDetailPage.Detail>
        <NavigationPage>
            <x:Arguments>
                <local:HomePage />
            </x:Arguments>
        </NavigationPage>
    </MasterDetailPage.Detail>
</MasterDetailPage>

和 OnItemSelected 方法:

void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            var item = e.SelectedItem as MasterPageItem;
            if (item != null)
            {

                if(item.Title == "Logout")
                {
                    SecureStorage.RemoveAll();
                    MessagingCenter.Send<Paindown.App>((Paindown.App)Xamarin.Forms.Application.Current, "UpdateMenu");
                    IsPresented = false;
                }
                else
                {
                    Detail = new NavigationPage((Page)Activator.CreateInstance(item.TargetType));
                    masterPage.listView.SelectedItem = null;
                    IsPresented = false;
                }
            }
        }

这是我在 MasterPage 的侧边栏菜单中定义页面的方式:

menu.Add(new MasterPageItem
                {
                    Title = "Login",
                    TargetType = typeof(LoginPage)
                });

我的问题来自 LoginPage,如何返回 HomePage?

标签: c#xamarin.forms

解决方案


您可以使用MessageCenter将消息从LoginPage发送到MenuPage,然后可以使MenuPage将所选项目设置为HomePage

例如,从LoginPage发送消息:

void Button_Clicked(object sender, EventArgs e)
{
    //MenuItemType.HomePage is a int value to represent the HomePage
    MessagingCenter.Send<object, int>(this, "Home", (int)MenuItemType.HomePage);
}

然后在MenuPage中可以设置当前页面为HomePage

public partial class MenuPage : ContentPage
{
    public MenuPage()
    {
       ...

       //ListViewMenu is the name of ListView in Slider menu
       //menuItems is the ViewModel which contains the HomePage

       MessagingCenter.Subscribe<object,int>(this, "Home", (senders,arg) =>
        {
            ListViewMenu.SelectedItem = menuItems[arg];
        });

       ...
    }

}  

==================================更新================ =================

如果想要对此进行动画处理,您需要打开滑块菜单并手动关闭它。

在此之前,我们可以创建一个主页public static Instance面( MasterDetailPage)。然后我们可以在MenuPage中使用它来使用IsPresented属性。

主页

public partial class MainPage : MasterDetailPage
{
  ...
  public static MainPage Instance { set; get; }

  ...

  public MainPage()
  {
   ...
   Instance = this;
   ..
  
  }
}

然后MenuPage会修改之前的方法MessagingCenter.Subscribe如下:

...
MessagingCenter.Subscribe<object,int>(this, "Home",async (senders,arg) =>
{
    // open slider menu
    MainPage.Instance.IsPresented = true;
    // set selected item
    ListViewMenu.SelectedItem = menuItems[arg];
    // wait 0.5 second
    await Task.Delay(500);
    //close slider menu
    MainPage.Instance.IsPresented = false;
});
...

推荐阅读