简体   繁体   English

如何在主详细信息页面上使用 PopToRootAsync?

[英]How to user PopToRootAsync on Master Detail Page?

I have a master detail page I would like to add a logout functionality (basically a poptorootasync) in that.我有一个主详细信息页面,我想在其中添加一个注销功能(基本上是一个 poptorootasync)。 For example here is the the list of the menu:例如这里是菜单列表:
Client Information - This will redirect the user to client info page.客户信息- 这会将用户重定向到客户信息页面。
Logout - This will use the " await Application.Current.MainPage.Navigation.PopToRootAsync(); " function.注销- 这将使用“ await Application.Current.MainPage.Navigation.PopToRootAsync(); ”function。

The problem is I have this ObservableCollection I don't know how to add PopToRootAsync to this问题是我有这个 ObservableCollection 我不知道如何将 PopToRootAsync 添加到这个

Here is my Code:这是我的代码:

MenuItems = new ObservableCollection<HomePageMenuItem>(new[]
{
   new HomePageMenuItem { Id = 0, Title = "Client Information", TargetType = typeof(ClientInformationMenu) },
   new HomePageMenuItem { Id = 1, Title = "Logout", TargetType = typeof(**HERE IS WHERE TO PUT POP TO ROOT**) }
});

HomePageMenuItem.cs HomePageMenuItem.cs

public class HomePageMenuItem
{
    public HomePageMenuItem()
    {
        TargetType = typeof(ClientInformationMenu);
    }
    public int Id { get; set; }
    public string Title { get; set; }
    public Type TargetType { get; set; }
}

lst selected event上次选择的事件

private void ListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
    {
        HomePageMenuItem item = e.SelectedItem as HomePageMenuItem;
        if (item == null)
            return;

        var page = (Page)Activator.CreateInstance(item.TargetType);
        page.Title = item.Title;

        Detail = new NavigationPage(page);
        IsPresented = false;

        MasterPage.ListView.SelectedItem = null;
    }

Your HomePageMenuItem would look something like this:您的 HomePageMenuItem 看起来像这样:

public class HomePageMenuItem
{
    public HomePageMenuItem()
   {
      TargetType = typeof(ClientInformationMenu);
   }
    public bool IsHome{ get; set;}
    public int Id { get; set; }
    public string Title { get; set; }
    public Type TargetType { get; set; }
}

You will initialize it something like this:您将像这样初始化它:

   new HomePageMenuItem { Id = 1, Title = "Logout", IsHome= true) }

Then in your click event, you check this property and perform the action accordingly然后在您的点击事件中,您检查此属性并相应地执行操作

if(Obj.IsHome)
{
    //Code to Pop
}

I guess you have a login page to navigate to the master detail page.我猜你有一个登录页面可以导航到主详细信息页面。 When you click logout in master details page, it would navigation to the root page as logout page.当您在主详细信息页面中单击注销时,它将导航到根页面作为注销页面。

I think Navigation.PopToRootAsync() is not a good choice, you could set the main page at runtime directly.我认为Navigation.PopToRootAsync()不是一个好的选择,您可以直接在运行时设置主页。 And please note, MasterDetailPage is designed to be a root page, there is no need to use Navigation.PopToRootAsync() to go back to root page.请注意,MasterDetailPage 设计为根页面,无需使用Navigation.PopToRootAsync()将 go 返回到根页面。

This the code set the logout as root page.此代码将注销设置为根页面。

void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
    {
        var item = e.SelectedItem as MasterPageItem;
        if (item != null)
        {
            if (item.Title == "Logout")
            {
                Application.Current.MainPage =new LogoutPageCS();
            }
            else
            {
                Detail = new NavigationPage((Page)Activator.CreateInstance(item.TargetType));
                masterPage.ListView.SelectedItem = null;
                IsPresented = false;
            }

        }
    }

在此处输入图像描述

For more details, you could download the source file from MasterDetailsDemo folder of GitHub.有关详细信息,您可以从 GitHub 的 MasterDetailsDemo 文件夹中下载源文件。 https://github.com/WendyZang/Test.git https://github.com/WendyZang/Test.git

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM