来自MasterDetailPage,Xamarin Forms的PushModalAsync [英] PushModalAsync from MasterDetailPage, Xamarin Forms

查看:199
本文介绍了来自MasterDetailPage,Xamarin Forms的PushModalAsync的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户从侧面菜单中点击注销"时,我试图导航至启动屏幕,因此它无法导航回到上一页. PushModalAsync可以解决这个问题,但是从我的MasterDetailPage执行此操作似乎无效.

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());

然后将其添加到我想要的位置:

var page5 = new MasterPageItem() { Title = "Log out", Icon = "signout.png", TargetType = typeof(goToMainPage) };-但这无法识别我的变量.

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.

当您按注销"时,我认为您必须转到登录"页面(也许您不知道"MainPage()" ...),并且此登录"页面应位于"NavigationStack"之外.

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

因此,如果您的情况是

  • PageA = LoginPage
  • PageB = MasterDetailPage(在LoginPage之后出现)

您应该在App.cs中做些稀薄的事情

You should do something like thin in your App.cs

Application.Current.MainPage = new PageA();

当您在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

这篇关于来自MasterDetailPage,Xamarin Forms的PushModalAsync的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆