简体   繁体   English

WPF UI不从ViewModel / Caliburn.Micro更新

[英]WPF UI Not updating from ViewModel / Caliburn.Micro

I have a caliburn Micro application, in which I'm loading several data tables in the bootstrapper. 我有一个caliburn Micro应用程序,其中在引导程序中加载了几个数据表。 I'm injecting this into MEF for use later in the application. 我将其注入到MEF中以供以后在应用程序中使用。

I want to show the user with a splash screen of the progress. 我想向用户显示进度的初始屏幕。 This is all working fine so far. 到目前为止,一切正常。 I'm using the EventAggregator to have the individual classes throw messages to the Splash Screen ViewModel, however the UI thread seems not the working to update my text label. 我正在使用EventAggregator让各个类向启动画面ViewModel抛出消息,但是UI线程似乎无法更新我的文本标签。 My property is set fine, and i'm calling the NotifyOfPropertyChange, however my window seems to be frozen, the get ancestor on my bound property is not being called. 我的属性设置良好,正在调用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;
    }

The Handle comes from the Caliburn.Micro IHandle EventAggregrator. 该句柄来自Caliburn.Micro IHandle EventAggregrator。 The data is loaded in the bootstrapper, I assume this thread is seperate from the UI Thread, but i'm not personally creating any threads. 数据加载到引导程序中,我假设此线程与UI线程是分开的,但我个人没有创建任何线程。

In my bootstrapper I load as follows: 在我的引导程序中,我加载如下:

events.Publish(new StartupNotification("Loading Feeds..."));
batch.AddExportedValue<IFeedService>(new FeedService());

I have tried to trigger the UI by indirect measures, but for some reason my main screen stays frozen. 我试图通过间接措施来触发UI,但是由于某种原因,我的主屏幕保持冻结。 Can anyone indicate what I'm doing wrong? 谁能指出我做错了吗?

Best Regards, Martin 最好的问候,马丁

The bootstrapper runs in the UI thread, I assume you have soemthing like this: 引导程序在UI线程中运行,我假设您有这样的事情:

 protected override void OnStartup(object sender, System.Windows.StartupEventArgs e)
 {
     DisplayRootViewFor<ShellViewModel>();
     RunLongLoadingOperation();
 }

You should do something like 你应该做类似的事情

 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
 }

This is now solved, I have moved my DB loads into a background worker, and added Thread.Sleep in there to allow some time for the UI to update on the ProgressChanged events. 现在,此问题已解决,我将数据库负载移至后台工作器中,并在其中添加了Thread.Sleep,以便为UI提供一些时间来更新ProgressChanged事件。

I expected .NET to take care of this since I moved it to a background worker already, but oh well. 我希望.NET能够解决这个问题,因为我已经将它移到了后台工作人员中,但是哦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM