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

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


请参考以下代码片段。


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?

}


吃鸡游戏
浏览 149回答 2
2回答

米脂

您应该使用AttachProperty关闭窗口。public static class Attach{&nbsp; &nbsp; #region CloseProperty&nbsp; &nbsp; public static DependencyProperty WindowCloseProperty = DependencyProperty.RegisterAttached("WindowClose",&nbsp; &nbsp; &nbsp; &nbsp; typeof(bool), typeof(Attach),&nbsp; &nbsp; &nbsp; &nbsp; new UIPropertyMetadata(false, WindowClosePropertyChangedCallback));&nbsp; &nbsp; private static void WindowClosePropertyChangedCallback(DependencyObject dependencyObject,&nbsp; &nbsp; &nbsp; &nbsp; DependencyPropertyChangedEventArgs eventArgs)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var window = (Window)dependencyObject;&nbsp; &nbsp; &nbsp; &nbsp; if (window != null && (bool)eventArgs.NewValue)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; window.Close();&nbsp; &nbsp; }&nbsp; &nbsp; public static bool GetWindowClose(DependencyObject obj)&nbsp; &nbsp; &nbsp; &nbsp; => (bool)obj.GetValue(WindowCloseProperty);&nbsp; &nbsp; public static void SetWindowClose(DependencyObject obj, bool value)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; obj.SetValue(WindowCloseProperty, value);&nbsp; &nbsp; }&nbsp; &nbsp; #endregion}并在 XAML 中<Window x:Class="MyProject.MyWindow"xmlns:helper="clr-namespace:MyProject.Helper;assembly=MyProject"&nbsp; &nbsp; helper:Attach.WindowClose="{Binding IsWindowClose}">并且在 ViewModel 当您将 IsWindowClose 设置为 true 时关闭窗口&nbsp; public bool IsWindowClose&nbsp; {&nbsp; &nbsp; &nbsp; get => _isWindowClose;&nbsp; &nbsp; &nbsp; set => SetProperty(ref _isWindowClose, value);&nbsp; }

catspeake

如何从此方法执行 onCloseCommand() ?您有多种选择,例如创建一个附加行为,在一定的超时后关闭窗口,并使用 xaml 中的行为,从而有效地将视图模型排除在外。我建议您在视图模型中对窗口的 Loaded 事件做出反应并存储窗口并在以后想要关闭它时使用它。<Window x:Class="MyWindow"&nbsp; &nbsp; &nbsp; &nbsp; xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"&nbsp; &nbsp; &nbsp; &nbsp; xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&nbsp; &nbsp; &nbsp; &nbsp; xmlns:d="http://schemas.microsoft.com/expression/blend/2008"&nbsp; &nbsp; &nbsp; &nbsp; xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"&nbsp; &nbsp; &nbsp; &nbsp; xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"&nbsp; &nbsp; &nbsp; &nbsp; mc:Ignorable="d">&nbsp; &nbsp; &nbsp;<i:Interaction.Triggers>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<i:EventTrigger EventName="Loaded">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<i:InvokeCommandAction Command="{Binding WindowLoadedCommand}" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}}"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</i:EventTrigger>&nbsp; &nbsp; &nbsp;</i:Interaction.Triggers>&nbsp; &nbsp; &nbsp;<!--- the rest of the window here ---></Window>但是让我向您推荐这个答案,以获取一种对 mvvm 更友好且更重要的是可测试的方法来从查看模型:使窗口实现一个接口并存储和使用它(不是完整的Window)!因此 WindowLoadedCommand 的类型为 DelegateCommand<IClosable> 并将 IClosable 存储在字段中。发生超时时,从字段中获取 IClosable 并调用 Close。
打开App,查看更多内容
随时随地看视频慕课网APP