我使用Prism 2.0框架和WPF的MVVM模式。我遇到了模式窗口的問題,並使用事件初始化ViewModel。在我的模塊中,我有一些代碼創建了一個對象,然後我想將它傳遞給ViewModel,以便View可以綁定到它的屬性。棱鏡wfh通過實體模態爲模態查看
通常我會使用EventAggregator來發佈一個包含可以在ViewModel中訂閱的對象的事件。但是,在這種情況下,我創建了一個新的模式窗口,因此ViewModel沒有及時創建,因此無法在發佈之前訂閱該事件。我試圖避免將對象作爲DataContext傳遞到Window或恢復到其他機制。有沒有人有解決這個問題的方法?在調用ShowDialog或Show之前,可能需要強制View加載?
var popup= new PopUpWindow();
regionManager.RegisterViewWithRegion("MyRegion", typeof(MyView));
eventAggregator.GetEvent<NotifyObjectEvent>().Publish(myObject);
// ViewModel only created and subscribes to event when the line below is run
popup.ShowDialog();
我的黑客做這項工作如下,但我想知道是否有一個更優雅的解決方案,我錯過了?
var popup= new PopUpWindow();
regionManager.RegisterViewWithRegion("MyRegion", typeof(MyView));
popup.Show();
popup.Hide();
eventAggregator.GetEvent<NotifyObjectEvent>().Publish(myObject);
popup.ShowDialog();
好吧,也許我已經想通了,似乎至少要工作...
var popup= new PopUpWindow();
regionManager.RegisterViewWithRegion("MyRegion", typeof(MyView));
RegionManager.SetRegionManager(popup, regionManager);
regionManager.AddToRegion("MyRegion", typeof(MyView));
eventAggregator.GetEvent<NotifyObjectEvent>().Publish(myObject);
popup.ShowDialog();
有趣的我竟以爲實現這樣的事情我自己,但決定不予採納。好的建議。 – TheCodeKing 2010-09-21 12:29:39