Binding:属性在Xamarin中找不到.表单使用MVVM



XFC0045 Binding: Property "在"page_navigation . viewmodels . thirdpageviewmodel"上找不到。从这个区域获取错误- Page_NavigationViewsThirdPage.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>

我不知道为什么这个异常。我正在使用VS2019和Xamarin。形式4.8.0.1451

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

查看有关XF编译绑定的信息:Xamarin。表单编译绑定

最好的选择:添加新的额外x:DataType到DataTemplate标签

<DataTemplate  x:DataType="VM:Matches" >
<ViewCell>
<Grid HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand">
<Label Text="{Binding Tittle}"/>
<Label Text="{Binding Description}"/>
</Grid>
</ViewCell>
</DataTemplate>

它正在提高数据绑定性能

最新更新