0

I have a Page with a StackLayout using BindableLayout.ItemsSource, inside each item I have a ListView, for each item in this nested ListView I need to do a Binding to a property on the Page's ViewModel. I'm trying to use the Source+Path approach but the app crashes as soon as I open the page that contains this structure.

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>

The exception shows that there is a problem finding the reference to the x:Name I used: Can not find the object referenced by listView

enter image description here

This only happens when I have a nested structure as I mentioned (StackLayout with BindableLayout > ListView). I'm not sure if this is not a supported scenario or if it is a bug.

I tried using different Paths for the binding, but since this is a problem parsing the XAML not finding the x:Name referenced, I don't think it even starts evaluating my Path.

Am I doing something wrong or is this a bug in Xamarin.Forms?

Xamarin.Forms version used: 3.6.0.293080
Repro sample: https://github.com/akamud/BindableLayoutReferenceBug

4

2 回答 2

1

毕竟这是一个错误,现在已修复,上述内容应该可以工作:https ://github.com/xamarin/Xamarin.Forms/issues/6192

于 2020-03-07T12:45:21.590 回答
1

也许,x:Name 在运行时无法识别,即使您已将其设置为 XAML 中的内容视图。你可以在 GitHub 上提出问题。在这里,我使用后面的代码尝试了这个绑定,它工作正常。您可以将其用作替代方案:

public MessageListViewTemplate()
{
    InitializeComponent();

    listView.ItemTemplate = new DataTemplate(() =>
    {
        ViewCell viewCell = new ViewCell();
        Label label = new Label();
        label.SetBinding(Label.TextProperty, new Binding("."));

        Button button = new Button() { Text = "Show Message", BackgroundColor = Color.Blue };
        button.SetBinding(Button.CommandProperty, new Binding("Parent.BindingContext.ShowMessageCommand", source: ContentView));
        button.SetBinding(Button.CommandParameterProperty, new Binding("."));


        viewCell.View = new StackLayout
        {
            Children =
            {
                label,
                button
            }
        };

        return viewCell;
    });
}

XAML:

<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
             x:Class="BindableLayoutReferenceBug.MessageListViewTemplate" 
             x:Name="ContentView">
    <StackLayout>
        <ListView ItemsSource="{Binding Options}" x:Name="listView" HasUnevenRows="True">


        </ListView>

    </StackLayout>

</ContentView>

在这里,我将父内容视图定义为ContentView并命名为 ListView listView

请注意,列表视图的数据模板应该是一个视图单元格。所以我使用了一个视图单元格来包装这里的内容。

于 2019-05-14T05:29:47.820 回答