XAML视图绑定对象变更后UI未更新问题求助
问题分析与解决方案
嘿,我一眼就瞅到问题的核心啦!你触发PropertyChanged时传错了参数,这才导致UI没收到更新通知。
问题根源
在你的VotingViewModel里,currentGame属性的setter是这么写的:
public GAME_TBL currentGame { get { return _currentGame; } set { _currentGame = value; OnPropertyChanged(nameof(_currentGame)); // 这里出错了! } }
你传给OnPropertyChanged的是后台字段名_currentGame,但Xamarin的绑定系统是基于属性名来监听变更的——它根本不知道_currentGame这个字段对应UI上绑定的currentGame属性,所以就算触发了事件,UI也不会响应。
修复方案
把OnPropertyChanged的参数改成属性名currentGame就搞定了:
public GAME_TBL currentGame { get { return _currentGame; } set { _currentGame = value; OnPropertyChanged(nameof(currentGame)); // 改成属性名! } }
如果你的BaseViewModel支持通过CallerMemberName特性自动推断调用属性,也可以简化成:
OnPropertyChanged();
不过显式传nameof(currentGame)更安全,能避免拼写错误。
额外检查点(可选)
如果修复后还是没更新,可以确认下:
loadGameInfo()是不是在UI线程执行的?如果API调用是异步操作,记得切回UI线程再赋值:currentGame = await loadGameInfo(); // 或者同步方法在后台线程调用时: Device.BeginInvokeOnMainThread(() => { currentGame = loadGameInfo(); });- 若后续需要修改
currentGame内部的属性值,要确保GAME_TBL类也实现了INotifyPropertyChanged接口,但你当前是直接替换整个对象,这一点暂时不影响问题解决。
内容的提问来源于stack exchange,提问作者Journeyman1234
相关产品推荐
相关产品推荐

