问题从不同的类访问属性... UI未更新[MVVM] [英] Problem Accessing Property from different class... UI is not Updating [MVVM]
本文介绍了问题从不同的类访问属性... UI未更新[MVVM]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问题从不同的类访问属性...
Problem Accessing Property from different class...
UI不是更新[MVVM]
UI is not Updating [MVVM]
1。我试图从不同的类访问IsEnabled属性。 (MVVM)
1. I am Trying to Access IsEnabled Property from Different Class. (MVVM)
public class FirstScreeenViewModel : INotifyPropertyChanged
{
TwoClassInteraction _sc = new TwoClassInteraction();
public FirstScreeenViewModel()
{
ControlEnabled = _sc.ControlEnabled;
}
private bool _controlEnabled;
public bool ControlEnabled
{
get => _controlEnabled;
set
{
_controlEnabled = value;
Selectedhandbuger = false;
OnPropertyChanged(nameof(ControlEnabled));
}
}
}
public class TwoClassInteraction : Basepropertychangedevent
{
private bool _controlEnabledproperty;
public bool ControlEnabled
{
get => _controlEnabledproperty;
set
{
if (_controlEnabledproperty == value) return;
_controlEnabledproperty = value;
OnPropertyChanged("ControlEnabled");
}
}
}
Now I am Accessing from Third Class
public class ThirdClassInteraction : Basepropertychangedevent
{
public void Test()
{
var mm = new TwoClassInteraction();
mm.ControlEnabled = false;
}
}
但是UI未更新...... IsEnabled =" {Binding ControlEnabled}"
But UI is Not Updating...... IsEnabled="{Binding ControlEnabled}"
我的要求很简单..
更改属性值来自多个班级和用户界面应该被反映
推荐答案
尝试使用Singleton以及不工作的UI不更新
Tried Singleton As well Not Working UI not Updating
public class TwoClassInteraction : Basepropertychangedevent
{
public TwoClassInteraction()
{
ControlEnabled = true;
}
private static volatile TwoClassInteraction _instance;
private static readonly object SyncRoot = new object();
public static TwoClassInteraction InstanceTwoClassInteraction
{
get
{
if (_instance != null) return _instance;
lock (SyncRoot)
{
if (_instance == null)
_instance = new TwoClassInteraction();
}
return _instance;
}
}
private bool _controlEnabledproperty;
public bool ControlEnabled
{
get => _controlEnabledproperty;
set
{
_controlEnabledproperty = value;
OnPropertyChanged("ControlEnabled");
}
}
}
public void Test()
{
TwoClassInteraction.InstanceTwoClassInteraction.ControlEnabled = false;
}
IsEnabled="{Binding ControlEnabled}"
不是他们工作..
我的要求很简单..
更改多个类和UI的属性值应该被反映
这篇关于问题从不同的类访问属性... UI未更新[MVVM]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文