【问题标题】:How to bind a nested Layout with external ContentView in Xamarin.Forms?如何在 Xamarin.Forms 中将嵌套布局与外部 ContentView 绑定?
【发布时间】:2019-09-30 12:59:33
【问题描述】:

我有一个Page 和一个使用BindableLayout.ItemsSourceStackLayout,在每个项目内我都有一个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


【解决方案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

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

【讨论】:

  • 这就是我正在寻找的那种验证。谢谢,我正在打开这个问题。
  • @MahmoudAli 您可以在此处发布链接以帮助我们跟踪进度。
  • 这是问题:github.com/xamarin/Xamarin.Forms/issues/6192 只有在 XAMLC 开启时才会发生
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-09
    • 2020-09-24
    • 2019-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多