如何将KeyValuePair作为参数传递给Command_Executed?

tro*_*ous 2 .net c# wpf command eventargs

看似简单的概念却无法超越这个.

我有一个命令... _Executed方法接收KeyValuePair(类型无关紧要),因为它是参数.

myCommand_Executed(object sender, ExecutedRoutedEventArgs e)
    {
        KeyValuePair<System.Type, MyCustomArgs> kvp = e.Parameter as KeyValuePair<Type, MyCustomArgs>;
:
:
:
}
Run Code Online (Sandbox Code Playgroud)

不能这样做,因为它不可空.我该如何做到这一点?我想从e.Parameter中提取KeyValuePair.

欣赏任何见解,并在必要时愉快地发布更多代码/信息.

dyl*_*web 7

您必须使用显式强制转换而不是隐式强制转换.
隐式演员:

KeyValuePair<System.Type, MyCustomArgs> kvp = e.Parameter as KeyValuePair<Type, MyCustomArgs>; 
Run Code Online (Sandbox Code Playgroud)

明确的演员:

KeyValuePair<System.Type, MyCustomArgs> kvp = (KeyValuePair<System.Type, MyCustomArgs>)e.Parameter; 
Run Code Online (Sandbox Code Playgroud)

  • 只是一点点说明.它们都是明确的.隐式测量你不会写任何语法和演员自动发生.例如,从较低精度到较高精度.(int b = 4; float c = b;) (2认同)