如何在列表视图中通过单击项目打开另一个 xamarin 表单页面 [英] How to open another xamarin forms page form a item-click in a list view

查看:27
本文介绍了如何在列表视图中通过单击项目打开另一个 xamarin 表单页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有一个在 Xamarin 表单中创建的列表视图,我想要它做的就是当用户单击列表视图中的一个选项时,它会将它们带到另一个 Xamarin 表单页面,在我的情况下,它将是 ContactInfo

这是我的 xaml:

<StackLayout Padding="0,20,0,0"><Label Text="ReadyMo" FontAttributes="Bold" Horizo​​ntalOptions="Center"/><ListView x:Name="listView"><ListView.ItemTemplate><数据模板><ViewCell><网格><Grid.ColumnDefinitions><ColumnDefinition Width="0.5*"/><ColumnDefinition Width="0.2*"/><ColumnDefinition Width="0.3*"/></Grid.ColumnDefinitions><Label Text="{Binding Name}" FontAttributes="Bold" Horizo​​ntalOptions="Center"></标签></网格></ViewCell></数据模板></ListView.ItemTemplate></ListView></StackLayout></内容页>

这是我的代码:

使用网络搜索;使用系统;使用 System.Collections.Generic;使用 System.Linq;使用 System.Text;使用 System.Threading.Tasks;使用 Xamarin.Forms;命名空间网络搜索{公共部分类 CountySelect : ContentPage{公共县选择(){初始化组件();var name = new List{新县(亚岱尔"),新县(安德鲁"),新县(艾奇逊"),新县(Audrain"),新县(巴里"),新县(巴顿"),新县(贝茨"),新县(本顿"),新县(布林格"),新县(布恩"),新县(布坎南"),新县(管家"),新县(考德威尔"),新县(卡拉威"),新县(卡姆登"),新县(开普吉拉多"),新县(卡罗尔"),新县(卡特"),新县(卡斯"),新县(雪松"),新县(Chariton"),新县(基督教"),新县(克拉克"),新县(粘土"),新县(克林顿"),新县(科尔"),新县(库珀"),新县(克劳福德"),新县(大德"),新县(达拉斯"),新县(戴维斯"),新县(DeKalb"),新县(凹痕"),新县(道格拉斯"),新县(邓克林"),新县(富兰克林"),新县(Gasconade"),新县(绅士"),新县(格林"),新县(Grundy"),新县(哈里森"),新县(亨利"),新县(山核桃"),新县(霍尔特"),新县(霍华德"),新县(豪厄尔"),新县(铁"),新县(杰克逊"),新县(贾斯珀"),新县(杰斐逊"),新县(约翰逊"),新县(诺克斯"),新县(拉克莱德"),新县(拉斐特"),新县(劳伦斯"),新县(刘易斯"),新县(林肯"),新县(林恩"),新县(利文斯顿"),新县(梅肯"),新县(麦迪逊"),新县(玛丽亚"),新县(马里昂"),新县(麦当劳"),新县(美世"),新县(米勒"),新县(密西西比州"),新县(Moniteau"),新县(梦露"),新县(蒙哥马利"),新县(摩根"),新县(新马德里"),新县(牛顿"),新县(Nodaway"),新县(俄勒冈"),新县(奥沙"),新县(欧扎克"),新县(佩米斯科特"),新县(佩里"),新县(佩蒂斯"),新县(菲尔普斯"),新县(派克"),新县(普拉特"),新县(波尔克"),新县(普拉斯基"),新县(普特南"),新县(拉尔斯"),新县(兰道夫"),新县(雷"),新县(雷诺兹"),新县(里普利"),新县(盐水"),新县(斯凯勒"),新县(苏格兰"),新县(斯科特"),新县(香农"),新县(谢尔比"),新县(圣查尔斯"),新县(圣克莱尔"),新县(圣弗朗索瓦"),新县(圣路易斯市"),新县(圣路易斯县"),新县(Ste. Genevieve"),新县(斯托达德"),新县(石头"),新县(沙利文"),新县(塔尼"),新县(德克萨斯州"),新县(弗农"),新县(沃伦"),新县(华盛顿"),新县(韦恩"),新县(韦伯斯特"),新县(值得"),新县(赖特")};listView.ItemsSource = 名称;listView.ItemTapped += async (sender, args) =>{var item = args.Item as County;if (item == null) 返回;等待 Navigation.PushAsync(new ContactInfo(item));listView.SelectedItem = null;};内容 = 列表视图;}}}

我对 Xamarin Forms 非常陌生,所以任何帮助都会很棒:)提前致谢!

解决方案

所以我发现我的 app.cs 中的代码是:公共类应用程序:

应用{公共应用程序(){MainPage = new MyFirstPage();}}

什么时候需要:

public class App : 应用{公共应用程序(){MainPage = new NavigationPage(new MyFirstPage());}}

多一点研究大有帮助!:)

Hello I have a List View that I made in Xamarin forms and all I want it to do is when the user clicks a option in the List View it take them to another Xamarin forms page in my case it would be ContactInfo

heres my 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="WebSearch.CountySelect" Title="ReadyMo">
  <StackLayout Padding="0,20,0,0">
   <Label Text="ReadyMo" FontAttributes="Bold" HorizontalOptions="Center" />
    <ListView x:Name="listView">
      <ListView.ItemTemplate>
        <DataTemplate>
          <ViewCell>
            <Grid>
              <Grid.ColumnDefinitions>
                <ColumnDefinition Width="0.5*" />
                <ColumnDefinition Width="0.2*" />
                <ColumnDefinition Width="0.3*" />
              </Grid.ColumnDefinitions>
              <Label Text="{Binding Name}" FontAttributes="Bold" HorizontalOptions="Center">
              </Label>
            </Grid>
          </ViewCell>
        </DataTemplate>
      </ListView.ItemTemplate>
    </ListView>
  </StackLayout>
</ContentPage>

here's my code behind:

using WebSearch;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;

namespace WebSearch
{
    public partial class CountySelect : ContentPage
    {

        public CountySelect()
        {
            InitializeComponent();


            var name = new List<County>
            {
                      new County("Adair"),
                      new County("Andrew"),
                      new County("Atchison"),
                      new County("Audrain"),
                      new County("Barry"),
                      new County("Barton"),
                      new County("Bates"),
                      new County("Benton"),
                      new County("Bollinger"),
                      new County("Boone"),
                      new County("Buchanan"),
                      new County("Butler"),
                      new County("Caldwell"),
                      new County("Callaway"),
                      new County("Camden"),
                      new County("Cape Girardeau"),
                      new County("Carroll"),
                      new County("Carter"),
                      new County("Cass"),
                      new County("Cedar"),
                      new County("Chariton"),
                      new County("Christian"),
                      new County("Clark"),
                      new County("Clay"),
                      new County("Clinton"),
                      new County("Cole"),
                      new County("Cooper"),
                      new County("Crawford"),
                      new County("Dade"),
                      new County("Dallas"),
                      new County("Daviess"),
                      new County("DeKalb"),
                      new County("Dent"),
                      new County("Douglas"),
                      new County("Dunklin"),
                      new County("Franklin"),
                      new County("Gasconade"),
                      new County("Gentry"),
                      new County("Greene"),
                      new County("Grundy"),
                      new County("Harrison"),
                      new County("Henry"),
                      new County("Hickory"),
                      new County("Holt"),
                      new County("Howard"),
                      new County("Howell"),
                      new County("Iron"),
                      new County("Jackson"),
                      new County("Jasper"),
                      new County("Jefferson"),
                      new County("Johnson"),
                      new County("Knox"),
                      new County("Laclede"),
                      new County("Lafayette"),
                      new County("Lawrence"),
                      new County("Lewis"),
                      new County("Lincoln"),
                      new County("Linn"),
                      new County("Livingston"),
                      new County("Macon"),
                      new County("Madison"),
                      new County("Maries"),
                      new County("Marion"),
                      new County("McDonald"),
                      new County("Mercer"),
                      new County("Miller"),
                      new County("Mississippi"),
                      new County("Moniteau"),
                      new County("Monroe"),
                      new County("Montgomery"),
                      new County("Morgan"),
                      new County("New Madrid"),
                      new County("Newton"),
                      new County("Nodaway"),
                      new County("Oregon"),
                      new County("Osage"),
                      new County("Ozark"),
                      new County("Pemiscot"),
                      new County("Perry"),
                      new County("Pettis"),
                      new County("Phelps"),
                      new County("Pike"),
                      new County("Platte"),
                      new County("Polk"),
                      new County("Pulaski"),
                      new County("Putnam"),
                      new County("Ralls"),
                      new County("Randolph"),
                      new County("Ray"),
                      new County("Reynolds"),
                      new County("Ripley"),
                      new County("Saline"),
                      new County("Schuyler"),
                      new County("Scotland"),
                      new County("Scott"),
                      new County("Shannon"),
                      new County("Shelby"),
                      new County("St. Charles"),
                      new County("St. Clair"),
                      new County("St. Francois"),
                      new County("St. Louis City"),
                      new County("St. Louis County"),
                      new County("Ste. Genevieve"),
                      new County("Stoddard"),
                      new County("Stone"),
                      new County("Sullivan"),
                      new County("Taney"),
                      new County("Texas"),
                      new County("Vernon"),
                      new County("Warren"),
                      new County("Washington"),
                      new County("Wayne"),
                      new County("Webster"),
                      new County("Worth"),
                      new County("Wright")
            };

            listView.ItemsSource = name;
            listView.ItemTapped += async (sender, args) =>
            {
                var item = args.Item as County;
                if (item == null) return;
                await Navigation.PushAsync(new ContactInfo(item));
                listView.SelectedItem = null;
            };

            Content = listView;


        }


    }
}

I very new to Xamarin Forms so any help would be amazing :) Thanks in advance!

解决方案

so I figured it out my code in my app.cs was: public class App :

Application

    {
        public App()
        {
            MainPage = new MyFirstPage();
        }
    }

when it needed to be:

public class App : Application
{
    public App()
    {
        MainPage = new NavigationPage(new MyFirstPage());
    }
}

A little more research went a long way! :)

这篇关于如何在列表视图中通过单击项目打开另一个 xamarin 表单页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆