【问题标题】:How to open another xamarin forms page form a item-click in a list view如何从列表视图中的项目单击打开另一个 xamarin 表单页面
【发布时间】:2016-09-05 13:45:46
【问题描述】:

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

这是我的 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>

这是我的代码:

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;


        }


    }
}

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

【问题讨论】:

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


    【解决方案1】:

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

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

    何时需要:

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

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

    【讨论】:

    • 如何在堆栈上推送页面是“我如何从列表视图点击推送页面”的答案
    【解决方案2】:

    应用程序类

    public class App: Application
    {
        public static NavigationPage MyNavigationPage;
    
        public App()
        {
            MyNavigationPage = new NavigationPage();
            MainPage = MyNavigation;
            MyNavigation.PushAsync(new Page_Countries, true);
        }
    }
    

    单击 BindableProperty 项的自定义 ListView

     public class MyListView : ListView
        {
    
    
            public MyListView()
            {
                this.ItemTapped += this.OnItemTapped;
    
            }
    
            public static BindableProperty ItemClickCommandProperty = BindableProperty.Create<MyListView, ICommand>(x => x.ItemClickCommand, null);
    
            public ICommand ItemClickCommand {
                get { return (ICommand)this.GetValue(ItemClickCommandProperty); }
                set { this.SetValue(ItemClickCommandProperty, value); }
            }
    
    
            private void OnItemTapped(object sender, ItemTappedEventArgs e) {
                if (e.Item != null && this.ItemClickCommand != null && this.ItemClickCommand.CanExecute(e)) {
                    this.ItemClickCommand.Execute(e.Item);
                    this.SelectedItem = null;
                }
            }
    
        }
    

    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:customcontrols="clr-namespace:MyNameSpace.CustomControls;assembly=MyNameSpace"
                 x:Class="MyNameSpace.Views.Page_Countries"
                 Title="Countries">
    
    <customcontrols:MyListView
                    x:Name="listOfCountries"
                    VerticalOptions="Fill"
                    HorizontalOptions="Fill"
                    ItemsSource="{Binding PropertyCountries}"
                    SelectedItem="{Binding PropertySelectedItem}"
                    ItemClickCommand="{Binding CountryItemClickCommand}" <---
     <ListView.ItemTemplate>
                <DataTemplate>
                  <ViewCell>
                    <ViewCell.View>
                      <StackLayout
                          Orientation="Horizontal"
                          HorizontalOptions="Fill"
                          VerticalOptions="Center"
                          Padding="5,0,5,0"
                          Spacing="10">
    <Label
    
                              HorizontalOptions="FillAndExpand"
                           LineBreakMode="TailTruncation"
                              VerticalOptions="Center"
                              Text="{Binding CountryName}" />
    </StackLayout>
                    </ViewCell.View>
                  </ViewCell>
                </DataTemplate>
              </ListView.ItemTemplate>
            </customcontrols:MyListView>
    

    视图模型

    class ViewModel_Countries
    {
        private ObservableCollection<Models.Country> Countries;
        public ObservableCollection<Models.Country> PropertyCountries
        {
                set { SetProperty(ref Countries, value); }
                get { return Countries; }
        }
    
        private ICommand countryItemClickCommand;
        public ICommand CountryItemClickCommand;
        {
            get
            {
                return countryItemClickCommand ?? (countryItemClickCommand = new Command (async () => await ExecuteCountryClickCommand()));
            }
        }
    
    private async Task ExecuteCountryClickCommand()
    {
        App.MyNavigation.PushAsync(MyNewPage, true);
    }
    

    }

    【讨论】:

    • 此处和另一个 Stack Overflow 页面上的所有示例都讨论了 Navigation 或 MyNavigation,但该变量不存在,在这种情况下它不是 App 的字段,您的代码甚至没有编译并且没有cmets = 没有帮助
    • 您可能想尝试查看导航页面在 Xamarin 中的工作方式,而不是尝试复制粘贴不打算编译的示例代码。 docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/…
    • 这个例子已经有 5 年历史了,可能不再是这个问题的最佳来源了。
    • 不尝试仅使用点运算符复制粘贴 @ClintL 并且不存在任何变量,此帖子已被弃用,应在此处明确说明。谢谢老兄的链接。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多