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

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

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 &quot;Name&quot; not found on &quot;MauiTest.ViewModels.MainViewModel&quot;.

Line that the error originates from:

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

View:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;
&lt;ContentPage xmlns=&quot;http://schemas.microsoft.com/dotnet/2021/maui&quot;
             xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;
             x:Class=&quot;MauiTest.MainPage&quot;
             xmlns:vm=&quot;clr-namespace:MauiTest.ViewModels&quot;
             x:DataType=&quot;vm:MainViewModel&quot;&gt;

  &lt;StackLayout&gt;
    
    &lt;CarouselView BindingContext=&quot;{Binding Models}&quot; &gt;
      
      &lt;CarouselView.ItemTemplate&gt; 
        &lt;DataTemplate&gt;
          &lt;ContentView&gt;
            &lt;StackLayout &gt;
              &lt;Label Text=&quot;{Binding Path=Name}&quot; /&gt;
              &lt;Label Text=&quot;{Binding Path=Description}&quot; /&gt;
            &lt;/StackLayout&gt;
          &lt;/ContentView&gt;
        &lt;/DataTemplate&gt;
      &lt;/CarouselView.ItemTemplate&gt;
      
    &lt;/CarouselView&gt;
    
  &lt;/StackLayout&gt;
  
&lt;/ContentPage&gt;

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&lt;TestModel&gt; m_models;
  public MainViewModel() 
  {
    Models = new List&lt;TestModel&gt;
    {
      new TestModel(&quot;name1&quot;, &quot;test1&quot;),                                   
      new TestModel(&quot;name2&quot;, &quot;test2&quot;)
    };
  }
}

答案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

&lt;ContentPage xmlns=&quot;http://schemas.microsoft.com/dotnet/2021/maui&quot;
         xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;
         x:Class=&quot;GetProperties75380426.MainPage&quot;
         xmlns:vm=&quot;clr-namespace:GetProperties75380426.ViewModel&quot;
         &gt;


&lt;StackLayout&gt;
    &lt;CarouselView ItemsSource=&quot;{Binding Models}&quot;  &gt;     
        &lt;CarouselView.ItemTemplate&gt; 
            &lt;DataTemplate&gt;
            &lt;ContentView&gt;
                &lt;StackLayout &gt;
                    &lt;Label Text=&quot;{Binding Path=Name}&quot; /&gt;
                    &lt;Label Text=&quot;{Binding Path=Description}&quot; /&gt;
                &lt;/StackLayout&gt;
            &lt;/ContentView&gt;
            &lt;/DataTemplate&gt;
         &lt;/CarouselView.ItemTemplate&gt; 
     &lt;/CarouselView&gt;
&lt;/StackLayout&gt;

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=&quot;vm:MainViewModel&quot;

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

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

  • 要么:从 XAML 文件中删除所有 x:Datatype
  • 要么:添加缺失的 x:Datatype
&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:

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:
&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:

确定