英文:
How to get properties from a list of objects binded to a CarouselView in .NET MAUI
问题
我有一个对象列表绑定到 CarouselView。在 CarouselView 内部,有一个简单的 DataTemplate,其中包含一些标签,它们应该绑定到对象的属性。然而,这些属性未被识别。
我正在使用 CommunityToolKit.Mvvm 来实现一个简单的 MVVM 解决方案。
我得到的错误是在第 16 行的视图中:
Error XFC0045 Binding: Property "Name" not found on "MauiTest.ViewModels.MainViewModel".
错误起源于以下行:
<Label Text="{Binding Path=Name}" />
视图:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MauiTest.MainPage"
xmlns:vm="clr-namespace:MauiTest.ViewModels"
x:DataType="vm:MainViewModel">
<StackLayout>
<CarouselView BindingContext="{Binding Models}" >
<CarouselView.ItemTemplate>
<DataTemplate>
<ContentView>
<StackLayout >
<Label Text="{Binding Path=Name}" />
<Label Text="{Binding Path=Description}" />
</StackLayout>
</ContentView>
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
</StackLayout>
</ContentPage>
模型:
public partial class TestModel : ObservableObject
{
[ObservableProperty]
private string m_name;
[ObservableProperty]
private string m_description;
public TestModel(string name, string description)
{
Name = name;
Description = description;
}
}
视图模型:
public partial class MainViewModel : ObservableObject
{
[ObservableProperty]
private List<TestModel> m_models;
public MainViewModel()
{
Models = new List<TestModel>
{
new TestModel("name1", "test1"),
new TestModel("name2", "test2")
};
}
}
英文:
I have a list of objects binded to a CarouselView. Inside the CarouselView is a simple DataTemplate containing some labels that are supposed to be binded to the properties of the object. However the properties are not recognized.
I am using CommunityToolKit.Mvvm for a simple MVVM solution.
The error I am getting is in the View on Line 16:
Error XFC0045 Binding: Property "Name" not found on "MauiTest.ViewModels.MainViewModel".
Line that the error originates from:
<Label Text="{Binding Path=Name}" />
View:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MauiTest.MainPage"
xmlns:vm="clr-namespace:MauiTest.ViewModels"
x:DataType="vm:MainViewModel">
<StackLayout>
<CarouselView BindingContext="{Binding Models}" >
<CarouselView.ItemTemplate>
<DataTemplate>
<ContentView>
<StackLayout >
<Label Text="{Binding Path=Name}" />
<Label Text="{Binding Path=Description}" />
</StackLayout>
</ContentView>
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
</StackLayout>
</ContentPage>
Model:
public partial class TestModel : ObservableObject
{
[ObservableProperty]
private string m_name;
[ObservableProperty]
private string m_description;
public TestModel(string name, string description)
{
Name = name;
Description = description;
}
}
ViewModel:
public partial class MainViewModel : ObservableObject
{
[ObservableProperty]
private List<TestModel> m_models;
public MainViewModel()
{
Models = new List<TestModel>
{
new TestModel("name1", "test1"),
new TestModel("name2", "test2")
};
}
}
答案1
得分: 0
你可以尝试以下的MVVM方式:
在XAML中,你可以将ItemsSource属性设置为Models
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="GetProperties75380426.MainPage"
xmlns:vm="clr-namespace:GetProperties75380426.ViewModel">
<StackLayout>
<CarouselView ItemsSource="{Binding Models}" >
<CarouselView.ItemTemplate>
<DataTemplate>
<ContentView>
<StackLayout >
<Label Text="{Binding Path=Name}" />
<Label Text="{Binding Path=Description}" />
</StackLayout>
</ContentView>
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
</StackLayout>
</ContentPage>
在.cs文件中,设置页面的BindingContext为MainViewModel
public MainPage()
{
InitializeComponent();
this.BindingContext = new MainViewModel();
}
欲了解更多信息,你可以参考使用数据填充 CarouselView。
希望对你有所帮助。
英文:
You could try the following MVVM way:
In xaml, you could set the ItemsSource property to Models
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="GetProperties75380426.MainPage"
xmlns:vm="clr-namespace:GetProperties75380426.ViewModel"
>
<StackLayout>
<CarouselView ItemsSource="{Binding Models}" >
<CarouselView.ItemTemplate>
<DataTemplate>
<ContentView>
<StackLayout >
<Label Text="{Binding Path=Name}" />
<Label Text="{Binding Path=Description}" />
</StackLayout>
</ContentView>
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
</StackLayout>
In .cs file, set the BindingContext of the page, that is the MainViewModel
public MainPage()
{
InitializeComponent();
this.BindingContext = new MainViewModel();
}
For more info, you could refer to Populate a CarouselView with data.
Hope it works for you.
答案2
得分: 0
错误消息指出它正在尝试在 MainViewModel
上查找属性 Name
。
它在那里寻找(而不是在 TestModel
中),是因为这个 XAML 代码:
x:DataType="vm:MainViewModel"
一旦在 XAML 文件中添加了 任何 x:DataType
,你就必须在它改变的任何地方添加正确的 x:DataType
。
你可以有两种不同的修复方式:
- 要么:从 XAML 文件中删除所有
x:Datatype
。 - 要么:添加缺失的
x:Datatype
:
<ContentView x:DataType="PutCorrectNamespaceHere.TestModel">
注意:未经测试。我不确定这是否是放置缺失的 x:DataType
的正确位置。
英文:
The error message says that it is trying to find property Name
on MainViewModel
.
The reason it is looking there (instead of in TestModel
), is this xaml:
x:DataType="vm:MainViewModel"
As soon as you add any x:DataType
to a xaml file, you have to add the correct x:DataType
, anywhere it changes.
There are two different ways you can fix:
- Either: remove all
x:Datatype
s from the xaml file. - Or: add the missing
x:Datatype
:
<ContentView x:DataType="PutCorrectNamespaceHere.TestModel">
NOTE: Not tested. I'm not sure that is the correct line to put the missing x:DataType
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论