如何从 C# 中的 ViewModelHelper 关闭 window(XAML)?

How do I close a window(XAML) from the ViewModelHelper in c#?

我想执行 onCloseCommand(object sender) 方法形式的 onTimeOutCommand() 方法,但我不知道如何传递从该方法传递的所需参数?

请参考以下代码片段。

XAML代码:

x:name = "Recorder" // window name define in the begining


//below command is used for closing this window when user clicks on close button

Command = "{Binding CloseCommand}" CommandParameter="{Binding ElementName=Recorder}"

视图模型代码:

CloseCommand = new DelegateCommand<object>(helper.onCloseCommand);

ViewModelHelper 代码:

Note: onCloseCommand() methodis working as per expectation
onCloseCommand(object sender) // This method is used for closing the window on clicking on close button of this window
{
    if(sender != null && send is window)
    {
         (sender as window).close();
    }
}

onTimeOutCommand() // this method is used for closing the window (the window which is passed in onCloseCommand() method) automaticlly after time out of the recording
{
      how to perform onCloseCommand() from this method?
}

how to perform onCloseCommand() from this method?

您有多种选择,例如创建一个附加行为,在一定超时后关闭 window 并使用 xaml 中的行为,有效地将视图模型排除在方程之外。

我建议你在视图模型中对 window 的 Loaded 事件做出反应并存储 window 并在以后要关闭它时使用它。

<Window x:Class="MyWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        mc:Ignorable="d">
     <i:Interaction.Triggers>
         <i:EventTrigger EventName="Loaded">
             <i:InvokeCommandAction Command="{Binding WindowLoadedCommand}" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}}"/>
         </i:EventTrigger>
     </i:Interaction.Triggers>

     <!--- the rest of the window here --->
</Window>

但是让我推荐你参考 以获得更 mvvm 友好,更重要的是,从视图模型关闭 window 的可测试方法:

使 window 实现一个接口并存储和使用它(不是完整的 Window)!

所以 WindowLoadedCommandDelegateCommand<IClosable> 类型并将 IClosable 存储在一个字段中。当超时发生时,从字段中获取 IClosable 并调用 Close.

您应该使用 AttachProperty 关闭您的 window。

public static class Attach
{
    #region CloseProperty
    public static DependencyProperty WindowCloseProperty = DependencyProperty.RegisterAttached("WindowClose",
        typeof(bool), typeof(Attach),
        new UIPropertyMetadata(false, WindowClosePropertyChangedCallback));

    private static void WindowClosePropertyChangedCallback(DependencyObject dependencyObject,
        DependencyPropertyChangedEventArgs eventArgs)
    {
        var window = (Window)dependencyObject;
        if (window != null && (bool)eventArgs.NewValue)
            window.Close();
    }

    public static bool GetWindowClose(DependencyObject obj)
        => (bool)obj.GetValue(WindowCloseProperty);

    public static void SetWindowClose(DependencyObject obj, bool value)
    {
        obj.SetValue(WindowCloseProperty, value);
    }

    #endregion
}

并在 XAML

<Window x:Class="MyProject.MyWindow"
xmlns:helper="clr-namespace:MyProject.Helper;assembly=MyProject"
    helper:Attach.WindowClose="{Binding IsWindowClose}">

并且在 ViewModel 中,当您将 IsWindowClose 设置为 true 时,您的 Window 关闭

  public bool IsWindowClose
  {
      get => _isWindowClose;
      set => SetProperty(ref _isWindowClose, value);
  }