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

科兹巫师

鉴于以下内容ListView,我想要一个命令将单击的对象(在本例中为地址)发送回视图模型中的命令 -SelectNewAddressDeleteAddress.

            <StackLayout VerticalOptions="FillAndExpand" Padding="10,15,10,15">
                <Label Text="Addresses" FontSize="22" HorizontalTextAlignment="Center" FontAttributes="Bold" Padding="0,0,0,7" TextColor="#404040" />
                <StackLayout VerticalOptions="FillAndExpand">
                    <flv:FlowListView FlowColumnCount="1"
                                        HeightRequest="200"
                                        SeparatorVisibility="None"
                                        HasUnevenRows="True"
                                        FlowItemsSource="{Binding AllAddresses}">
                        <flv:FlowListView.FlowColumnTemplate>
                            <DataTemplate x:DataType="popups:AddressItem">
                                <Grid ColumnDefinitions="*,35" Padding="0,0,0,15" x:Name="Item">
                                    <Grid Grid.Column="0">
                                        <Grid.GestureRecognizers>
                                            <TapGestureRecognizer 
                                                Command="{Binding SelectNewAddress}" />
                                        </Grid.GestureRecognizers>
                                        <Label Text="{Binding MainAddress}" 
                                                LineBreakMode="TailTruncation" 
                                                HorizontalTextAlignment="Start" 
                                                VerticalTextAlignment="Center"
                                                FontSize="18"
                                                TextColor="{StaticResource CommonBlack}"/>
                                    </Grid>
                                    <Grid Grid.Column="1" IsVisible="{Binding IsSelected}"  >
                                        <Grid.GestureRecognizers>
                                            <TapGestureRecognizer 
                                                Command="{Binding SelectNewAddress}"/>
                                        </Grid.GestureRecognizers>
                                        <StackLayout Padding="10,0,0,0">
                                            <flex:FlexButton Icon="check.png" 
                                                                WidthRequest="25" 
                                                                HeightRequest="25"
                                                                CornerRadius="18"
                                                                BackgroundColor="{StaticResource Primary}" 
                                                                ForegroundColor="{StaticResource CommonWhite}" 
                                                                HighlightBackgroundColor="{StaticResource PrimaryDark}"
                                                                HighlightForegroundColor="{StaticResource CommonWhite}"/>
                                        </StackLayout>
                                    </Grid>
                                    <Grid Grid.Column="1" IsVisible="{Binding IsSelected, Converter={StaticResource invertBoolConverter}}">
                                        <Grid.GestureRecognizers>
                                            <TapGestureRecognizer Command="{Binding DeleteAddress} />
                                        </Grid.GestureRecognizers>
                                        <StackLayout Padding="10,0,0,0">
                                            <flex:FlexButton Icon="deleteCard.png" 
                                                                WidthRequest="25" 
                                                                HeightRequest="25"
                                                                CornerRadius="18"
                                                                BackgroundColor="{StaticResource WooopDarkGray}" 
                                                                ForegroundColor="{StaticResource CommonWhite}" 
                                                                HighlightBackgroundColor="{StaticResource PrimaryDark}"
                                                                HighlightForegroundColor="{StaticResource CommonWhite}"/>
                                        </StackLayout>
                                    </Grid>
                                </Grid>
                            </DataTemplate>
                        </flv:FlowListView.FlowColumnTemplate>
                    </flv:FlowListView>
                </StackLayout>

视图模型中的命令如下:

        ...

        public ICommand SelectNewAddress { get; set; }
        public ICommand DeleteAddress { get; set; }

        ...

        public AddressSelectionViewModel()
        {
            DeleteAddress = new Command(DeleteAddressCommand);
            SelectNewAddress = new Command(SelectNewAddressCommand);
        }
        
        ...
        private void SelectNewAddressCommand(object obj)
        {
            try
            {
                var item = (AddressItem)obj;
                AddressHelper.UpdateAddress(item.DeliveryAddressLocation);
                UpdateAddresses();
            }
            catch (Exception ex)
            {
                // TODO
            }
        }

        private void DeleteAddressCommand(object obj)
        {
            try
            {
                var item = (AddressItem)obj;
                AddressHelper.RemoveAddress(item.DeliveryAddressLocation);
                UpdateAddresses();
            }
            catch (Exception ex)
            {
                // TODO
            }
        }

我希望object obj传递给SelectNewAddressCommandDeleteAddressCommand成为点击的地址ListView

科兹巫师

首先确保您已将您的视图模型包含DataTypeClass以下内容中ContentPage

xmlns:pages="clr-namespace:your.namespace.ViewModels"
x:DataType="pages:AddressSelectionViewModel"
x:Class="your.namespace.Views.AddressSelectionPage"
<ContentPage xmlns="..."
             xmlns:x="..." 
             xmlns:flv="..." 
             xmlns:popups="..." 
             xmlns:flex="..." 
             xmlns:views="..." 
             xmlns:xct="..." 
             xmlns:pages="clr-namespace:your.namespace.ViewModels"
             x:DataType="pages:AddressSelectionViewModel"
             x:Class="your.namespace.Views.AddressSelectionPage"
             Shell.FlyoutItemIsVisible="..."
             Shell.NavBarIsVisible="..."
             Shell.TabBarIsVisible="...">

在顶部Grid元素内部添加属性x:Name="Item"(“Item”仅用作示例,您可以将其命名为任何名称):

                    <flv:FlowListView FlowColumnCount="1"
                                        HeightRequest="200"
                                        SeparatorVisibility="None"
                                        HasUnevenRows="True"
                                        FlowItemsSource="{Binding AllAddresses}">
                        <flv:FlowListView.FlowColumnTemplate>
                            <DataTemplate x:DataType="popups:AddressItem">
                                <Grid ColumnDefinitions="*,35" Padding="0,0,0,15" x:Name="Item"> <!-- Here -->

然后我们将Commandand更改CommandParameterTapGestureRecognizer以下内容:

<TapGestureRecognizer 
        Command="{Binding Path=SelectNewAddress, Source={RelativeSource AncestorType={x:Type pages:AddressSelectionViewModel}}}" 
        CommandParameter="{Binding Source={x:Reference Item}, Path=BindingContext}" />
<TapGestureRecognizer 
        Command="{Binding Path=DeleteAddress, Source={RelativeSource AncestorType={x:Type pages:AddressSelectionViewModel}}}" 
        CommandParameter="{Binding Source={x:Reference Item}, Path=BindingContext}" />

Command我们将函数指定为Path,然后我们通过 阐明这个函数的来源是在视图模型内部AncestoryType当在列表视图中时,我们不能引用被迭代对象之外的属性。因此,我们需要指定所需的来源。

所以现在我们正在引用实际的函数。但是我们还没有发送object obj作为参数。

在 中,我们必须用CommandParameter传递当前绑定的对象请注意,在 Source 中,我们引用的名称是我们之前定义PathSourceItemx:NameGrid

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Xamarin.Forms:StackLayout中的ListView:如何设置高度?

Xamarin Forms ListView数据绑定

Xamarin.Forms JSON对象进入Listview

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

Xamarin.Forms中的水平Listview

在Xamarin Forms ListView中停止细胞回收

xamarin.forms ListView ItemSelected

如何将ListView.ItemTapped事件绑定到Xamarin Forms中的ViewModel命令?

如何在Xamarin Forms中将BackgroundImage添加到ListView?

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

Xamarin.Forms。优化ListView

如何在Xamarin.Forms中的ListView内绑定列表

XAML中的Xamarin.Forms ListView AutoHeight

从按钮命令xamarin.forms MVVM获取ListView

更新ObservableCollection Xamarin.Forms ListView之后发送空事件

ListView中的Xamarin Forms命令绑定不起作用

Xamarin Forms:如何在ListView内绑定Xamarin.forms.Maps的位置值?

Xamarin.Forms ListView,如何设置RowHeight?

在Xamarin.Forms中搜索分组的ListView

Xamarin.Forms禁用ListView中的项目

在Tableview xamarin.forms中创建Listview

创建列表后,如何更新Xamarin Forms ListView的ViewCell属性?

在Xamarin.Forms中刷新UWP的ListView

如何在xamarin.forms中获得ListView的子级?

如何更改 Xamarin Forms 中 `ListView` 内 `TextCell` 的背景颜色?

Xamarin Forms - 如何根据您输入的数据填充 ListView?

Xamarin Forms - 如何在 xaml 中创建水平 ListView?

Xamarin Forms Listview 中的 TapGestureRecognition

无法在 ListView Xamarin Forms 中显示数据