簡體   English   中英

WPF UI不從ViewModel / Caliburn.Micro更新

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM