gpt4 book ai didi

c# - Xamarin Forms 上的主从页面导航

转载 作者:行者123 更新时间:2023-11-30 21:36:29 25 4
gpt4 key购买 nike

我创建了一个 Master Detail 页面,并试图实现在点击其中一个侧边选项卡时导航到特定页面的部分。但是,它一直返回到默认的详细信息页面,这是一个纯内容页面。

    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;
};
}
}

最佳答案

简短的回答是你做错了。

一种更动态的方法是使用 来保存您的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 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;
}
};

...

Note : If in doubt always read the documentation

Master-Detail Page

其他资源

Activator.CreateInstance Method (Type)

Xamarin.Forms.ListView.ItemTapped Event

Data Templates

Some more samples

关于c# - Xamarin Forms 上的主从页面导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48163587/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com