0
问题已在其他线程中得到解答,但我没有得到完整答案。我做了一些四处走动和工作,但我对此并不满意。 所以我问如何在MVVM模式中做到这一点。如何从后台线程上升PropertyChanged
我在View中的文本框中更新值时遇到了令人沮丧的问题。 Proprerty消息正在从我的回调中更新,但不显示在GUI中。属性正确绑定。现在我可以做到这一点。原因是我没有提高UI线程上的PropertyChanged,因为我从回调调用它。我正在尝试,但没有任何工作。如何在UI线程上引发PropertyChange事件?
编辑再次刷新CODE:
namespace test
{
public class MainViewModel : INotifyPropertyChanged, IDataExchangeCallback
{
public MainViewModel()
{
Message = "TEST1";
}
void IDataExchangeCallback.Result(string result)
{
Message += result;
}
public void Register()
{
InstanceContext instanceContext = new InstanceContext(new MainViewModel());
DataExchangeClient client = new DataExchangeClient(instanceContext);
client.RegisterClient(Guid.NewGuid().ToString());
}
private string _message;
public string Message
{
get { return this._message; }
set
{
if (this._message != value)
{
this._message = value;
OnPropertyChanged("Message");
}
}
}
public event PropertyChangedEventHandler PropertyChanged; //ALWAYS NULL!!! BUT I HAVE INotifyPropertyChanged implement
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
Application.Current.Dispatcher.Invoke(() =>
handler(this, new PropertyChangedEventArgs(name)));
}
}
}
}
我PropertChange总是空。为什么?当我使用绑定时,它应该订阅PropertyChanged。
编辑 我仍然需要帮助
我不是附近的VS,但现在确实有部分标示出的解决方案:(() Application.Current.Dispatcher.Invoke((动作)=> {处理程序(这一点,新PropertyChangedEventArgs(propertyName的))}); –
这是**不**工作,我不知道为什么 – UserBuzzer
不太确定,但它可能是你传递一个新的MainViewModel实例到:InstanceContext?你尝试过传递:InstanceContext instanceContext = new InstanceContext(this); – rauland