2014-03-12 78 views
0

我有一個caliburn Micro應用程序,其中我正在引導程序中加載幾個數據表。 我正在將其注入到MEF中以便稍後在應用程序中使用。WPF UI不從ViewModel/Caliburn.Micro更新

我想向用戶顯示進度的啓動畫面。到目前爲止,這一切都工作正常。 我使用EventAggregator讓各個類將消息投擲到Splash Screen ViewModel,但UI線程似乎不是工作來更新我的文本標籤。我的屬性設置得很好,我打電話給NotifyOfPropertyChange,但是我的窗口似乎被凍結,我的綁定屬性上的祖先沒有被調用。

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; 
    } 

Handle來自Caliburn.Micro IHandle EventAggregrator。數據被加載到引導程序中,我假設這個線程與UI線程是分離的,但我並不親自創建任何線程。

在我的引導程序我加載如下:

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

我試圖觸發通過間接措施的UI,但由於某些原因,我的主屏幕保持凍結。 任何人都可以指出我做錯了什麼?

最好的問候, 馬丁

+0

顯示更多的代碼。消息如何設置?您的視圖中的綁定設置如何 –

+0

您是否將數據加載到單獨的線程中? – dkozl

+0

已更新,問題。 – Martin

回答

0

引導程序的UI線程中運行,我認爲你有soemthing這樣的:

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 
} 
+0

問題是主模型中的幾個ViewModel注入了我試圖在我的啓動畫面中加載的數據。這就是爲什麼我一直在我的主循環中加載這個。 – Martin

+0

我通常使用的策略是讓啓動屏幕加載過程中的最後一項執行某種「LoadMainView()」,以便在開始移動之前,在後臺線程中完成所有事情並完成所有操作。 –

+0

我正在做同樣的事情,現在在我的主線程中,但UI仍然不會更新。消息=「正在加載Feeds ...」; IoC.Get ()。LoadItems(); – Martin

-2

這現在已經解決了,我已將我的數據庫加載移動到後臺工作器中,並在其中添加了Thread.Sleep以允許UI在ProgressChanged事件上進行更新。

我希望.NET能夠照顧這個,因爲我已經把它移到了後臺工作者,但是哦。