xaml - 当我们通过资源将 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(如果多次使用),那么我该如何设置命令呢?
解决方案
对不起,误导性链接,我刚刚了解您的问题。
你可以使用 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/
推荐阅读
- azure-devops - 通过 azure devops 管道上传 .pfx 证书
- reactjs - 在 opendsitro 中为 elasticsearch 启用 cors
- typescript - 有没有办法使用带有 TypeORM 和 TypeScripts 的动态环境变量来解决或绕过类型错误?
- python - 选择列表列表的特定元素
- javascript - Chrome 开发工具扩展无法通过 inspectedWindow.eval 获得承诺
- css - 背景位置不适用于 img 类
- python - snapcraft 给出 'ascii' 编解码器无法解码位置 0 中的字节 0xd0:序数不在范围内(128)
- scala - scala数据框中的collect_list,它将以固定列号的间隔收集行
- sql - SQL - 如何使用某些表将用户作为新用户导入
- javascript - 两个 div 合二为一不处理鼠标事件