从绑定到.NET MAUI中的CarouselView的对象列表中获取属性的方法。

huangapple go评论108阅读模式
英文:

How to get properties from a list of objects binded to a CarouselView in .NET MAUI

问题

我有一个对象列表绑定到 CarouselView。在 CarouselView 内部,有一个简单的 DataTemplate,其中包含一些标签,它们应该绑定到对象的属性。然而,这些属性未被识别。

我正在使用 CommunityToolKit.Mvvm 来实现一个简单的 MVVM 解决方案。

我得到的错误是在第 16 行的视图中:

  1. Error XFC0045 Binding: Property "Name" not found on "MauiTest.ViewModels.MainViewModel".

错误起源于以下行:

  1. <Label Text="{Binding Path=Name}" />

视图:

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  4. x:Class="MauiTest.MainPage"
  5. xmlns:vm="clr-namespace:MauiTest.ViewModels"
  6. x:DataType="vm:MainViewModel">
  7. <StackLayout>
  8. <CarouselView BindingContext="{Binding Models}" >
  9. <CarouselView.ItemTemplate>
  10. <DataTemplate>
  11. <ContentView>
  12. <StackLayout >
  13. <Label Text="{Binding Path=Name}" />
  14. <Label Text="{Binding Path=Description}" />
  15. </StackLayout>
  16. </ContentView>
  17. </DataTemplate>
  18. </CarouselView.ItemTemplate>
  19. </CarouselView>
  20. </StackLayout>
  21. </ContentPage>

模型:

  1. public partial class TestModel : ObservableObject
  2. {
  3. [ObservableProperty]
  4. private string m_name;
  5. [ObservableProperty]
  6. private string m_description;
  7. public TestModel(string name, string description)
  8. {
  9. Name = name;
  10. Description = description;
  11. }
  12. }

视图模型:

  1. public partial class MainViewModel : ObservableObject
  2. {
  3. [ObservableProperty]
  4. private List<TestModel> m_models;
  5. public MainViewModel()
  6. {
  7. Models = new List<TestModel>
  8. {
  9. new TestModel("name1", "test1"),
  10. new TestModel("name2", "test2")
  11. };
  12. }
  13. }
英文:

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:

  1. Error XFC0045 Binding: Property &quot;Name&quot; not found on &quot;MauiTest.ViewModels.MainViewModel&quot;.

Line that the error originates from:

  1. &lt;Label Text=&quot;{Binding Path=Name}&quot; /&gt;

View:

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;
  2. &lt;ContentPage xmlns=&quot;http://schemas.microsoft.com/dotnet/2021/maui&quot;
  3. xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;
  4. x:Class=&quot;MauiTest.MainPage&quot;
  5. xmlns:vm=&quot;clr-namespace:MauiTest.ViewModels&quot;
  6. x:DataType=&quot;vm:MainViewModel&quot;&gt;
  7. &lt;StackLayout&gt;
  8. &lt;CarouselView BindingContext=&quot;{Binding Models}&quot; &gt;
  9. &lt;CarouselView.ItemTemplate&gt;
  10. &lt;DataTemplate&gt;
  11. &lt;ContentView&gt;
  12. &lt;StackLayout &gt;
  13. &lt;Label Text=&quot;{Binding Path=Name}&quot; /&gt;
  14. &lt;Label Text=&quot;{Binding Path=Description}&quot; /&gt;
  15. &lt;/StackLayout&gt;
  16. &lt;/ContentView&gt;
  17. &lt;/DataTemplate&gt;
  18. &lt;/CarouselView.ItemTemplate&gt;
  19. &lt;/CarouselView&gt;
  20. &lt;/StackLayout&gt;
  21. &lt;/ContentPage&gt;

Model:

  1. public partial class TestModel : ObservableObject
  2. {
  3. [ObservableProperty]
  4. private string m_name;
  5. [ObservableProperty]
  6. private string m_description;
  7. public TestModel(string name, string description)
  8. {
  9. Name = name;
  10. Description = description;
  11. }
  12. }

ViewModel:

  1. public partial class MainViewModel : ObservableObject
  2. {
  3. [ObservableProperty]
  4. private List&lt;TestModel&gt; m_models;
  5. public MainViewModel()
  6. {
  7. Models = new List&lt;TestModel&gt;
  8. {
  9. new TestModel(&quot;name1&quot;, &quot;test1&quot;),
  10. new TestModel(&quot;name2&quot;, &quot;test2&quot;)
  11. };
  12. }
  13. }

答案1

得分: 0

你可以尝试以下的MVVM方式:

在XAML中,你可以将ItemsSource属性设置为Models

  1. <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  2. xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  3. x:Class="GetProperties75380426.MainPage"
  4. xmlns:vm="clr-namespace:GetProperties75380426.ViewModel">
  5. <StackLayout>
  6. <CarouselView ItemsSource="{Binding Models}" >
  7. <CarouselView.ItemTemplate>
  8. <DataTemplate>
  9. <ContentView>
  10. <StackLayout >
  11. <Label Text="{Binding Path=Name}" />
  12. <Label Text="{Binding Path=Description}" />
  13. </StackLayout>
  14. </ContentView>
  15. </DataTemplate>
  16. </CarouselView.ItemTemplate>
  17. </CarouselView>
  18. </StackLayout>
  19. </ContentPage>

在.cs文件中,设置页面的BindingContext为MainViewModel

  1. public MainPage()
  2. {
  3. InitializeComponent();
  4. this.BindingContext = new MainViewModel();
  5. }

欲了解更多信息,你可以参考使用数据填充 CarouselView

希望对你有所帮助。

英文:

You could try the following MVVM way:

In xaml, you could set the ItemsSource property to Models

  1. &lt;ContentPage xmlns=&quot;http://schemas.microsoft.com/dotnet/2021/maui&quot;
  2. xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;
  3. x:Class=&quot;GetProperties75380426.MainPage&quot;
  4. xmlns:vm=&quot;clr-namespace:GetProperties75380426.ViewModel&quot;
  5. &gt;
  6. &lt;StackLayout&gt;
  7. &lt;CarouselView ItemsSource=&quot;{Binding Models}&quot; &gt;
  8. &lt;CarouselView.ItemTemplate&gt;
  9. &lt;DataTemplate&gt;
  10. &lt;ContentView&gt;
  11. &lt;StackLayout &gt;
  12. &lt;Label Text=&quot;{Binding Path=Name}&quot; /&gt;
  13. &lt;Label Text=&quot;{Binding Path=Description}&quot; /&gt;
  14. &lt;/StackLayout&gt;
  15. &lt;/ContentView&gt;
  16. &lt;/DataTemplate&gt;
  17. &lt;/CarouselView.ItemTemplate&gt;
  18. &lt;/CarouselView&gt;
  19. &lt;/StackLayout&gt;

In .cs file, set the BindingContext of the page, that is the MainViewModel

  1. public MainPage()
  2. {
  3. InitializeComponent();
  4. this.BindingContext = new MainViewModel();
  5. }

For more info, you could refer to Populate a CarouselView with data.

Hope it works for you.

答案2

得分: 0

错误消息指出它正在尝试在 MainViewModel 上查找属性 Name

它在那里寻找(而不是在 TestModel 中),是因为这个 XAML 代码:

  1. x:DataType=&quot;vm:MainViewModel&quot;

一旦在 XAML 文件中添加了 任何 x:DataType,你就必须在它改变的任何地方添加正确的 x:DataType

你可以有两种不同的修复方式:

  • 要么:从 XAML 文件中删除所有 x:Datatype
  • 要么:添加缺失的 x:Datatype
  1. &lt;ContentView x:DataType=&quot;PutCorrectNamespaceHere.TestModel&quot;&gt;

注意:未经测试。我不确定这是否是放置缺失的 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:

  1. x:DataType=&quot;vm:MainViewModel&quot;

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:Datatypes from the xaml file.
  • Or: add the missing x:Datatype:
  1. &lt;ContentView x:DataType=&quot;PutCorrectNamespaceHere.TestModel&quot;&gt;

NOTE: Not tested. I'm not sure that is the correct line to put the missing x:DataType.

huangapple
  • 本文由 发表于 2023年2月8日 08:53:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75380426.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定