You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET MAUI绑定报错:未找到DodatniNaziv属性求助

解决.NET MAUI中XFC0045绑定错误(ParkingSistemiViewModel未找到DodatniNaziv属性)

可能的原因及排查修复步骤

1. 绑定上下文未正确关联

检查页面的BindingContext是否正确设置为ParkingSistemiViewModel:

  • 代码后台(.xaml.cs)构造函数设置示例:
public ParkingPage()
{
    InitializeComponent();
    BindingContext = new ParkingSistemiViewModel();
}
  • XAML内设置示例(需确保local命名空间已正确引用ViewModel所在程序集):
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:YourAppNamespace.ViewModels"
             x:Class="YourAppNamespace.ParkingPage">
    <ContentPage.BindingContext>
        <local:ParkingSistemiViewModel />
    </ContentPage.BindingContext>
</ContentPage>

2. ViewModel中缺少目标属性或访问权限错误

确认ParkingSistemiViewModel中存在公共可访问的DodatniNaziv属性,且实现INotifyPropertyChanged以支持数据绑定更新:

using System.ComponentModel;
using System.Runtime.CompilerServices;

public class ParkingSistemiViewModel : INotifyPropertyChanged
{
    private string _dodatniNaziv;
    // 必须为public,绑定系统无法访问private/internal属性
    public string DodatniNaziv
    {
        get => _dodatniNaziv;
        set
        {
            _dodatniNaziv = value;
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

3. 绑定路径大小写/拼写错误

MAUI数据绑定默认区分大小写,检查XAML中的绑定路径是否与属性名完全一致:
错误示例(小写开头与ViewModel属性名不匹配):

<Label Text="{Binding dodatniNaziv}" />

正确示例:

<Label Text="{Binding DodatniNaziv}" />

4. 列表项绑定对象错误(ListView/CollectionView场景)

如果是列表控件的ItemTemplate中绑定DodatniNaziv,该属性应属于列表项的Model类而非ViewModel:

  • ViewModel中需包含集合属性:
public ObservableCollection<ParkingModel> ParkingItems { get; set; } = new();
  • XAML列表控件绑定集合,ItemTemplate绑定Model属性:
<ListView ItemsSource="{Binding ParkingItems}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <!-- 此处绑定的是ParkingModel的DodatniNaziv属性 -->
                <Label Text="{Binding DodatniNaziv}" />
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

此时需确保Model类存在对应属性:

public class ParkingModel
{
    public string DodatniNaziv { get; set; }
    // 其他业务属性...
}

额外修复手段

  • 清理并重建解决方案:编译缓存可能导致绑定识别异常,执行清理解决方案→生成解决方案后重新运行
  • 检查命名空间:确认XAML中引用ViewModel/Model的命名空间无拼写错误

内容的提问来源于stack exchange,提问作者whyMe251

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 09:03:18