【发布时间】:2019-09-30 12:59:33
【问题描述】:
我有一个Page 和一个使用BindableLayout.ItemsSource 的StackLayout,在每个项目内我都有一个ListView,对于这个嵌套ListView 中的每个项目我需要对一个属性执行Binding页面的 ViewModel。我正在尝试使用 Source+Path 方法,但一旦我打开包含此结构的页面,应用程序就会崩溃。
MainPage.xaml:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
x:Class="BindableLayoutReferenceBug.ListViewPage"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:BindableLayoutReferenceBug">
<StackLayout BindableLayout.ItemsSource="{Binding Items}">
<BindableLayout.ItemTemplate>
<DataTemplate>
<local:MessageListViewTemplate />
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
</ContentPage>
MessageListViewTemplate.xaml:
<?xml version="1.0" encoding="UTF-8" ?>
<ContentView
x:Class="BindableLayoutReferenceBug.MessageListViewTemplate"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Name="listView">
<ListView ItemsSource="{Binding Options}">
<ListView.ItemTemplate>
<DataTemplate>
<StackLayout>
<Label Text="{Binding .}" />
<Button
BackgroundColor="Blue"
Command="{Binding Source={x:Reference Name=listView}, Path=Parent.BindingContext.ShowMessageCommand}"
CommandParameter="{Binding .}"
Text="Show Message" />
</StackLayout>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentView>
异常说明找我用的x:Name的引用有问题:找不到listView引用的对象
只有在我提到的嵌套结构(StackLayout with BindableLayout > ListView)时才会发生这种情况。我不确定这是否是不受支持的方案,或者它是否是一个错误。
我尝试使用不同的路径进行绑定,但由于这是解析 XAML 时找不到引用的 x:Name 的问题,我认为它甚至不会开始评估我的路径。
我做错了什么还是这是 Xamarin.Forms 中的错误?
使用的 Xamarin.Forms 版本:3.6.0.293080
复制样本:https://github.com/akamud/BindableLayoutReferenceBug
【问题讨论】:
-
如果您需要这种嵌套结构,您能否在 MessageListViewTemplate 上定义一个可绑定属性 ButtonCommand,并在 MainPage.xaml 中设置该绑定?在MessageListViewTemplate中,ButtonCommand的OnPropertyChanged,设置按钮的Command为新命令?
-
我知道有一些解决方法可能在某些情况下有效,但提出这个问题的主要原因是了解该情况是否受支持以及 XF 本身是否存在错误。
标签: xamarin xamarin.forms