Prism wfh 将实体传递给模态以获取模态视图
我将 MVVM 模式与 Prism 2.0 框架和 WPF 结合使用。我遇到了模态窗口和使用事件初始化 ViewModel 的问题。在我的模块中,我有一些代码创建一个对象,然后我想将其传递到我的 ViewModel 中,以便视图可以绑定到它的属性。
通常我会使用 EventAggregator 发布一个包含我的对象的事件,该对象可以在 ViewModel 中订阅。然而,在这种情况下,我正在创建一个新的模态窗口,因此在发布事件之前未及时创建 ViewModel 来订阅该事件。我试图避免将对象作为 DataContext 传递到 Window 或恢复到其他机制。有人有办法让它工作吗?也许有某种方法可以在调用 ShowDialog 或 Show 之前强制加载视图?
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();
I'm using the MVVM pattern with Prism 2.0 framework and WPF. I've run into a problem with a modal Window and initializing a ViewModel using Events. In my module I have some code that creates an object which I then want to pass this into my ViewModel so that the View can bind to it's properties.
Normally I'd use the EventAggregator to publish an event containing my object that can be be subscribed to in the ViewModel. However in this scenario I'm creating a new modal Window and therefore the ViewModel is not created in time to subscribe to the event before I can publish it. I'm trying to avoid passing the object in to the Window as DataContext or reverting to other mechanisms. Does anyone have a solution to get this working? Maybe some way of forcing the View to load before a call to ShowDialog or Show?
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();
My hack to make this work is as follows, but I'm wondering if there's a more elegant solution I'm missing?
var popup= new PopUpWindow();
regionManager.RegisterViewWithRegion("MyRegion", typeof(MyView));
popup.Show();
popup.Hide();
eventAggregator.GetEvent<NotifyObjectEvent>().Publish(myObject);
popup.ShowDialog();
Ok maybe I've figured it out, seems to work at least...
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();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用类似于 Ade Miller 的缓存事件聚合器的东西。此链接来自 08 年,但应该仍然有用:http://www.ademiller.com/blogs/tech/2008/11/adding-store-and-forward-support-to-the-prism-eventaggregator/< /a>
这个想法是发布事件,如果没有订阅者,则存储它直到第一个订阅者出现。
我希望这有帮助。
谢谢,
达米安
You could use something similar to Ade Miller's cached event aggregator. This link is from '08, but it should still be useful: http://www.ademiller.com/blogs/tech/2008/11/adding-store-and-forward-support-to-the-prism-eventaggregator/
The idea is to publish the event and in case there are no subscribers, store it until the first subscriber appears .
I hope this helps.
Thanks,
Damian