【发布时间】:2013-10-09 02:28:00
【问题描述】:
我有一个 WPF 窗口,datacontext 使用 XAML 实例化
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ViewModels="clr-namespace:Contratos.ViewModels" x:Class="Contratos.Views.TipoAsociadoAcopio"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF45"
xmlns:l="clr-namespace:Recursos;assembly=Recursos"
xmlns:controls="clr-namespace:Recursos.Controls;assembly=Recursos"
xmlns:resources="clr-namespace:ModelSeguridad.Resources;assembly=ModelSeguridad"
Title="{x:Static resources:Labels.CONTRATO_TipoContratoAcopio}" Height="Auto" Width="Auto">
<Window.Resources>
<CollectionViewSource x:Key="ListaItems" Source="{Binding ListaItems}"/>
<ViewModels:TipoAsociadoVM x:Key="ViewDataContext"/>
</Window.Resources>
<Grid>
<StackPanel>
<Button Command="{Binding _ICommandExit}" CommandParameter="{W H A T H E R E}" />
</StackPanel>
</Grid>
当用户单击退出按钮时,我需要关闭此窗口,我的问题是,如果它是使用 XAML 实例化的,我该如何将 Window 引用发送到 viewmodel。
我正在尝试维护 MVVM 模式,那是因为我在 mi codebehind 上没有任何代码。
【问题讨论】:
-
就个人而言,我认为将按钮单击处理程序放在后面的代码中而不是绑定到命令是完全可以接受的。关闭窗口纯粹是一个 UI 任务,因此您仍然保持视图逻辑解耦。
-
@zmb Tks,是的,你有原因。但我喜欢为我所有的 Windows 自动执行此任务,而且当用户关闭窗口时必须捕获一些日志。
-
那不是 MVVM。只需在 UI 中关闭窗口即可。
-
很遗憾,那些用户决定关闭这个问题,说这是另一个帖子的重复,因为如果他们真的阅读了这两个帖子,他们都会看到他们是错误。这个问题不是关于像其他问题一样关闭
MainWindow,因此显示如何关闭MainWindow的答案不回答了这个问题。