Xamarin.Forms:当单击ListView中的项目时,如何显示模态?

杰西·埃文格里斯塔

今天是个好日子。我目前正在Xamarin.Forms中做一个简单的应用程序,使我可以CRUD员工的记录。创建的记录显示在ListView上。这是我的截图。

在此处输入图片说明

我想做的就是每当我单击ListView上的一个项目时,它将显示一个模版,其中包含雇员的更多详细信息,例如(生日,地址,性别,工作经历)。我怎样才能做到这一点?那有可能吗?你能告诉我怎么做吗?

这是我的代码,显示ListView。

<?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="XamarinFormsDemo.EmployeeRecordsPage"
         xmlns:ViewModels="clr-namespace:XamarinFormsDemo.ViewModels;assembly=XamarinFormsDemo"
         xmlns:controls="clr-namespace:ImageCircle.Forms.Plugin.Abstractions;assembly=ImageCircle.Forms.Plugin.Abstractions"
         BackgroundImage="bg3.jpg"
         Title="List of Employees">


  <ContentPage.BindingContext>
    <ViewModels:MainViewModel/>
  </ContentPage.BindingContext>

  <StackLayout Orientation="Vertical">



  <ListView ItemsSource="{Binding EmployeesList, Mode=TwoWay}"
        HasUnevenRows="True">
    <ListView.ItemTemplate>
      <DataTemplate>
        <ViewCell>
          <Grid Padding="10" RowSpacing="10" ColumnSpacing="5">
            <Grid.RowDefinitions>
              <RowDefinition Height="Auto"/>
              <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
              <ColumnDefinition Width="Auto"/>
              <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>

        <controls:CircleImage Source="icon.png"
               HeightRequest="66"
               HorizontalOptions="CenterAndExpand"
               Aspect="AspectFill"
               WidthRequest="66"
               Grid.RowSpan="2"
               />


        <Label Grid.Column="1"
          Text="{Binding Name}"
               TextColor="#24e97d"
               FontSize="24"/>

        <Label Grid.Column="1"
               Grid.Row="1"
              Text="{Binding Department}"
               TextColor="White"
               FontSize="18"
               Opacity="0.6"/>


           </ViewCell>
      </DataTemplate>
    </ListView.ItemTemplate>

  </ListView>


   <StackLayout Orientation="Vertical"
         Padding="30,10,30,10"
         HeightRequest="20"
         BackgroundColor="#24e97d"
         VerticalOptions="Center"
         Opacity="0.5">
  <Label Text="© Copyright 2015   smesoft.com.ph   All Rights Reserved "
         HorizontalTextAlignment="Center"
         VerticalOptions="Center"
         HorizontalOptions="Center" />
    </StackLayout>
  </StackLayout>

</ContentPage>

注意:显示的记录在ASP.NET Web应用程序中创建,并且仅显示在UWP中的ListView上。如果您需要查看更多代码,请告诉我。

非常感谢你们。

阿图尔·哈里库玛(Athul Harikumar)

要将命令绑定到项目选择的属性,请参见下面的示例,否则ItemSelected将仅绑定到模型属性

有关完整示例,请参见https://github.com/TheRealAdamKemp/Xamarin.Forms-Tests/blob/master/RssTest/View/Pages/MainPage.xaml.cs

现在,您可以绑定一个Icommand,它可能具有类似

 private Command login;
        public ICommand Login
        {
            get
            {
                login = login ?? new Command(DoLogin);
                return login;
            }
        }

private async void DoLogin()
        {

            await Navigation.PopModalAsync(new MySampXamlPage());
            //await DisplayAlert("Hai", "thats r8", "ok");

        }

并查看:

[Navigation.RegisterViewModel(typeof(RssTest.ViewModel.Pages.MainPageViewModel))]
    public partial class MainPage : ContentPage
    {
        public const string ItemSelectedCommandPropertyName = "ItemSelectedCommand"; 
        public static BindableProperty ItemSelectedCommandProperty = BindableProperty.Create(
            propertyName: "ItemSelectedCommand",
            returnType: typeof(ICommand),
            declaringType: typeof(MainPage),
            defaultValue: null);

        public ICommand ItemSelectedCommand
        {
            get { return (ICommand)GetValue(ItemSelectedCommandProperty); }
            set { SetValue(ItemSelectedCommandProperty, value); }
        }

        public MainPage ()
        {
            InitializeComponent();
        }

        protected override void OnBindingContextChanged()
        {
            base.OnBindingContextChanged();

            RemoveBinding(ItemSelectedCommandProperty);
            SetBinding(ItemSelectedCommandProperty, new Binding(ItemSelectedCommandPropertyName));
        }

        protected override void OnAppearing()
        {
            base.OnAppearing();

            _listView.SelectedItem = null;
        }

        private void HandleItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            if (e.SelectedItem == null)
            {
                return;
            }

            var command = ItemSelectedCommand;
            if (command != null && command.CanExecute(e.SelectedItem))
            {
                command.Execute(e.SelectedItem);
            }
        }
    }

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:ValueConverters="clr-namespace:RssTest.ValueConverters;assembly=RssTest"
    x:Class="RssTest.View.Pages.MainPage"
    Title="{Binding Title}">
    <ContentPage.Resources>
        <ResourceDictionary>
            <ValueConverters:BooleanNegationConverter x:Key="not" />
        </ResourceDictionary>
    </ContentPage.Resources>
    <Grid VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
        <ListView x:Name="_listView"
            IsVisible="{Binding IsLoading, Converter={StaticResource not}" ItemsSource="{Binding Items}"
            ItemSelected="HandleItemSelected"
            VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextCell Text="{Binding Title}" />
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
        <ActivityIndicator IsVisible="{Binding IsLoading}" IsRunning="{Binding IsLoading}"
            VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" />
    </Grid>
</ContentPage>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Xamarin.Forms中禁用ListView上的突出显示

如何从Xamarin Forms ListView中删除项目?

Xamarin.Forms中滚动时项目的放大

如何将ListView设置为开始在Xamarin表单中显示最后一个项目?

如何在Xamarin.forms xaml中水平显示ListView中的项目?

通过单击ListView(Xamarin.Forms)中的按钮获取当前项目

Xamarin Forms-如何在ListView中显示/绑定列表?

Xamarin.forms如何在列表中彼此相邻显示项目

Xamarin Android单击ListView中的按钮并获取项目位置

Xamarin Forms ListView不显示内容

如何在Xamarin.Forms中基于ListView滚动方向显示和隐藏StackLayout?

如何在Xamarin Forms应用程序(android)中仅激活1个ListView项目并禁用其他项目

如何在Xamarin Forms中以2列和n行显示单个listview项目

Xamarin Forms MVVM在CollectionView中单击一行时显示图标/图像

在UWP中,以Flex布局显示Xamarin.forms的选定项目

Xamarin.Forms禁用ListView中的项目

Xamarin.Forms ListView中项目之间的填充

Xamarin.Forms(iOS 项目):ListView 分隔符显示在所有屏幕中

Xamarin.Forms 在 ListView 中显示文本而不是 typename

Xamarin Forms ListView 文本未显示

Xamarin Forms (Xaml) ListView 不显示图像

如何在 Xamarin Forms 中的其他项目之上(之上)显示 ListView?

Xamarin Forms Listview 中的 TapGestureRecognition

单击 Xamarin.Forms 中的 ListView 项后完整显示详细信息

Xamarin.Forms:显示 ToolbarItems 时如何在 TitleView 中居中显示图像?

无法在 ListView Xamarin Forms 中显示数据

如何在 Xamarin Forms 中一一显示 Collection View 的项目?

如何在 Xamarin Forms 中使用 MVVM 在 ListView 中实现增量加载(ItemAppearing)项目?

在 ListView 如何将单击的对象发送回视图模型中的命令 - Xamarin Forms