为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
吃鸡游戏 2023-12-17 10:34:27
我想从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 buttonCommand = "{Binding CloseCommand}" CommandParameter="{Binding ElementName=Recorder}"视图模型代码:CloseCommand = new DelegateCommand<object>(helper.onCloseCommand);ViewModelHelper代码:Note: onCloseCommand() methodis working as per expectationonCloseCommand(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?}
查看完整描述

2 回答

?
米脂

TA贡献1836条经验 获得超3个赞

您应该使用AttachProperty关闭窗口。


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 时关闭窗口


  public bool IsWindowClose

  {

      get => _isWindowClose;

      set => SetProperty(ref _isWindowClose, value);

  }


查看完整回答
反对 回复 2023-12-17
?
catspeake

TA贡献1111条经验 获得超0个赞

如何从此方法执行 onCloseCommand() ?


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


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


<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)!


因此 WindowLoadedCommand 的类型为 DelegateCommand<IClosable> 并将 IClosable 存储在字段中。发生超时时,从字段中获取 IClosable 并调用 Close。


查看完整回答
反对 回复 2023-12-17
  • 2 回答
  • 0 关注
  • 69 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信