我有一个视图,其中包含从网络服务获取的一组图像
我在此类列表中收到它们:
public class ImageModel
{
public int Id { get; set; }
public string Name { get; set; }
public string imageUrl { get; set; }
}
在每个图像下我都会显示一个投票按钮,因此我向上面的模型添加了另一个 bool 属性:
public bool UpVoted { get; set; }
the ListView
显示这些图像绑定到ObservableCollection<ImageModel >
,我想通过转换值的转换器来更改投票图标UpVoted
到相应的图标,当用户单击投票图标时:命令执行此方法:
private void OnVoting(ImageModel image)
{
Images.Single(x => x.id == image.id).UpVoted = !image.UpVoted;
}
问题是 UI 没有更新,为了确保我理解问题,我将模型转换为视图模型并对UpVoted
属性(我正在使用 MVVM 灯光库)
bool upVoted;
public bool UpVoted
{
get { return upVoted; }
set
{
Set(ref upVoted, value);
}
}
现在它可以工作了,
所以我需要绑定UpVoted
到 UI,因此每当更改时都会更新
第一的
您的模型类必须继承自 MvxNotifyPropertyChanged
public class ImageModel : MvxNotifyPropertyChanged
{
public int Id { get; set; }
public string Name { get; set; }
private bool upVoted ;
public bool UpVoted
{
get { return upVoted ; }
set { upVoted = value; RaisePropertyChanged(() => UpVoted ); }
}
}
然后与MvxValueConverter
你准备好了
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)