简体   繁体   中英

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

The problem is I have this ObservableCollection I don't know how to add PopToRootAsync to this

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

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:

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

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. https://github.com/WendyZang/Test.git

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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