wpf prism (CAL) - 当 ViewModel 对视图一无所知时关闭弹出区域中的窗口

发布于 2024-08-26 08:22:50 字数 761 浏览 4 评论 0原文

我们的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吃兔兔 2024-09-02 08:22:50

安迪,

我自己花了很长时间才弄清楚这个问题。

完成此操作的最简洁方法是使用 DelegateCommand(或 RelayCommand)并在使用 window.Show() 创建窗口的代码中添加事件处理程序。

// Define the View
Shell window = Container.Resolve<Shell>();

// Define the ViewModel
ShellViewModel windowVM = Container.Resolve<ShellViewModel>();

// When the ViewModel asks to be closed, close the View.
EventHandler handler = null;
handler = (sender, e) =>
{
    windowVM.RequestClose -= handler;
    window.Close();
};
windowVM.RequestClose += handler;

// Set the ViewModel as the DataContext of the View
window.DataContext = windowVM;

// Display the View
window.Show();

然后,我使用复合事件来通知窗口的 ViewModel(而不是 UserControl 的)它有关闭请求。然后,为复合事件分配的订阅处理程序调用 this.OnRequestClose()

在 ViewModel 的构造函数中:

//subscribe to composite events
_eventAggregator.GetEvent<WindowCloseEvent>().Subscribe(WindowClose);

在 ViewModel 的主体中:

/// <summary>
/// Private Event handler for WindowCloseEvent.
/// </summary>
private void WindowClose(bool value)
{
    // Close the View
    this.OnRequestClose();
}

请参阅 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().

// Define the View
Shell window = Container.Resolve<Shell>();

// Define the ViewModel
ShellViewModel windowVM = Container.Resolve<ShellViewModel>();

// When the ViewModel asks to be closed, close the View.
EventHandler handler = null;
handler = (sender, e) =>
{
    windowVM.RequestClose -= handler;
    window.Close();
};
windowVM.RequestClose += handler;

// Set the ViewModel as the DataContext of the View
window.DataContext = windowVM;

// Display the View
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:

//subscribe to composite events
_eventAggregator.GetEvent<WindowCloseEvent>().Subscribe(WindowClose);

In the body of the ViewModel:

/// <summary>
/// Private Event handler for WindowCloseEvent.
/// </summary>
private void WindowClose(bool value)
{
    // Close the View
    this.OnRequestClose();
}

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.

岁月静好 2024-09-02 08:22:50

_regionManager.Regions[RegionNames.PopupRegion].Deactivate(_regionManager.Regions[RegionNames.PopupRegion].ActiveViews.FirstOrDefault());

_regionManager.Regions[RegionNames.PopupRegion].Deactivate(_regionManager.Regions[RegionNames.PopupRegion].ActiveViews.FirstOrDefault());

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文