首页 > 解决方案 > 当我们通过资源将 ListView 与 DataTemplate 一起使用时,绑定不起作用。(MVVM)

问题描述

我有一个从资源文件ListView中获取的。ItemTemplate

我有一个资源字典如下!

<?xml version="1.0" encoding="utf-8" ?>
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:ffimageloading="clr-namespace:FFImageLoading.Forms;assembly=FFImageLoading.Forms"
         xmlns:common="clr-namespace:xx.Common;assembly=xx"
         x:Class="xx.ResourceDictionaries.BaseHomeStyles"> 

 <DataTemplate x:Key="ListItemTemplate">
    <ViewCell>
        <Grid Padding="{StaticResource ListPadding}">
            <Grid.RowDefinitions>
                <RowDefinition Height="1.5*"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <ffimageloading:CachedImage Grid.Row="0" Aspect="Fill" DownsampleToViewSize="True" BitmapOptimizations="True"
                                            ErrorPlaceholder = "nopreviewlandscape" LoadingPlaceholder = "loadingicon" Source="{Binding TripImage}" />
            <StackLayout Grid.Row="1">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="4*"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Label Grid.Column="0" Text="{Binding TripName}" FontAttributes="Bold" TextColor="{StaticResource PrimaryColor}" />
                    <Image Grid.Column="1" Source="downarrow" Rotation="-90" BackgroundColor="{Binding PrimaryColor}"/>
                </Grid>
                <Label Text="{Binding ReferenceNumber}" FontAttributes="Bold"/>
                <Label Text="{Binding TripUIStartDate}" />
                <Label Text="{Binding DaysDetails}" TextColor="{StaticResource AppQuaternaryBackground}" />
            </StackLayout>
            <!--<Grid.GestureRecognizers>
                    <TapGestureRecognizer Command="{Binding Path=BindingContext.ToItineraryCommand, Source={x:Reference HomeList}}" CommandParameter="{Binding .}"/>
                </Grid.GestureRecognizers>-->
        </Grid>
    </ViewCell>
</DataTemplate>
</ResourceDictionary>

然后我用它DataTemplate作为这样的ItemTemplate东西:

<ListView HasUnevenRows = "true" ItemsSource="{Binding CurrentTripInfo}" BackgroundColor="Transparent"
       CachingStrategy="RecycleElement" x:Name="HomeList" SeparatorVisibility ="Default" Grid.Row="1" ItemTemplate="{StaticResource ListItemTemplate}" />

我正在添加这个 ResourceDictionary 是这样的:

 <ContentPage.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <resources:BaseHomeStyles/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</ContentPage.Resources>

现在我在这里尝试做的是Command在我的 中设置一个 Click ViewModel,通常,我是如何做到这一点的,ListView就像我在上面的示例中那样设置一个名称,然后使用这个名称来获取列表BindingContext,然后使用它来设置命令,但是当我现在尝试使用它时,它会引发 XML 解析器异常:

Xamarin.Forms.Xaml.XamlParseException:位置 38:43。找不到引用的对象HomeList

现在我有两个问题:

1- 以上述方式为 ListView 设置命令的正确方法是什么?

2-如果将来我打算将其移至 App.XAML(如果多次使用),那么我该如何设置命令呢?

标签: xamlxamarinxamarin.formsxamarin.androidxamarin.ios

解决方案


对不起,误导性链接,我刚刚了解您的问题。

你可以使用 ListView Behaviors 来解决你的问题,它比在后面的代码上绑定命令更花哨和更容易阅读。

您可以按照下面的链接查看它是如何完成的,稍后将所有行为转换为仅一个行为并将事件名称传递给它,GetRuntimeEvent(eventName)AddEventHandler()在最后一个方法返回的对象上查找事件。

最后,您的列表视图将如下所示:

<ListView HasUnevenRows = "true" ItemsSource="{Binding CurrentTripInfo}" BackgroundColor="Transparent"
       CachingStrategy="RecycleElement" x:Name="HomeList" SeparatorVisibility ="Default" Grid.Row="1" ItemTemplate="{StaticResource ListItemTemplate}" >
    <ListView.Behaviors>
        <behaviors:EventToCommand EventName="ItemTapped" Command="{Binding ClickCommand}"/>
    </ListView.Behaviors>
</ListView>

https://devblogs.microsoft.com/xamarin/turn-events-into-commands-behaviors/


推荐阅读