簡體   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