简体   繁体   中英

PushModalAsync from MasterDetailPage, Xamarin Forms

I am trying to navigate to start-up screen when user taps "Log out" from the side menu, therefore it cannot have navigation to go back to previous page. PushModalAsync accomodates this but doing it from my MasterDetailPage doesn't seem to work.

Profile.cs

 public partial class Profile : MasterDetailPage
    {
        public List<MasterPageItem> menuList { get; set; }
        public Profile()
        {
            InitializeComponent();
            this.lblMessage.Text = Settings.Name + " " + Settings.Surname;
            menuList = new List<MasterPageItem>();
            var page1 = new MasterPageItem() { Title = "Home", Icon = "home.png", TargetType = typeof(Page1) };
            var page2 = new MasterPageItem() { Title = "Cards", Icon = "card.png", TargetType = typeof(Cards) };
            var page3 = new MasterPageItem() { Title = "Transactions", Icon = "settings.png", TargetType = typeof(Transactions) };
            var page5 = new MasterPageItem() { Title = "Log out", Icon = "signout.png", TargetType = typeof(MainPage) };


            menuList.Add(page1);
            menuList.Add(page2);
            menuList.Add(page3);
            menuList.Add(page5);


            navigationDrawerList.ItemsSource = menuList;
            Detail = new NavigationPage((Page)Activator.CreateInstance(typeof(Page1)));

        }
        private void OnMenuItemSelected(object sender, SelectedItemChangedEventArgs e)
        {

            var item = (MasterPageItem)e.SelectedItem;
            Type page = item.TargetType;

            Detail = new NavigationPage((Page)Activator.CreateInstance(page));
            IsPresented = false;
        }



    }

What I have tried: I tried assigning it to a variable:

var goToMainPage = Navigation.PushModalAsync(new MainPage());

then adding it to where I want it:

var page5 = new MasterPageItem() { Title = "Log out", Icon = "signout.png", TargetType = typeof(goToMainPage) }; - But this didnt recognise my variable.

Are there any alternate ways of getting this done? Thanks.

I think you have to change your design.

When you press "Logout" I think you have to go to a "Login" page (maybe your "MainPage()"... I don't know) and this "Login" page should be outside your "NavigationStack".

So if your scenario is

  • PageA = LoginPage
  • PageB = MasterDetailPage (Appears After LoginPage)

You should do something like thin in your App.cs

Application.Current.MainPage = new PageA();

when you press "Login button" on PageA() you should change your MainPage

Application.Current.MainPage = new PageB();

You use Navigation only for your "Details" pages... so when you press "Logout" you should not Push or Pop, but you should change your MainPage

Application.Current.MainPage = new PageA(); // Come back to Login

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