繁体   English   中英

Xamarin表单上的主要详细信息页面导航

[英]Master Detail Page Navigation on Xamarin Forms

我创建了一个“母版详细信息页面”,并希望实现在点击侧边选项卡之一时导航到特定页面的部分。 但是,它会继续返回到默认的详细信息页面,即默认内容页面。

    public class HomePage : MasterDetailPage
{
    string[] sideTabs = { "My Account", "Charity A", "Charity B", "Charity C", "Charity D", "Support", "Logout" };
    ListView listView = new ListView();
    ContentPage master = new ContentPage();
    StackLayout masterStack = new StackLayout();

    public HomePage()
    {
        NavigationPage.SetHasNavigationBar(this,false);

        //creating content page
        listView.Header = "     ";
        listView.ItemsSource = sideTabs;
        listView.SeparatorColor = Color.Transparent;
        listView.BackgroundColor = Color.Pink;
        masterStack.Children.Add(listView);
        master.Title = "Menu";
        master.Content = masterStack;

        //assigning master detail page properties
        Master = master;
        Detail = new NavigationPage(new ContentPage ());

        listView.ItemTapped += (sender, args) =>
        {
            // Set the BindingContext of the detail page.
            switch (args.Item)
            {
                case "My Account": Detail.BindingContext = new LoginPage (); break;
                default : Detail.BindingContext = args.Item; break;
            }
            // Show the detail page.
            IsPresented = false;
        };
    }
}

简短的答案是您做错了。

一种更动态的方法是使用一个class来保存TitlesContentPage Types

用来保存页面数据的类

public class MasterPageItem
{
    public string Title { get; set; }
    public Type TargetType { get; set; }
    public MasterPageItem(string title, Type targetType)
    {
        Title = title;
        TargetType = targetType;
    }
}

声明MasterPageItems列表

public class HomePage : MasterDetailPage
{
    // A Global List of Tabs (ie Pages)
    List<MasterPageItem> Pages = new List<MasterPageItem>();

    ...

填写您的清单

public HomePage()
{

    // You need to populate them with a Title and Page Type using typeof()

    Pages.Add(new MasterPageItem("My Account", typeof(LoginPage)));
    Pages.Add(new MasterPageItem("Charity At", typeof(CharityAPage)));
    Pages.Add(new MasterPageItem("Charity B", typeof(CharityBPage)));

    ...

您将必须指定如何在xaml中将Title链接到您的Listview或创建Data Template

示例ListView

...

// Here is an example of how you would use a data template in code

var listView = new ListView
    {
        ItemsSource = Pages,
        ItemTemplate = new DataTemplate(
            () =>
                {
                    var label = new Label();
                    label.VerticalOptions = LayoutOptions.FillAndExpand
                    label.SetBinding(Label.TextProperty, "Title");

                    var viewCell = new ViewCell();
                    viewCell.View = label;

                    return viewCell;
                })
    };

...

现在,当您订阅ItemTapped Eventargs参数包含一个MasterPageItem

ListView.ItemTapped

...

listView.ItemTapped += (sender, args) =>
{
    // you don't really need a switch here, as all your pages 
    // are kept in aa MasterPageItem 

    // Its Good to check if its not null
    if (args is MasterPageItem item)
    {

        // set the Detail page when click
        // Activator.CreateInstance, is just a fancy way of saying create the
        // page from the type you supplied earlier 
        Detail = new NavigationPage((Page)Activator.CreateInstance(item.TargetType));
        IsPresented = false;
    }
};

...

注意 :如有疑问,请务必阅读文档

主从页面

其他资源

Activator.CreateInstance方法(类型)

Xamarin.Forms.ListView.ItemTapped事件

资料范本

一些样品

暂无
暂无

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

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