[英]How to open another page when a listview item is selected in xamarin forms?
Hello I am working on a mobile app that is built using xamarin forms the main page is a list view with that lists categories. 您好我正在开发使用xamarin表单构建的移动应用程序主页面是一个列表视图,其中列出了类别。 what I need to happen is when the user clicks a item in the list view it opens new page in the app heres the xaml code:
我需要发生的是当用户点击列表视图中的项目时,它会在应用程序中打开新页面,而下面是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" x:Class="SchoolTools.HomePage">
<ListView HasUnevenRows="true">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Frame Padding="0,0,0,8" BackgroundColor="#d2d5d7">
<Frame.Content>
<Frame Padding="15,15,15,15" OutlineColor="Gray" BackgroundColor="White">
<Frame.Content>
<StackLayout Padding="20,0,0,0" Orientation="Horizontal" HorizontalOptions="CenterAndExpand">
<Label x:Name="Internet" Text="Internet" HorizontalOptions="Center">
</Label>
<Label x:Name ="Math" Text="Math" HorizontalOptions="Center">
</Label>
<Label x:Name="Science" Text="Science" HorizontalOptions="Center">
</Label>
<Label x:Name ="Handwriting" Text="Handwriting" HorizontalOptions="Center">
</Label>
<Label x:Name ="FlashCards" Text="FlashCards" HorizontalOptions="Center">
</Label>
<Label x:Name="Books" Text="Books" HorizontalOptions="Center">
</Label>
</StackLayout>
</Frame.Content>
</Frame>
</Frame.Content>
</Frame>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage>
so my question is whats the code beind to make the math filed take you to the MathToolsHome class? 所以我的问题是什么代码beind使数学提交带你到MathToolsHome类? and so on and so forth?
等等等等?
any help would be amazing! 任何帮助都会很棒!
Thanks a million in advance! 提前一百万! :)
:)
Define ItemSelected for your Listview: 为Listview定义ItemSelected:
<ListView HasUnevenRows="true"
ItemSelected="OnItemSelected">
Then code the handler: 然后编写处理程序:
private void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
{
var item = e.SelectedItem;
// Your code here
}
You're probably looking for TapGestureRecognizer, which you'd set up in the constructor for SchoolTools.HomePage after the call to InitializeComponent(): 你可能正在寻找TapGestureRecognizer,你在调用InitializeComponent()之后在SchoolTools.HomePage的构造函数中设置了它:
var mathTapRecognizer = new TapGestureRecognizer();
mathTapRecognizer.Tapped += async delegate {
// Whatever you need to do here, like
await Navigation.PushAsync(new MathToolsHome());
};
Math.GestureRecognizers.Add(mathTapRecognizer);
Assuming that MathToolsHome is a Page of some sort. 假设MathToolsHome是某种类型的页面。
EDIT 编辑
Adding XAML solution... 添加XAML解决方案......
With the way that you have your XAML, the approach I outlined won't quite work, as you can't alter settings on members of DataTemplates that way (that's why the compiler is binding Math to System.Math instead of the Label in your layout). 通过你拥有XAML的方式,我概述的方法不会很有效,因为你无法改变DataTemplates成员的设置(这就是为什么编译器将Math绑定到System.Math而不是你的Label中布局)。
The easiest way to do this is to add the TapGestureRecognizer into your XAML, with a call to your code-behind. 最简单的方法是将TapGestureRecognizer添加到您的XAML中,同时调用您的代码隐藏。
XAML: XAML:
<Label x:Name ="Math" Text="Math" HorizontalOptions="Center">
<Label.GestureRecognizers>
<TapGestureRecognizer Tapped="Math_Clicked"/>
</Label.GestureRecognizers>
</Label>
And in your code behind: 在您的代码背后:
public async void Math_Clicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new MathToolsHome());
}
I've written more DataTemplate implementations with pure C# rather than XAML, but this approach ought to work. 我用纯C#而不是XAML编写了更多DataTemplate实现,但这种方法应该可行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.