简体   繁体   English

Xamarin.forms与MasterDetails页面和导航

[英]Xamarin.forms with MasterDetails page and navigation

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. 我有一个应用程序,我希望在每个详细信息页面都有一个包含2个选项和toolbarItems的母版页。

在此输入图像描述

For example, here I have a settings page as my details page which has two tollbaritems save and cancel. 例如,这里我有一个设置页面作为我的详细信息页面,其中有两个tollbaritems保存和取消。 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) 所以点击保存toolbarItem我想保存数据并将用户重定向到其他页面(比如'PageA'),这不包含在主页列表选项中(Settins,Login)

How can I go about it? 我该怎么办呢? If I do await Navigation.PushAsync(new PageA()); 如果我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). 但我想将用户重定向到一个页面,该页面也应该有masterdetails选项(设置和登录)。

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

I want to redirect to a new page say page1 from onclick event. 我想从onclick事件重定向到新页面说page1。 How do I achieve this? 我该如何实现这一目标?

After saving the settings you should change the detail of an exisiting MasterDetailPage instance. 保存设置后,您应该更改现有 MasterDetailPage实例的详细信息。

I think the simplest and clearest way to do this is using MessagingCenter : 我认为最简单,最清晰的方法是使用MessagingCenter

In settings page: 在设置页面中:

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

Your master-detail page: 您的主要详细信息页面:

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

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

And OpenMyPageMessage is just a simple class: OpenMyPageMessage只是一个简单的类:

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

Result: 结果: 预习

Thanks to answer by Nikolai Doronin I got to work. 感谢Nikolai Doronin的 回答 ,我开始工作了。 Here is how I did it: 我是这样做的:

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
            };
        }
        protected override void OnAppearing()
        {
            MessagingCenter.Subscribe<Class1.OpenMyPageMessage>(this, Class1.OpenMyPageMessage.Key, (sender) =>
            {
                Detail = new NavigationPage (Page1() };
            });
            MessagingCenter.Subscribe<Class1.OpenPage2>(this, Class1.OpenPage2.Key, (sender) =>
            {
                Detail = new NavigationPage( new Page2());
            });
        }

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

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)
        {
             MessagingCenter.Send(new Class1.OpenPage2(), Class1.OpenPage2.Key);
        }
        void test1_Clicked(object sender, EventArgs e)
        {

        }
    }

class.cs class.cs

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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