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

Xamarin中Grid背景色无法随Status动态变更问题排查

问题解决:状态变更后Grid背景色未更新

问题核心是Exercise类未实现属性变更通知机制,修改Status属性时UI无法感知变化,因此不会重新调用转换器更新背景色。初始化时能触发转换器是因为绑定首次加载,动态修改属性时缺少通知环节导致无响应。

修复步骤

让Exercise类实现INotifyPropertyChanged接口,在Status属性的setter中触发属性变更通知:

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

public class Exercise : INotifyPropertyChanged
{
    private Status _status;
    public Status Status
    {
        get => _status;
        set
        {
            if (_status != value)
            {
                _status = value;
                OnPropertyChanged();
            }
        }
    }

    // 实现INotifyPropertyChanged接口
    public event PropertyChangedEventHandler PropertyChanged;

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

    // 类内其他属性、方法...
}

原理说明

当Status属性值变化时,OnPropertyChanged方法会触发PropertyChanged事件,通知绑定的Grid更新绑定值,此时转换器会重新执行,根据新状态返回对应背景色。

你的按钮触发方法、转换器及XAML代码无需修改,只需确保Exercise类正确实现属性变更通知即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 12:52:46