[英]WPF UI Not updating from ViewModel / Caliburn.Micro
我有一个caliburn Micro应用程序,其中在引导程序中加载了几个数据表。 我将其注入到MEF中以供以后在应用程序中使用。
我想向用户显示进度的初始屏幕。 到目前为止,一切正常。 我正在使用EventAggregator让各个类向启动画面ViewModel抛出消息,但是UI线程似乎无法更新我的文本标签。 我的属性设置良好,正在调用NotifyOfPropertyChange,但是我的窗口似乎被冻结,未调用绑定属性的get祖先。
public string Message
{
get
{
return this.message;
}
set
{
this.message = value;
//new System.Action(() => NotifyOfPropertyChange(() => Message)).BeginOnUIThread();
//App.Current.Dispatcher.Invoke(() => NotifyOfPropertyChange("Message"));
NotifyOfPropertyChange(() => Message);
}
}
public void Handle(StartupNotification message)
{
Message = message.Message;
}
该句柄来自Caliburn.Micro IHandle EventAggregrator。 数据加载到引导程序中,我假设此线程与UI线程是分开的,但我个人没有创建任何线程。
在我的引导程序中,我加载如下:
events.Publish(new StartupNotification("Loading Feeds..."));
batch.AddExportedValue<IFeedService>(new FeedService());
我试图通过间接措施来触发UI,但是由于某种原因,我的主屏幕保持冻结。 谁能指出我做错了吗?
最好的问候,马丁
引导程序在UI线程中运行,我假设您有这样的事情:
protected override void OnStartup(object sender, System.Windows.StartupEventArgs e)
{
DisplayRootViewFor<ShellViewModel>();
RunLongLoadingOperation();
}
你应该做类似的事情
protected override void OnStartup(object sender, System.Windows.StartupEventArgs e)
{
DisplayRootViewFor<ShellViewModel>();
var task = Task.Factory.StartNew(RunLongLoadingOperation);
// task error handling omitted,
// if your long operation can throw an exception you should
// add an oncompleted handler and do something with it or the
// app will be brought down when the task gets GC'd
}
现在,此问题已解决,我将数据库负载移至后台工作器中,并在其中添加了Thread.Sleep,以便为UI提供一些时间来更新ProgressChanged事件。
我希望.NET能够解决这个问题,因为我已经将它移到了后台工作人员中,但是哦。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.