HandyControl 使用内置Command 执行无效问题

blog-hbh-hc-header
HandyControl 中通过查阅代码HandyControl_Shared 共享项目中,Interactivity/Commands 目录下,存在着一些内置 Command,开心发现还有关闭窗体,最小化等系统级别常用命令。

CloseWindowCommand.cs
ControlCommands.cs
OpenLinkCommand.cs
PushMainWindow2TopCommand.cs
ScreenshotCommand.cs
ShutdownAppCommand.cs
StartScreenshotCommand.cs

其中主要是在ControlCommands.cs 中。由于是静态属性,所以可以直接在xaml中使用,已关闭窗体为例。

/// <summary>
///     控件库使用的所有命令(为了统一,不使用wpf自带的命令)
/// </summary>
public static class ControlCommands
{
	/// <summary>
    ///     关闭窗口
    /// </summary>
    public static CloseWindowCommand CloseWindow { get; } = new();
}

CloseWindowCommand 代码实现如下:

public class CloseWindowCommand : ICommand
{
    public bool CanExecute(object parameter) => true;

    public void Execute(object parameter)
    {
        if (parameter is DependencyObject dependencyObject)
        {
            if (Window.GetWindow(dependencyObject) is { } window)
            {
                window.Close();
            }
        }
    }

    public event EventHandler CanExecuteChanged;
}

页面按钮使用:

<Button Style="{StaticResource CloseButtonIcon}" 
        Command="{x:Static hc:ControlCommands.CloseWindow}" 
        Padding="10,0" hc:IconElement.Geometry="{StaticResource CloseGeometry}"
            ToolTip="关闭">

运行项目之后,点击按钮并没有触发对应的命令。通过查阅Github 中的issue可以查到如下信息:https://github.com/HandyOrg/HandyControl/issues/687
解决办法是添加对应的命令参数CommandParameter

<Button Style="{StaticResource CloseButtonIcon}" 
        Command="{x:Static hc:ControlCommands.CloseWindow}" 
        CommandParameter="{Binding RelativeSource={RelativeSource Self}}" 
        Padding="10,0" hc:IconElement.Geometry="{StaticResource CloseGeometry}"
            ToolTip="关闭">

实际并不是执行无效,只是函数Execute(object parameter)代码中存在类型判定,未指定参数情况时,代码内部并不会执行目标逻辑。

  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值