与MasterDetails页面和导航Xamarin.forms [英] Xamarin.forms with MasterDetails page and navigation

查看:1672
本文介绍了与MasterDetails页面和导航Xamarin.forms的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序在那里我想有一个母版页有2个选项,并toolbarItems每个细节的页面。

I have a app where in I would like to have a master page with 2 options and toolbarItems for each of the details page.

例如,在这里我有一个设置页面作为我的详细信息页面有两个tollbaritems保存和取消。它是形成,其中用户将在用户数据有得到保存。因此,对保存toolbarItem的点击,我想保存数据并重定向用户到其他页面(说网页A),这是不包括在母版列表选项(Settins,登录)

For example, here I have a settings page as my details page which has two tollbaritems save and cancel. It is form wherein user has to put in userdata which has to get saved. So on click of save toolbarItem I would like to save the data and redirect user to some other page (say 'PageA') which is not included in Masterpage list options(Settins, Login)

我如何做呢?如果我做等待Navigation.PushAsync(新网页A()); 我没有得到母版。我回去图标返回掌握详细信息页面。但我想用户重定向到一个网页,其中也应该有masterdetails选项(设置和登录)

How can I go about it? If I do await Navigation.PushAsync(new PageA()); I do not get masterpage. I get back icon to go back to master details page. But I would like to redirect user to a page which should also have masterdetails option(settings and login).

我也试过这样的保存按钮点击:

I also tried this in save button click:

MasterDetailPage MDPage = new MasterDetailPage();
MDPage.Detail = new NavigationPage(new PageA());
MDPage.IsPresented = false;



先谢谢了。

Thanks in advance.

修改
这里是我的代码:

Edit Here is my code:

MasterPage.cs

MasterPage.cs

public ListView ListView { get { return listView; } }

        ListView listView;
        public MasterPage()
        {
            var masterPageItems = new List<MasterPageItem>();
            masterPageItems.Add(new MasterPageItem
            {
                Title = "Settings",
                //IconSource = "Settings.png",
                TargetType = typeof(TestPage)
            });

            listView = new ListView
            {
                ItemsSource = masterPageItems,
                ItemTemplate = new DataTemplate(() =>
                {
                    var imageCell = new ImageCell();
                    imageCell.SetBinding(TextCell.TextProperty, "Title");
                    imageCell.SetBinding(ImageCell.ImageSourceProperty, "IconSource");
                    return imageCell;
                }),
                VerticalOptions = LayoutOptions.FillAndExpand,
                SeparatorVisibility = SeparatorVisibility.None
            };

            Padding = new Thickness(0, 40, 0, 0);
           // Icon = "hamburger.png";
            Title = "Menu";
            BackgroundColor = Color.FromHex("#4F6276");

            Content = new StackLayout
            {
                VerticalOptions = LayoutOptions.FillAndExpand,
                Children = {
                    listView
                }
            };
        }



MasterDetailsPage:

MasterDetailsPage:

 public class MainPage : MasterDetailPage
    {
        public MainPage()
        {
            MasterPage masterPage;
            masterPage = new MasterPage();
            Master = masterPage;
            Detail = new NavigationPage(new TestPage())
            {
                //Tint = Color.Red // put your color here
                BarBackgroundColor = Color.FromRgb(172, 183, 193),//Color.FromHex("#9E9E9E"),
                BarTextColor = Color.Black,
                BackgroundColor = Color.White
            };
        }
     }



TestPage:

TestPage:

public class TestPage : ContentPage
    {
        public TestPage()
        {
            Button test = new Button();
            test.Clicked += test_Clicked;
            test.Text = "Save";
            Button test1 = new Button();
            test1.Clicked += test1_Clicked;
            test1.Text = "cancel";
            Content = new StackLayout
            {
                Children = {
                    test,
                    test1
                }
            };
        }

        void test_Clicked(object sender, EventArgs e)
        {
            //redirect to new page(Page1)
        }
        void test1_Clicked(object sender, EventArgs e)
        {
            //redirect to new page(Page2)
        }
    }

我要重定向到一个新的页面,从说onclick事件第1页。我如何做到这一点?

I want to redirect to a new page say page1 from onclick event. How do I achieve this?

推荐答案

保存设置之后,你应该改变的细节的已有的的MasterDetailPage实例。

After saving the settings you should change the detail of an exisiting MasterDetailPage instance.

我觉得这样做最简单和最清晰的方法是使用的 MessagingCenter

I think the simplest and clearest way to do this is using MessagingCenter:

在设置页面:

MessagingCenter.Send(new OpenMyPageMessage(), OpenMyPageMessage.Key);

您主从页面:

protected override void OnAppearing()
{
    MessagingCenter.Subscribe<OpenMyPageMessage>(this, OpenMyPageMessage.Key, (sender) =>
        {
            Detail = new YourAnotherPage();
        });
}

protected override void OnDisappearing()
{
    MessagingCenter.Unsubscribe<OpenMyPageMessage>(this, OpenMyPageMessage.Key);
}

OpenMyPageMes​​sage 是只是一个简单的类:

And OpenMyPageMessage is just a simple class:

public class OpenMyPageMessage
{
    public static string Key = "OpenMyPageMessage";
}



结果:

这篇关于与MasterDetails页面和导航Xamarin.forms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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