我正在爲我的WPF應用程序使用MVVM/PRISM/MEF。它有一個包含多條記錄的DataGrid,當雙擊一行時,一個單獨的視圖被添加到帶有多個控件的區域,初始化控件對於新屏幕大約需要10秒,這就是爲什麼我想在該過程中顯示RadBusyIndicator時間。RadBusyIndicator沒有顯示來自ViewModel的PRISM/MEF/WPF
在XAML
<!-- This is Main View -->
<!-- Module: MainModule, ViewModel: MainViewViewModel -->
<telerik:RadBusyIndicator IsBusy="{Binding IsBusy}" BusyContent="{Binding BusyContent}">
<!-- All PRISM regions are here -->
</telerik:RadBusyIndicator>
視圖模型以下是
class MainViewViewModel : ViewModelBase
{
ImportingConstructor]
public MainViewViewModel(IEventAggregator eventAggregator, IRegionManager regionManager, IServiceLocator serviceLocator)
:base(eventAggregator, regionManager, serviceLocator)
{
eventAggregator.GetEvent<BusyStateChangedEvent>().Subscribe(OnBusyStateChanged,ThreadOption.BackgroundThread);
}
#region BusyStateChanged
private void OnBusyStateChanged(bool newState)
{
IsBusy = newState;
}
#endregion
}
而且在當DataGrid行被雙擊ViewModelBase函數被調用,如下
public class ViewModelBase
{
private NavigationItem global_navItem = null;
public virtual void OnNavigationItemChanged(NavigationItem item)
{
changeNav = true;
global_navItem = item;
//Firing event to change the state
EventAggregator.GetEvent<BusyStateChangedEvent>().Publish(true);
//Using BackgroundWorker, but its not showing any Busy Indicator as well
var bw = new BackgroundWorker();
bw.DoWork += bw_DoWork;
bw.RunWorkerCompleted += bw_RunWorkerCompleted;
bw.RunWorkerAsync();
}
void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//Setting busy indicator to false
EventAggregator.GetEvent<BusyStateChangedEvent>().Publish(false);
}
void bw_DoWork(object sender, DoWorkEventArgs e)
{
//DisplayView function is taking too long
if (global_navItem != null) this.DisplayView(global_navItem);
}
}
public void DisplayView(NavigationItem item)
{
try
{
//This call is taking long as it initializes the View
MyCustomeUserControl view = this.ServiceLocator.GetInstance<MyCustomeUserControl>(item.viewName);
view.Region = this.Region;
}catch(Exception e)
{
}
}
其他視圖事件正在被正確觸發並且視圖顯示正確,但是我的問題是繁忙指示器根本沒有顯示,當我雙擊DataGrid行時,GUI變得沒有響應,一段時間後出現新的視圖。我懷疑這是GUI線程繁忙的問題,但我能做些什麼來避免這種情況,我已經使用BackgroudWorker
了?
編輯
1 - 我養的PropertyChanged事件IsBusy屬性。並且我已經嘗試了Thread in event訂閱中的所有選項。即Thread.BackgroundThread
,Thread.UIThread
和Thread.PublisherThread
。但沒有變化。
2-我已經測試Thread.Sleep
寧可DisplayView
在bw_DoWork
,其示出正常RadBusyIndicator
,所以這意味着GUI控件在GUI線程被初始化,不管我已經創建了它BackgroundWorker
。
您發佈的代碼似乎沒問題。只是一個愚蠢的問題(我的意思是沒有傷害)您是否爲IsBusy屬性提出PropertyChanged事件?我只需要問...還有一件事... [這裏](http://msdn.microsoft.com/en-us/library/ff921122(v = pandp.20).aspx)它說* If你需要能夠在接收到事件時更新用戶界面元素,訂閱在用戶界面線程上接收事件。*如果我是你,我會在ThreadOption.UIThread訂閱BusyStateChangedEvent時切換ThreadOption.BackgroundThread。但這些只是想法... – 2013-02-13 22:18:14
謝謝@ViktorLaCroix,是的我正在提高'IsBusy'屬性的PropertyChanged事件。並且我已經嘗試了Thread in event訂閱中的所有選項。即'Thread.BackgroundThread','Thread.UIThread'和'Thread.PublisherThread'。但沒有變化。 – 2013-02-14 06:16:14
現在它變得有趣:)所以,請你用這些額外的信息編輯你的問題?並且請不要忘記,當您切換DoWork方法的主體時,一切正常。Thread.Sleep – 2013-02-14 08:31:50