简体   繁体   中英

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.

在此输入图像描述

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)

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

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:

 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:

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. How do I achieve this?

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

I think the simplest and clearest way to do this is using 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:

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

Result: 预习

Thanks to answer by Nikolai Doronin I got to work. Here is how I did it:

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:

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:

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

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

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

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