wpf prism (CAL) - 当 ViewModel 对视图一无所知时关闭弹出区域中的窗口
我们的 shell 的 Window 标签中有一个区域,向该区域添加内容会弹出另一个窗口。
<Window x:Class="GTS.GRS.N3.Shell.Shell1"
--removed namespace references for clarity
cal:RegionManager.RegionName="{x:Static Constants:RegionNames.WindowRegion}">
我们将 ViewModel 添加到区域管理器,然后通过数据上下文附加视图,以便 ViewModel 对视图一无所知,即
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<DataTemplate DataType="{x:Type Model:CommunicationViewModel}">
<v:CommunicationView />
</DataTemplate>
</ResourceDictionary>
我的问题是如何关闭弹出窗口,我尝试从 RegionManager 中删除 ViewModel -但这例外......视图是一个用户控件,但我需要关闭它的所有者,这是由区域打开的新窗口。我真的不想通过 ViewModel 的 DataContext 来破解它。
有人可以帮忙吗?
We have a Region in the Window tag of our shell, adding things to this region pops out another Window.
<Window x:Class="GTS.GRS.N3.Shell.Shell1"
--removed namespace references for clarity
cal:RegionManager.RegionName="{x:Static Constants:RegionNames.WindowRegion}">
We're adding ViewModels to the Region Manager and then the View is attached via a data context so that the ViewModel knows nothing about the View i.e.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<DataTemplate DataType="{x:Type Model:CommunicationViewModel}">
<v:CommunicationView />
</DataTemplate>
</ResourceDictionary>
My question is how do I close the Pop up Window, I tried removing the ViewModel from the RegionManager - but this exceptions ... the View is a UserControl, but I need to close its Owner which is a new Window opened by the Region. I don't really want to have to hack it via the DataContext of the ViewModel.
Can anyone assist please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
安迪,
我自己花了很长时间才弄清楚这个问题。
完成此操作的最简洁方法是使用 DelegateCommand(或 RelayCommand)并在使用 window.Show() 创建窗口的代码中添加事件处理程序。
然后,我使用复合事件来通知窗口的 ViewModel(而不是 UserControl 的)它有关闭请求。然后,为复合事件分配的订阅处理程序调用
this.OnRequestClose()
。在 ViewModel 的构造函数中:
在 ViewModel 的主体中:
请参阅 Josh Smith 在 MSDN 上关于在 WPF 中使用 MV-VM 模式的优秀文章,网址为 http://msdn.microsoft.com/en-us/magazine/dd419663.aspx 了解更多信息。
Andy,
It took me quite awhile to figure this one out myself.
The cleanest way to accomplish this is by using the DelegateCommand (or RelayCommand) and adding an event handler in the code that creates the window with window.Show().
I then use a Composite Event to notify the window's ViewModel (not the UserControl's) that it has a request to close. The assigned subscription handler for the composite event then calls
this.OnRequestClose()
.In the Constructor for the ViewModel:
In the body of the ViewModel:
See Josh Smith's excellent article on MSDN about using the M-V-VM pattern with WPF at http://msdn.microsoft.com/en-us/magazine/dd419663.aspx for more information.
_regionManager.Regions[RegionNames.PopupRegion].Deactivate(_regionManager.Regions[RegionNames.PopupRegion].ActiveViews.FirstOrDefault());
_regionManager.Regions[RegionNames.PopupRegion].Deactivate(_regionManager.Regions[RegionNames.PopupRegion].ActiveViews.FirstOrDefault());