自定义命令(Command)

当控件样式和控件应用不在同一个文件时,也就是说当样式写到一个独立的文件时;

如果需要在控件样式里触发某些事件,这事就需要绑定自定义的命令处理。

自定义命令代码如下:

 1  
 2 
 3 using System;
 4 using System.Collections.Generic;
 5 using System.Linq;
 6 using System.Text;
 7 using System.Windows.Input;
 8 using System.Diagnostics.CodeAnalysis;
 9 
10 namespace Medcare_V5.Teaching.Common.BaseTypes
11 {
12     public class MyCommad<T1, T2> : ICommand
13     {
14         private Func<T1, bool> canExecuteMethod;
15         private Action<T2> executeMethod;
16 
17         public MyCommad(Func<T1, bool> canExecuteMethod, Action<T2> executeMethod)
18         {
19             this.executeMethod = executeMethod;
20             this.canExecuteMethod = canExecuteMethod;
21         }
22 
23         public MyCommad(Action<T2> executeMethod)
24         {
25             this.executeMethod = executeMethod;
26             this.canExecuteMethod = (x) => { return true; };
27         }
28 
29         public bool CanExecute(T1 parameter)
30         {
31             if (canExecuteMethod == null) return true;
32             return canExecuteMethod(parameter);
33         }
34 
35         public void Execute(T2 parameter)
36         {
37             if (executeMethod != null)
38             {
39                 executeMethod(parameter);
40             }
41         }
42 
43         public bool CanExecute(object parameter)
44         {
45             return CanExecute((T1)parameter);
46         }
47 
48         public void Execute(object parameter)
49         {
50             Execute((T2)parameter);
51         }
52         /// <summary>
53         /// Occurs when changes occur that affect whether the command should execute.
54         /// </summary>
55         public event EventHandler CanExecuteChanged
56         {
57             add
58             {
59                 if (canExecuteMethod != null)
60                 {
61                     CommandManager.RequerySuggested += value;
62                 }
63             }
64 
65             remove
66             {
67                 if (canExecuteMethod != null)
68                 {
69                     CommandManager.RequerySuggested -= value;
70                 }
71             }
72         }
73     }
74 }
View Code

 

转载于:https://www.cnblogs.com/fengxingblog/p/fengxing2014110160331.html

在PowerShell中,可进行多种类型的自定义,以下是不同方面的自定义命令方法和使用指南: ### 自定义命令行提示符 可以通过定义`prompt`函数来实现。例如,将命令行提示符设置为显示计算机名: ```powershell function prompt { "PS [$env:COMPUTERNAME] >" } ``` 此函数重新定义了命令行提示符,每次显示提示符时会展示当前计算机名 [^3]。 ### 自定义用户输入方式 通过定义`PSConsoleHostReadLine`函数来自定义用户输入。以下为简单示例: ```powershell function PSConsoleHostReadLine { # 这里可以添加自定义输入逻辑 $input = Read-Host "请输入内容" return $input } ``` 在这个函数里,可以添加自定义的输入逻辑,像添加提示信息、验证输入等 [^3]。 ### 自定义命令解析行为 可通过设置`$executionContext.SessionState.InvokeCommand.CommandNotFoundAction`来自定义命令解析。例如: ```powershell $executionContext.SessionState.InvokeCommand.CommandNotFoundAction = { param($commandName, $commandLookupEventArgs) Write-Host "你输入的命令 $commandName 未找到,请检查。" } ``` 当输入的命令不存在时,会执行此自定义操作,给出相应提示 [^3]。 ### 查找命令 使用`Get-Command`命令查找特定命令。例如,查找使用`Get`动词的命令: ```powershell Get-Command -Verb Get ``` 这有助于在众多命令中找到所需命令 [^3]。 ### 获取命令帮助 PowerShell 2.0 提供了丰富的帮助文档,使用`Get-Help`命令能获取各种命令和概念的详细信息。例如,要了解`Get-Service`命令的使用方法,可在 PowerShell 中输入: ```powershell Get-Help Get-Service ``` 通过这种方式可以学习命令的具体使用 [^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值