本文主要是介绍HandyControl 使用内置Command 执行无效问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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)
代码中存在类型判定,未指定参数情况时,代码内部并不会执行目标逻辑。
这篇关于HandyControl 使用内置Command 执行无效问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!