c# - 使用 MVVM 绑定(bind) : Property not found in Xamarin. 表单

标签 c# xamarin.forms mvvm

XFC0045 绑定(bind):在“Page_Navigation.ViewModels.ThirdPageViewModel”上找不到属性“Tittle”。从该区域获取错误 - Page_Navigation\Views\ThirdPage.xaml

using Xamarin.Forms;

namespace Page_Navigation.ViewModels
{
    public class ThirdPageViewModel : BindableObject
    {
        public ObservableCollection<Matches> MatchesList { get; set; }
        
        public ThirdPageViewModel()
        {
            MatchesList = new ObservableCollection<Matches>();
            MatchesList.Add(new Matches { Tittle = "Tittle 1", Description = "Description 1" });
            MatchesList.Add(new Matches { Tittle = "Tittle 2", Description = "Description 2" });
            MatchesList.Add(new Matches { Tittle = "Tittle 3", Description = "Description 3" });
            MatchesList.Add(new Matches { Tittle = "Tittle 4", Description = "Description 4" });
        }
    }

    public class Matches
    {
        public string Tittle { get; set; }
        public string Description { get; set; }
    }
}

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
             xmlns:VM="clr-namespace:Page_Navigation.ViewModels"
             x:DataType="VM:ThirdPageViewModel"
             x:Class="Page_Navigation.Views.ThirdPage"
             NavigationPage.HasNavigationBar="True"
             NavigationPage.HasBackButton="True"
             Title="Third Page">

    <ContentPage.BindingContext>
        <VM:ThirdPageViewModel/>
    </ContentPage.BindingContext>
    
    <ContentPage.Content>
        <ListView ItemsSource="{Binding MatchesList}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Grid HorizontalOptions="FillAndExpand"
                              VerticalOptions="FillAndExpand">
                            <Label Text="{Binding Tittle}"/>
                            <Label Text="{Binding Description}"/>
                        </Grid>
                    </ViewCell>
                </DataTemplate>
                
            </ListView.ItemTemplate>
        </ListView>
    </ContentPage.Content>
</ContentPage>

I don't know why this exception. I am using VS2019 and Xamarin.forms 4.8.0.1451

最佳答案

您必须删除 x:DataType 属性,因为它与编译绑定(bind)相关,并且您已经为 View 模型定义了 BindingContext。

在此处检查 XF 编译绑定(bind):Xamarin.Forms Compiled Bindings

关于c# - 使用 MVVM 绑定(bind) : Property not found in Xamarin. 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65791339/

相关文章:

c# - 如何构建类似于 TransactionScope 的类

android - 更改微调器文本颜色

c# - 如何使用 MVVM 更新 Datagrid?

c# - 使用 CanExecute 参数调用 RelayCommand<T>

c# - 在 XNA 中依次显示列表中的名称

c# - 无法将带有 [] 的索引应用于 mvc Controller 中类型为“System.Collections.Generic.ICollection<int>”的表达式

c# - 保存派生类的集合实例的基类

c# - Xamarin.Forms ListView 更改 Cell on ContextMenu 打开

c# - 当我转到上一页时,所选图像的颜色不显示在 Collection View 中

c# - 从 MVVM 中的 DataGrid 或 ListBox 绑定(bind)到 SelectedItems