简体   繁体   English

如何在xamarin表单中选择listview项目时打开另一个页面?

[英]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.

相关问题 如何更改listview所选项目的文本颜色Xamarin.forms - How to change the listview selected item text color Xamarin.forms 如何从 xamarin forms 中的列表视图中获取所选项目索引? - How to get the selected item index from the listview in xamarin forms? 编辑Xamarin Forms中ListView的所选项的标签 - Edit the Label of the selected item of the ListView in Xamarin Forms 如何在列表视图中通过单击项目打开另一个 xamarin 表单页面 - How to open another xamarin forms page form a item-click in a list view 如何通过单击数据模板列表视图中的项目来打开另一个xamarin表单页面? - How to open another xamarin forms page from clicking a item in a data template list view? Xamarin Forms - 单击图像时如何选择 ListView 项? - Xamarin Forms - How to select a ListView item when its image is clicked? 如何从另一个页面访问 ListView?(Xamarin Forms) - How to access a ListView from another page?(Xamarin Forms) Xamarin Forms 单击列表视图上的菜单时如何打开弹出窗口 - Xamarin Forms how to open a popup when you clicked a menu on the listview 如何通过单击xamarin形式的数据模型中的项目将其推到另一页? - how to push to another page by clicking on a item in a data model in xamarin forms? 如何从Xamarin Forms ListView中删除项目? - How to remove an item from a Xamarin Forms ListView?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM