【问题标题】:How to open another page when a listview item is selected in xamarin forms?在 xamarin 表单中选择列表视图项时如何打开另一个页面?
【发布时间】:2016-06-04 00:46:06
【问题描述】:

您好,我正在开发一个使用 xamarin 表单构建的移动应用程序,主页是一个列表视图,其中列出了类别。我需要发生的是,当用户单击列表视图中的项目时,它会在应用程序中打开新页面,这是 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>

所以我的问题是使数学文件带您进入 MathToolsHome 类的代码是什么?等等等等?

任何帮助都会很棒!

提前一百万谢谢! :)

【问题讨论】:

  • 列表中的每一行都有 6 个标签?还是您只想要一个包含 6 行且每行一个标签的列表?

标签: c# xaml xamarin xamarin.android xamarin.forms


【解决方案1】:

为您的 Listview 定义 ItemSelected:

 <ListView HasUnevenRows="true"
           ItemSelected="OnItemSelected">

然后对处理程序进行编码:

    private void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
    {
        var item = e.SelectedItem;
        // Your code here
    }

【讨论】:

    【解决方案2】:

    您可能正在寻找 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);
    

    假设 MathToolsHome 是某种页面。

    编辑

    添加 XAML 解决方案...

    以您拥有 XAML 的方式,我概述的方法将无法正常工作,因为您无法以这种方式更改 DataTemplates 成员的设置(这就是编译器将 Math 绑定到 System.Math 而不是在布局中添加标签)。

    执行此操作的最简单方法是将 TapGestureRecognizer 添加到您的 XAML 中,并调用您的代码隐藏。

    XAML:

    <Label x:Name ="Math" Text="Math" HorizontalOptions="Center">
      <Label.GestureRecognizers>
        <TapGestureRecognizer Tapped="Math_Clicked"/>
      </Label.GestureRecognizers>
    </Label>
    

    在你的代码后面:

    public async void Math_Clicked(object sender, EventArgs e)
    {
        await Navigation.PushAsync(new MathToolsHome());
    }
    

    我已经用纯 C# 而不是 XAML 编写了更多的 DataTemplate 实现,但是这种方法应该可以工作。

    【讨论】:

    • 感谢您的回复 我在尝试您的代码时收到此错误:/Users/JailBrak0e/Projects/SchoolTools/SchoolTools/HomePage.xaml.cs(9,9): Error CS0117: System.Math' does not contain a definition for GestureRecognizers' (CS0117) (SchoolTools)
    • 查看更新后的答案,它应该更适合使用 XAML 定义的 DataTemplate。
    • 我认为没有必要搞得那么复杂,Christine 的回答应该没问题。
    猜你喜欢
    • 2016-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多