Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xaml 通过资源将ListView与DataTemplate一起使用时绑定不起作用。(MVVM)_Xaml_Xamarin_Xamarin.forms_Xamarin.android_Xamarin.ios - Fatal编程技术网

Xaml 通过资源将ListView与DataTemplate一起使用时绑定不起作用。(MVVM)

Xaml 通过资源将ListView与DataTemplate一起使用时绑定不起作用。(MVVM),xaml,xamarin,xamarin.forms,xamarin.android,xamarin.ios,Xaml,Xamarin,Xamarin.forms,Xamarin.android,Xamarin.ios,我有一个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:ffimage

我有一个
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>
我正在添加以下资源字典:

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

现在我要做的是在我的
ViewModel
中设置一个Click
命令,
通常情况下,我是这样做的,我在上面的示例中为
ListView
设置了一个名称,然后使用此名称获取列表的
BindingContext
,然后使用此名称将命令设置为该列表,但当我现在尝试使用此名称时,会引发XML解析器异常:

Xamarin.Forms.Xaml.XamlParseException:位置38:43。找不到主列表所引用的对象

现在我有两个问题:

1-按上述方法执行操作时,将命令设置到ListView的正确方法是什么


2-如果将来我打算将此移动到App.XAML(如果多次使用),那么如何将命令设置为它?

很抱歉出现了误导性链接,我刚才理解了您的问题

您可以使用ListView行为来解决您的问题,这比在代码后面绑定命令更有趣、更容易阅读

您可以按照下面的链接查看它是如何完成的,稍后只需将所有行为转换为一个并向其传递事件名称,在最后一个方法返回的对象上使用
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 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>


很抱歉出现了误导性链接,我刚才理解了您的问题

您可以使用ListView行为来解决您的问题,这比在代码后面绑定命令更有趣、更容易阅读

您可以按照下面的链接查看它是如何完成的,稍后只需将所有行为转换为一个并向其传递事件名称,在最后一个方法返回的对象上使用
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 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>


您可以查看这篇文章以供参考:您如何设置在资源字典中使用DataTemplate而不是自定义viewcell?无论如何,这可能是一个有趣的阅读:@Knoop谢谢,但我喜欢行为解决方案better@G.hakim没问题,只是考虑一些替代方案。我理解在这种情况下,这是一个更干净但具体情况与更通用/可扩展但不太干净之间的决定。这是一个因项目而异的决定,所以只有你才能做出决定。不管怎样,祝项目好运@Knoop谢谢你,兄弟,我感谢你的帮助。你可以查看这篇文章作为参考:你对在资源字典中使用DataTemplate而不是自定义viewcell有什么看法?无论如何,这可能是一个有趣的阅读:@Knoop谢谢,但我喜欢行为解决方案better@G.hakim没问题,只是考虑一些替代方案。我理解在这种情况下,这是一个更干净但具体情况与更通用/可扩展但不太干净之间的决定。这是一个因项目而异的决定,所以只有你才能做出决定。不管怎样,祝项目好运@Knoop无论如何谢谢你,兄弟,我感谢你的帮助。谢谢,但我建议你在回答中添加链接中的相关部分,因为链接将来可能会失效!谢谢,但我建议你在答案中添加链接中的相关位,因为链接将来可能会失效!