Xamarin.forms Xamarin表单:如何将具有参数的contentpage添加到列表中?
我正在我的项目上实现Xamarin.forms Xamarin表单:如何将具有参数的contentpage添加到列表中?,xamarin.forms,navigation-drawer,master-detail,Xamarin.forms,Navigation Drawer,Master Detail,我正在我的项目上实现导航抽屉。为了这个特性,我遵循这个 Mainpage.xsml.cs: public partial class NavigationDrawerPage : MasterDetailPage { public List<MasterPageItem> menuList { get; set; } public MainPage() { InitializeComponent();
导航抽屉
。为了这个特性,我遵循这个
Mainpage.xsml.cs:
public partial class NavigationDrawerPage : MasterDetailPage
{
public List<MasterPageItem> menuList { get; set; }
public MainPage()
{
InitializeComponent();
menuList = new List<MasterPageItem>();
// Adding menu items to menuList and you can define title ,page and icon
menuList.Add(new MasterPageItem() { Title = "Home", Icon = "home.png", TargetType = typeof(HomePage) });
menuList.Add(new MasterPageItem() { Title = "Setting", Icon = "setting.png", TargetType = typeof(SettingPage) });
menuList.Add(new MasterPageItem() { Title = "LogOut", Icon = "logout.png", TargetType = typeof(LogoutPage) });
navigationDrawerList.ItemsSource = menuList;
Detail = new NavigationPage((Page)Activator.CreateInstance(typeof(HomePage)));
}
}
menuList.Add(new MasterPageItem() { Title = "Home", Icon = "home.png", TargetType = typeof(SettingPage) });
menuList.Add(new MasterPageItem() { Title = "Help", Icon = "help.png", TargetType = typeof(HelpPage(false)) }); //showing syntax errors for this line
我的问题是将我的内容页添加到列表时。以上代码工作正常:
public partial class NavigationDrawerPage : MasterDetailPage
{
public List<MasterPageItem> menuList { get; set; }
public MainPage()
{
InitializeComponent();
menuList = new List<MasterPageItem>();
// Adding menu items to menuList and you can define title ,page and icon
menuList.Add(new MasterPageItem() { Title = "Home", Icon = "home.png", TargetType = typeof(HomePage) });
menuList.Add(new MasterPageItem() { Title = "Setting", Icon = "setting.png", TargetType = typeof(SettingPage) });
menuList.Add(new MasterPageItem() { Title = "LogOut", Icon = "logout.png", TargetType = typeof(LogoutPage) });
navigationDrawerList.ItemsSource = menuList;
Detail = new NavigationPage((Page)Activator.CreateInstance(typeof(HomePage)));
}
}
menuList.Add(new MasterPageItem() { Title = "Home", Icon = "home.png", TargetType = typeof(SettingPage) });
menuList.Add(new MasterPageItem() { Title = "Help", Icon = "help.png", TargetType = typeof(HelpPage(false)) }); //showing syntax errors for this line
但是我有一个只有一个参数的页面,我无法将该参数添加到TargetType
中。请参阅以下代码:
public partial class NavigationDrawerPage : MasterDetailPage
{
public List<MasterPageItem> menuList { get; set; }
public MainPage()
{
InitializeComponent();
menuList = new List<MasterPageItem>();
// Adding menu items to menuList and you can define title ,page and icon
menuList.Add(new MasterPageItem() { Title = "Home", Icon = "home.png", TargetType = typeof(HomePage) });
menuList.Add(new MasterPageItem() { Title = "Setting", Icon = "setting.png", TargetType = typeof(SettingPage) });
menuList.Add(new MasterPageItem() { Title = "LogOut", Icon = "logout.png", TargetType = typeof(LogoutPage) });
navigationDrawerList.ItemsSource = menuList;
Detail = new NavigationPage((Page)Activator.CreateInstance(typeof(HomePage)));
}
}
menuList.Add(new MasterPageItem() { Title = "Home", Icon = "home.png", TargetType = typeof(SettingPage) });
menuList.Add(new MasterPageItem() { Title = "Help", Icon = "help.png", TargetType = typeof(HelpPage(false)) }); //showing syntax errors for this line
如何将包含参数的内容页添加到列表项中?我是否应该更改
TargetType
datatype
?您可以使用方法公共静态对象CreateInstance(Type Type,params object[]args)传递参数
项目
母版页
添加具有相应参数的构造函数
我假设您正在使用TargetType
实例化详细信息页面。您可以将具有默认值的字典
添加到MasterPageItem
,并在创建详细信息后传递它们instance@shanranm你能分享一个样品吗?