C# 当ListView ItemSelected位于其他类文件中时,如何导航到其上的页面?
我无法在应用程序中导航页面。我是Xamarin形式的初学者。我正在做的是,我有一个名为Master.cs的主控详细信息页,默认情况下,它会导航到主页,即内容页。我已经在其他类页面(即ModelClasses.cs)中创建了列表视图,现在我想在选择listview项的同时导航到其他内容页面。我尝试了更多的东西,但没有成功。 救救我 母版页C# 当ListView ItemSelected位于其他类文件中时,如何导航到其上的页面?,c#,listview,xamarin,C#,Listview,Xamarin,我无法在应用程序中导航页面。我是Xamarin形式的初学者。我正在做的是,我有一个名为Master.cs的主控详细信息页,默认情况下,它会导航到主页,即内容页。我已经在其他类页面(即ModelClasses.cs)中创建了列表视图,现在我想在选择listview项的同时导航到其他内容页面。我尝试了更多的东西,但没有成功。 救救我 母版页 public class master : MasterDetailPage { public master () {
public class master : MasterDetailPage
{
public master ()
{
Label header = new Label {
Text = "Master",
Font = Font.BoldSystemFontOfSize (20)
};
MenuStacklayout _MenuStacklayout = new MenuStacklayout ();
this.Master = new ContentPage{
Title=header.Text,
Content=_MenuStacklayout
};
this.Detail = new NavigationPage(new FTWHome());
// For Windows Phone, provide a way to get back to the master page.
if (Device.OS == TargetPlatform.WinPhone)
{
(this.Detail as ContentPage).Content.GestureRecognizers.Add(
new TapGestureRecognizer((view) =>
{
this.IsPresented = true;
}));
}
}
}
模型类
菜单堆栈布局创建菜单
public class MenuStacklayout : StackLayout
{
String[] homelinks = { "HOME" };
String[] healthlinks = { "I NEED HELP", "A MATE NEEDS HELP", "MOOD TRACKER", "HELPFUL ARTICLES" };
String[] entertainmentlinks = { "PHOTO OF THE DAY", "MAGZINE CONTENTS", "FTW TEAM" };
String[] aboutlinks = { "FTW PHILOSOPHY", "ABOUT FTW", "FTW STORE", "FTW SOCIAL" };
String[] joinftwlinks = { "JOIN FTW" };
String[] loginlinks = { "LOGIN" };
#region Menu Layout
public MenuStacklayout ()
{
ListView homelist = new ListView {
ItemsSource = menuitems._menuitems (homelinks),
ItemTemplate = new DataTemplate (typeof(ftwMenuCell))
};
homelist.RowHeight = 30;
homelist.ItemSelected+= navigatePage_onSelected;
StackLayout homeliststack = new StackLayout ();
homeliststack.Children.Add (homelist);
homelist.SeparatorVisibility = SeparatorVisibility.None;
//homeliststack.Padding = new Thickness (2, 5, 0, 0);
ListView healthlist = new ListView {
//Header = "HEALTH",
ItemsSource = menuitems._menuitems (healthlinks),
ItemTemplate = new DataTemplate (typeof(ftwMenuCell))
};
healthlist.RowHeight = 30;
healthlist.ItemSelected+=navigatePage_onSelected;
StackLayout healthliststack = new StackLayout ();
healthliststack.Children.Add (healthlist);
healthlist.SeparatorVisibility = SeparatorVisibility.None;
ListView entertainmentlist = new ListView {
//Header = "ENTERTAINMENT",
ItemsSource = menuitems._menuitems (entertainmentlinks),
ItemTemplate = new DataTemplate (typeof(ftwMenuCell))
};
entertainmentlist.RowHeight = 30;
entertainmentlist.ItemSelected+=navigatePage_onSelected;
StackLayout entertainmentliststack = new StackLayout ();
entertainmentliststack.Children.Add (entertainmentlist);
entertainmentlist.SeparatorVisibility = SeparatorVisibility.None;
ListView aboutlist = new ListView {
//Header = "ABOUT",
ItemsSource = menuitems._menuitems (aboutlinks),
ItemTemplate = new DataTemplate (typeof(ftwMenuCell))
};
aboutlist.RowHeight = 30;
aboutlist.ItemSelected+=navigatePage_onSelected;
StackLayout aboutliststack = new StackLayout ();
aboutliststack.Children.Add (aboutlist);
aboutlist.SeparatorVisibility = SeparatorVisibility.None;
ListView joinftwlist = new ListView {
ItemsSource = menuitems._menuitems (joinftwlinks),
ItemTemplate = new DataTemplate (typeof(ftwMenuCell))
};
joinftwlist.RowHeight = 30;
StackLayout joinftwliststack = new StackLayout ();
joinftwliststack.Children.Add (joinftwlist);
joinftwlist.SeparatorVisibility = SeparatorVisibility.None;
ListView loginlist = new ListView {
ItemsSource = menuitems._menuitems (loginlinks),
ItemTemplate = new DataTemplate (typeof(ftwMenuCell))
};
loginlist.RowHeight = 30;
loginlist.SeparatorVisibility = SeparatorVisibility.None;
loginlist.BackgroundColor = Color.Blue;
StackLayout loginstack = new StackLayout ();
loginstack.Children.Add (loginlist);
this.Spacing = 1;
this.BackgroundColor = Color.FromHex("#cccccc");
this.Padding = new Thickness (0, 65, 0, 0);
this.Orientation = StackOrientation.Vertical;
this.VerticalOptions = LayoutOptions.StartAndExpand;
this.Children.Add (homeliststack);
this.Children.Add (healthliststack);
this.Children.Add (entertainmentliststack);
this.Children.Add (aboutliststack);
this.Children.Add (joinftwlist);
this.Children.Add (loginstack);
}
}
菜单项模板
公共类ftwMenuCell:ViewCell
{
公共ftwMenuCell()
{
var namelab=新标签(){
FontFamily=“HelveticaNeue中等”,
FontSize=14,
FontAttributes=FontAttributes.Bold,
TextColor=Color.Black,
垂直选项=布局选项.Center
};
nameLabel.SetBinding(Label.TextProperty,“_链接”);
var cellLayout=新堆栈布局{
间距=0,
填充=新厚度(10,0,0,0),
VerticalOptions=LayoutOptions.FillAndExpand,
方向=堆叠方向。水平,
子项={namelab}
};
this.View=cellLayout;
}
}
公共类
{
公共字符串_链接{get;set;}
}
公共静态类菜单项
{
公共静态列表\u菜单项(字符串[]linkid)
{
列表菜单列表=新列表();
foreach(linkid中的变量项){
_menulink menulink=new_menulink();
菜单链接=项目;
menulinklist.Add(menulink);
}
返回菜单列表;
}
}
每个Listview项目都有各自的内容页。
提前谢谢 我看不到您导航的位置声明了Page_onSelected,但您可以尝试以下操作
void navigatePage_onSelected(object sender, SelectedItemChangedEventArgs e) {
Navigation.PushModalAsync(new PageName());
}
如果对你有效,请告诉我:)我已经解决了
在MenuStacklayout类中声明的navigatePage_onSelected事件处理程序中,我放入了以下代码,它正在工作
void navigatePage_onSelected(object sender, SelectedItemChangedEventArgs args){
_menulink menuitem = (_menulink)args.SelectedItem;
MasterDetailPage mstr = (MasterDetailPage)(Application.Current.MainPage); // My Application main page is a Masterpage so..
if (menuitem._link == "ABOUT FTW") {
mstr.Detail = new NavigationPage (new AboutFTW ());
}else if(menuitem._link == "I NEED HELP"){
mstr.Detail = new NavigationPage (new Entertainment ());
}else if(menuitem._link == "HOME"){
mstr.Detail = new NavigationPage (new FTWHome ());
}
// Show the detail page.
mstr.IsPresented = false;
}
是的,没有给出,但它在我的解决方案中。我会尽力让你知道的。谢谢我一直在等谁给我一些想法。不工作,这是错误的说法。导航。PushModalAsync(new PageName());这似乎是一个很好的解决方案,尽管我有一点意见。NavigationPages通常用作转到其他页面的中心。我看不到你页面的代码,所以我可能是错的,但看起来这些是目标页面。如果是这种情况,您可能需要使用ContentPage。这其实没那么重要,但我想我应该提一下:)@I_Khanage是的,提到的导航页面是我的目标页面,我的其他东西都是在那里写的。
void navigatePage_onSelected(object sender, SelectedItemChangedEventArgs args){
_menulink menuitem = (_menulink)args.SelectedItem;
MasterDetailPage mstr = (MasterDetailPage)(Application.Current.MainPage); // My Application main page is a Masterpage so..
if (menuitem._link == "ABOUT FTW") {
mstr.Detail = new NavigationPage (new AboutFTW ());
}else if(menuitem._link == "I NEED HELP"){
mstr.Detail = new NavigationPage (new Entertainment ());
}else if(menuitem._link == "HOME"){
mstr.Detail = new NavigationPage (new FTWHome ());
}
// Show the detail page.
mstr.IsPresented = false;
}