Xamarin.forms: Xamarin 表单内容视图(用户控件)未在 ListView 内绑定

创建于 2018-05-15  ·  3评论  ·  资料来源: xamarin/Xamarin.Forms

描述

有一个绑定到字符串 ObservableCollection 的 ListView。 Listview 有一个标签和一个 ContentView(只包含一个标签)。 两者都绑定到同一个集合。

此外,还有一个按钮可以为集合生成一些随机数据。

问题是当我运行应用程序并单击“生成数据”按钮时,标签会更新,但不会更新 ContentView。

重现步骤

  1. 打开 VS 2017 并新建一个 Xamarin Forms 空白项目(标准)
  2. 在 MainPage.xaml 中放置一个带有按钮和列表视图的 StackLayout
  3. 将 Listview 的 DataTemplate 设置为包含一个 Label 和一个 ContentView 的 ViewCell(我们将在下面创建它)。 将 Text 属性绑定到 ObservableCollection 项。
  4. 在 MainPage.xaml.cs 中创建一个 Observalbe财产
  5. 在构造函数中用一些随机值初始化 Observable 集合并将绑定上下文设置为 this。
  6. 订阅按钮点击并在每次点击按钮时为 Observable 集合分配一些随机值。
  7. 创建一个上面使用的新 ContentView(xaml),并在其中放置一个绑定到 Text 属性的标签
  8. 在 xaml.cs 中创建一个可绑定的属性文本。
  9. 或者您可以简单地在 VS 2017(15.7.1) 中解压并打开附加的解决方案
    测试样本.zip
  10. 在 Android 中运行,然后单击生成按钮。
  11. 请注意绑定到 label 的文本正在更改,但是绑定到 ContentView 的文本不会更改。

预期行为

内容视图内的标签也应该改变。

实际行为

内容视图内的标签保持不变

基本信息

  • 有问题的版本:4.10.0.442
  • 最后一个已知的好版本:
  • IDE:VS 2017 (15.7.1)
  • 平台目标框架:

    • IOS:

    • 安卓:8.3.0.19

    • UWP:

  • 安卓支持库版本:
  • Nuget 包:
    Xamarin.Forms
    NET标准库

  • 受影响的设备:

截图

复制链接

https://github.com/AppGrate/ListViewBindingIssue

question

最有用的评论

正在做

this.BindingContext = this;

在 MagicBox.xaml.cs 中强制 BindingContext 到当前对象。 这也意味着不再继承父级的 BindingContext。

为了使其工作,将您的代码更改为

    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class MagicBox : ContentView
    {

        public static readonly BindableProperty TextProperty =
            BindableProperty.Create("Text", typeof(string), typeof(MagicBox), default(string));

        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        public MagicBox ()
        {
            InitializeComponent ();
        }
    }

和你的 xaml

<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="TestSample.Controls.MagicBox"
             x:Name="box">
  <ContentView.Content>
      <Grid>
          <Label Text="{Binding Text, Source={x:Reference box}}" />
      </Grid>
  </ContentView.Content>
</ContentView>

所有3条评论

正在做

this.BindingContext = this;

在 MagicBox.xaml.cs 中强制 BindingContext 到当前对象。 这也意味着不再继承父级的 BindingContext。

为了使其工作,将您的代码更改为

    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class MagicBox : ContentView
    {

        public static readonly BindableProperty TextProperty =
            BindableProperty.Create("Text", typeof(string), typeof(MagicBox), default(string));

        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        public MagicBox ()
        {
            InitializeComponent ();
        }
    }

和你的 xaml

<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="TestSample.Controls.MagicBox"
             x:Name="box">
  <ContentView.Content>
      <Grid>
          <Label Text="{Binding Text, Source={x:Reference box}}" />
      </Grid>
  </ContentView.Content>
</ContentView>

谢谢@StephaneDelcroix ,它奏效了:)

此页面是否有帮助?
0 / 5 - 0 等级