C# Xamarin表单上的母版详细信息页导航
我创建了一个Master Detail页面,并正在实现一个部件,当点击其中一个侧选项卡时,它将导航到一个特定页面。但是,它会继续返回默认的详细信息页面,这是一个普通的内容页面C# Xamarin表单上的母版详细信息页导航,c#,xamarin.forms,C#,Xamarin.forms,我创建了一个Master Detail页面,并正在实现一个部件,当点击其中一个侧选项卡时,它将导航到一个特定页面。但是,它会继续返回默认的详细信息页面,这是一个普通的内容页面 public class HomePage : MasterDetailPage { string[] sideTabs = { "My Account", "Charity A", "Charity B", "Charity C", "Charity D", "Support", "Logout" };
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;
};
}
}
简单的回答是你做错了 一种更为动态的方法是使用
类
来保存标题
和内容页
类型
保存页面数据的类
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>();
...
您必须在xaml中指定如何将标题
链接到列表视图
,或者创建数据模板
示例列表视图
...
// 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;
})
};
...
现在,当您订阅itemtrapped
事件时,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;
}
};
...
注意:如果有疑问,请务必阅读文档
其他资源
public class HomePage : MasterDetailPage
{
// A Global List of Tabs (ie Pages)
List<MasterPageItem> Pages = new List<MasterPageItem>();
...
@RajdeepSinghHundal很高兴能为您提供帮助