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

.NET MAUI中ObservableCollection更新后UI未同步问题求助

.NET MAUI中ObservableCollection元素属性修改后UI不更新的解决方法

问题根源

ObservableCollection仅会在集合本身发生变更(添加、删除、替换元素)时触发UI更新,但集合内元素的属性值修改不会自动通知UI——因为你的Account类没有实现INotifyPropertyChanged接口,UI无法感知到单个对象的属性变化。你之前给集合属性添加PropertyChanged通知,只解决了集合引用替换的问题,对元素内部属性变更无效。

解决方案:让Account类实现INotifyPropertyChanged

修改Account类,实现INotifyPropertyChanged接口,并在属性的setter中触发PropertyChanged事件:

public class Account : INotifyPropertyChanged
{
    private int _id;
    private string _accountName;
    private decimal _startingBalance;

    public int Id
    {
        get => _id;
        set
        {
            if (_id != value)
            {
                _id = value;
                OnPropertyChanged(nameof(Id));
            }
        }
    }

    public string AccountName
    {
        get => _accountName;
        set
        {
            if (_accountName != value)
            {
                _accountName = value;
                OnPropertyChanged(nameof(AccountName));
            }
        }
    }

    public decimal StartingBalance
    {
        get => _startingBalance;
        set
        {
            if (_startingBalance != value)
            {
                _startingBalance = value;
                OnPropertyChanged(nameof(StartingBalance));
            }
        }
    }

    public override string ToString()
    {
        return $"Id: {Id}: {AccountName} | {StartingBalance}";
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

额外优化:ViewModel规范实现INotifyPropertyChanged

你的ViewModel已经有了属性变更通知的基础逻辑,显式实现INotifyPropertyChanged接口会更规范,同时可以优化属性的判断逻辑:

public class YourViewModel : INotifyPropertyChanged
{
    private ObservableCollection<Account> _accounts = new();
    public ObservableCollection<Account> Accounts
    {
        get => _accounts;
        set
        {
            if (_accounts != value)
            {
                _accounts = value;
                OnPropertyChanged(nameof(Accounts));
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public void EditAccount(string editAccountName, string editAccountStartingBalance, int editAccountId)
    {
        if (decimal.TryParse(editAccountStartingBalance, out var startingBalance))
        {
            var editAccount = Accounts.FirstOrDefault(x => x.Id == editAccountId);
            if (editAccount != null)
            {
                editAccount.AccountName = editAccountName;
                editAccount.StartingBalance = startingBalance;
            }
        }
    }
}

为什么之前的修改无效?

你给Accounts集合属性添加的PropertyChanged通知,只有当你替换整个集合实例(比如执行Accounts = new ObservableCollection<Account>())时才会触发。而修改集合内元素的属性,属于单个对象的状态变更,需要对象自身发出通知。

完成上述修改后,当你调用EditAccount修改Account的属性时,UI会立即收到变更通知并同步更新,无需切换页面。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 13:05:30