【问题标题】:Binding not working when we use ListView with DataTemplate through Resources.(MVVM)当我们通过资源将 ListView 与 DataTemplate 一起使用时,绑定不起作用。(MVVM)
【发布时间】:2019-04-22 21:30:33
【问题描述】:

我有一个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>

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

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

现在我有两个问题:

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

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

【问题讨论】:

  • 您可以查看这篇文章以供参考:stackoverflow.com/questions/29776976/…
  • 您如何在资源字典中使用 DataTemplate 而不是自定义视单元?无论如何,这可能是一个有趣的阅读:forums.xamarin.com/discussion/96459/…
  • @Knoop 谢谢,但我更喜欢行为解决方案
  • @G.hakim 没问题,只是想一些替代方案。我明白在这种情况下,这是在更清洁但特定情况下与更通用/可扩展但不太干净之间的决定。这是一个因项目而异的决定,所以只有你才能做出那个决定。无论如何,祝项目好运!
  • @Knoop 谢谢你,兄弟,感谢你的帮助。

标签: xaml xamarin xamarin.forms xamarin.android xamarin.ios


【解决方案1】:

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

你可以使用 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/

【讨论】:

  • 谢谢,但我建议您在答案中添加链接中的相关位,因为该链接将来可能会失效!
猜你喜欢
  • 2013-10-23
  • 1970-01-01
  • 2021-07-21
  • 1970-01-01
  • 2021-09-29
  • 1970-01-01
  • 2018-06-14
  • 2021-09-18
  • 1970-01-01
相关资源
最近更新 更多