Xamarin形成MasterDetailPage导航

时间:2016-09-16 10:20:14

标签: xamarin.forms master-detail

所以我一直在研究一个使用MasterDetail页面的应用程序并且它很好但我对它如何浏览页面感到有点困惑。

目前我的菜单项在应用程序中打开了一些页面,而且这些部分运行良好,侧面菜单保持不变。与之混淆的是如何处理显示主页面上的按钮。我的按钮此刻只是打开一个新页面,但MasterDetail页面的侧面菜单刚刚消失在常规的NavigationPage中。

我将在下面给出我的按钮代码。

btnSocial.GestureRecognizers.Add(new TapGestureRecognizer
{
    Command = new Command(() =>
    {
        Navigation.PushAsync(new SocialPage());
    })
});

这是MasterDetail页面导航的方式,还是您认为我做错了什么?

**已编辑**

只是有了这个帮助,我会附上我的菜单和启动页代码:

MenuPage.cs

public class MenuPage : ContentPage
{
    public Action<ContentPage> OnMenuSelect { get; set; }

    public MenuPage()
    {
        Title = "Menu";
        Icon = "ic_menu.png";
        BackgroundColor = ProjectVariables.PRIMARY_COLOR;

        var items = new List<MenuItems>()
        {
            new MenuItems("Social", () => new SocialPage()),
            new MenuItems("Career", () => null),
            new MenuItems("MySchedule", () => null),
            new MenuItems("Videos", () => null),
            new MenuItems("Contact", () => null),
            new MenuItems("Sign in", () => null)
        };

        var dataTemplate = new DataTemplate(typeof(TextCell));

        dataTemplate.SetValue(TextCell.TextColorProperty, Color.White);
        dataTemplate.SetBinding(TextCell.TextProperty, "Name");

        var listview = new ListView()
        {
            ItemsSource = items,
            ItemTemplate = dataTemplate
        };

        listview.BackgroundColor = ProjectVariables.PRIMARY_COLOR;
        listview.ItemSelected += (object sender, SelectedItemChangedEventArgs e) =>
        {
            if(OnMenuSelect != null)
            {
                var item = (MenuItems)e.SelectedItem;
                var itemPage = item.PageFn();

                OnMenuSelect(itemPage);
            }
        };

        Content = new StackLayout
        {
            Orientation = StackOrientation.Vertical,
            Children =
            {
                listview
            }
        };
    }
}

LaunchPage.cs

public class LaunchPage : MasterDetailPage
{
    public LaunchPage()
    {
        var menuPage = new MenuPage();

        menuPage.OnMenuSelect = (categoryPage) =>
        {
            Detail = new NavigationPage(categoryPage);
            //Detail.Navigation.PushAsync(categoryPage);
            IsPresented = false;
        };

        Master = menuPage;
        Detail = new NavigationPage(new MainPage())
        {
            BarTextColor = Color.White,
            BarBackgroundColor = ProjectVariables.PRIMARY_COLOR
        };

        MasterBehavior = MasterBehavior.Split;
    }
}

1 个答案:

答案 0 :(得分:0)

看看Xamarin的this documentation page

看起来你没有使用导航服务。您需要对主页面的引用并为其设置Detail属性。

特别关注this section

public partial class MainPage : MasterDetailPage
{
    public MainPage ()
    {
        ...
        masterPage.ListView.ItemSelected += OnItemSelected;
    }

    void OnItemSelected (object sender, SelectedItemChangedEventArgs e)
    {
        var item = e.SelectedItem as MasterPageItem;
        if (item != null) {
            Detail = new NavigationPage ((Page)Activator.CreateInstance (item.TargetType));
            masterPage.ListView.SelectedItem = null;
            IsPresented = false;
        }
    }
}

在选择ListView项时,他们设置了Detail属性,它将为您进行导航。