有没有办法将VM的属性绑定到任何类型的单例属性(静态资源属性,单例服务中的属性......),这样我们就不需要使用IMessenger或处理SingletonServiceResolved OnPropertyChanged?
让我感觉有点脏(即使它在基类中)让每个活动来处理我的单身时钟属性的变化。
public class ClockService : ObservableObject, IClockService {
private DateTime _clock;
public DateTime Clock {
get{ return _clock;}
set { _clock = value; RaisePropertyChanged("Clock"); }
}
}
public class SomeViewModel : BaseViewModel {
private IClockService _clockService;
private IMvxMessenger _messenger;
public SomeViewModel(IClockService clockService, IMvxMessenger messenger) {
_clockService=clockService;
_messenger = messenger;
//trying to avoid
clockService.PropertyChanged += OnClockServicePropertyChanged;
}
public DateTime MyClock {
get{return _clockService.Clock;}
}
private OnClockServicePropertyChanged(...) {
if(e.PropertyName=="Clock") RaisePropertyChanged("Clock");
}
}
答案 0 :(得分:0)
绑定到单例的一种方法是通过ViewModel上的属性公开单例:
public Thing MySingleton
{
get
{
return Thing.Instance;
}
}
评论单身不是常数后更新:
您可以使用另一个常量单例作为持有者 - 然后可以实现INotifyPropertyChanged
- 例如:
public class ThingHolder : MvxNotifyPropertyChanged
{
public static readonly ThingHolder Instance = new ThingHolder();
private Thing _thing;
public Thing CurrentThing
{
get { return _thing; }
set { _thing = value; RaisePropertyChanged(() => CurrentThing); }
}
}
您的虚拟机可以使用以下属性:
public ThingHolder ThingHolder
{
get
{
return ThingHolder.Instance;
}
}
然后,您的观点可以绑定到ThingHolder.CurrentThing