【问题标题】:Bindable Layouts > Binding: Property X not found on ViewModel可绑定布局 > 绑定:在 ViewModel 上找不到属性 X
【发布时间】:2021-01-13 06:26:15
【问题描述】:

我有一个简单的 XAML 页面,代码如下:

<StackLayout BindableLayout.ItemsSource="{Binding Data}">
    <BindableLayout.ItemTemplate>
        <DataTemplate>
            <Label BackgroundColor="{Binding Color}"
                   Text="{Binding Text}"
                   FontAttributes="Italic"
                   FontSize="20"
                   VerticalTextAlignment="Center"
                   HorizontalTextAlignment="Center"
                   HeightRequest="105"
                   Margin="25" />
        </DataTemplate>
    </BindableLayout.ItemTemplate>
</StackLayout>

在我后面的查看页面代码中:

public partial class DataView
    {
        public DataView()
        {
            InitializeComponent();

            BindingContext = new ViewModel();
        }
...
}

数据是:

public class ViewModel
{
    public ViewModel()
    {
        Data = new ObservableCollection()<Model> { 
            new Model { Text = "Pink", Color = Color.DeepPink }, 
            new Model { Text = "Crimson", Color = Color.Crimson }, 
            new Model { Text = "Aqua", Color = Color.Aqua }, 
            new Model { Text = "Blue", Color = Color.DeepSkyBlue }, 
            new Model { Text = "BurlyWood", Color = Color.BurlyWood }, };
    }
 
    public ObservableCollection<Model> Data { get; set; }
}
 
public class Model
{
    public Color Color { get; set; }
    public string Text { get; set; }
}

但是,在编译时,我得到:

绑定:在“MyComp.ViewModel”上找不到属性“Color”。

似乎它在 ViewModel 中搜索 Color 而不是父级(数据)。

我正在使用最新的 Xamarin.Forms 4.8.0、.NET Standard 2.0 和 Visual Studio 2019 16.7.4。

【问题讨论】:

  • 尝试使用 Color 以外的名称重命名您的属性
  • 用 ColorTyp 试过了,还是一样。在编译时失败并出现此错误。
  • 我创建了一个基本示例来测试您的代码,它工作正常。尝试清理项目的 obj 和 bin 文件夹,然后重新构建。 'ViewModel' 类的代码应如下所示,确保引用正确的包。 public class ViewModel { public ViewModel() { Data = new System.Collections.ObjectModel.ObservableCollection&lt;Model&gt;() {...}; } public ObservableCollection&lt;Model&gt; Data { get; set; } }
  • 根据描述无法重现问题,请贴出相关代码。如果您确认这是一个潜在问题,您可以在 github 上报告该问题。
  • 我贴了相关代码,你可以参考一下。

标签: xamarin xamarin.forms binding


【解决方案1】:

我测试了 xaml 代码,它仍然可以正常工作。检查 gif:https://us.v-cdn.net/5019960/uploads/editor/pd/aa0fp5pwnvkl.gif

这是相关代码,你可以参考一下。

Page.xaml

<StackLayout x:Name="expanderLayout" BindableLayout.ItemsSource="{Binding Data}">
    <BindableLayout.ItemTemplate>
        <DataTemplate>
            <Expander>
                <Expander.Header>
                    <Grid>
                        <Label BackgroundColor="{Binding Color}"/>
                    </Grid>
                </Expander.Header>
                <Expander.ContentTemplate>
                    <DataTemplate>
                        <Label
                            Text="{Binding Text}"
                            FontAttributes="Italic"
                            FontSize="20"
                            VerticalTextAlignment="Center"
                            HorizontalTextAlignment="Center"
                            HeightRequest="105"
                            Margin="25" />
                    </DataTemplate>
                </Expander.ContentTemplate>
            </Expander>
        </DataTemplate>
    </BindableLayout.ItemTemplate>
</StackLayout>

Page.xaml.cs

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();

        BindingContext = new ViewModel();
    }
}

模型类和视图模型类

public class ViewModel
{
    public ViewModel()
    {
        Data = new System.Collections.ObjectModel.ObservableCollection<Model>() {
                new Model { Text = "Pink", Color = Color.DeepPink },
            new Model { Text = "Crimson", Color = Color.Crimson },
            new Model { Text = "Aqua", Color = Color.Aqua },
            new Model { Text = "Blue", Color = Color.DeepSkyBlue },
            new Model { Text = "BurlyWood", Color = Color.BurlyWood }, };
    }

    public ObservableCollection<Model> Data { get; set; }
}

public class Model
{
    public Color Color { get; set; }
    public string Text { get; set; }
}

【讨论】:

  • 我的页面定义中有 x:DataType="local:ViewModel" 。当我删除它时,它起作用了。
  • 谢谢@MilanM。实际上,我有一个工具提示帮助要求添加 DataType 条目以帮助绑定 Intellisense。不知道那是什么破坏了我的 BindingLayout。对于发现此内容的任何其他人,您还可以在 DataTemplate 声明中添加一个 DataType 条目并告诉它它是什么类型。因此,对于 Milan 的条目,将 DataTemplat 更改为 然后添加一个定义模型的 xmlns 也会修复它。
【解决方案2】:

我尝试运行您的代码并没有发现任何问题 除了你的问题中有一些轻微的错字,我有这个

   public ViewModel()
    {
        Data = new ObservableCollection<Model>
        {
            new Model {Text = "Pink", Color = Color.DeepPink},
            new Model {Text = "Crimson", Color = Color.Crimson},
            new Model {Text = "Aqua", Color = Color.Aqua},
            new Model {Text = "Blue", Color = Color.DeepSkyBlue},
            new Model {Text = "BurlyWood", Color = Color.BurlyWood},
        };
    }

,也许尝试清理/重建解决方案并再次运行?

【讨论】:

    猜你喜欢
    • 2021-12-17
    • 2013-06-25
    • 2011-02-01
    • 2020-08-11
    • 2011-03-01
    • 2012-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多