【问题标题】:PushModalAsync from MasterDetailPage, Xamarin Forms来自 MasterDetailPage、Xamarin 表单的 PushModalAsync
【发布时间】:2018-04-02 09:02:26
【问题描述】:

当用户从侧面菜单中点击“注销”时,我正在尝试导航到启动屏幕,因此它无法导航到返回上一页。 PushModalAsync 可以满足这一点,但从我的 MasterDetailPage 执行此操作似乎不起作用。

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;
        }



    }

我尝试过的: 我尝试将它分配给一个变量:

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

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

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

有没有其他方法可以完成这项工作?谢谢。

【问题讨论】:

    标签: xamarin xamarin.forms


    【解决方案1】:

    我认为你必须改变你的设计。

    当您按“注销”时,我认为您必须转到“登录”页面(也许您的“MainPage()”......我不知道)并且此“登录”页面应该在您的“NavigationStack”之外”。

    所以如果你的情况是

    • PageA = 登录页面
    • PageB = MasterDetailPage(出现在 LoginPage 之后)

    你应该在你的 App.cs 中做一些类似瘦的事情

    Application.Current.MainPage = new PageA();
    

    当您按下 PageA() 上的“登录按钮”时,您应该更改您的 MainPage

    Application.Current.MainPage = new PageB();
    

    您只对“详细信息”页面使用导航...所以当您按“注销”时,您不应该推送或弹出,但您应该更改您的主页

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

    【讨论】:

    • 谢谢。我试过这个但没有帮助。它仍在显示导航抽屉
    • 只需将其添加到 app.cs 即可使用: NavigationPage.SetHasNavigationBar(MainPage, false);
    猜你喜欢
    • 2016-03-13
    • 2018-03-01
    • 1970-01-01
    • 2017-01-24
    • 2018-08-16
    • 2019-06-12
    • 2015-04-21
    • 1970-01-01
    相关资源
    最近更新 更多