Xamarin表格上的主细节页面导航

时间:2018-01-09 07:52:52

标签: c# 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;
        };
    }
}

1 个答案:

答案 0 :(得分:3)

简短的回答是你做错了。

更有效的方法是使用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中指定如何将TitleListview相关联,或创建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 Event时,args参数包含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;
    }
};

...
  

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

Master-Detail Page

其他资源

Activator.CreateInstance Method (Type)

Xamarin.Forms.ListView.ItemTapped Event

Data Templates

Some more samples