我有一个listView。我要实现的目标是,当我单击列表项目时,它应该导航到该特定项目的另一页。我得到一个示意,要求我使用导航页面(请参阅代码中的评论)。
mainpage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:App1"
x:Class="App1.MainPage"
Title="MainPage">
<ContentPage.Content>
<StackLayout>
<Label Text="Dynamic ListView" HorizontalOptions="Center"></Label>
<ListView x:Name="lstView" HasUnevenRows="true">
</ListView>
</StackLayout>
</ContentPage.Content>
</ContentPage>
mainpage.xaml.cs
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
//BindingContext = this;
string[] courses =
{
"ADO.NET By Example",
"Xamarin.Android",
"Xamarin.iOS",
"Xamarin.Forms",
"JAVA EE: Java Server Pages",
"JAVA EE: Programming Servlets"
};
lstView.ItemsSource = courses;
lstView.ItemSelected += async (sender, e) =>
{
if (e.SelectedItem == null)
{
// don't do anything if we just de-selected the row
return;
}
else
{
if (e.SelectedItem.ToString() == "ADO.NET By Example")
{
await Navigation.PushAsync(new Project1()); /// It throws an error here saying I need to use a Navigation Page.
}
}
};
}
}
新页面Project1与主页处于同一级别。另外,我看到有些人建议在getMainPage()方法中更改代码。我的应用中没有GetMainPage()。
如果您尝试使用
await Navigation.PushAsync(new Project1());
您必须将MaingPage定义为导航页
MainPage = new NavigationPage(new MainPage());
然后您可以传递到项目1您的项目
await Navigation.PushAsync(new Project1(e.SelectedItem));
我建议您看看MVVM具有最清洁的代码