How to cast KeyValuePair passed as Parameter to Command_Executed?
题
Seemingly simple concept but can't get past this.
I have a Command...the _Executed method receives a KeyValuePair (types don't matter) as it's Parameter.
myCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
KeyValuePair<System.Type, MyCustomArgs> kvp = e.Parameter as KeyValuePair<Type, MyCustomArgs>;
:
:
:
}
Can't do that as it's non-nullable. How do I accomplish this? I want to extract the KeyValuePair from e.Parameter.
Appreciate any insight and will happily post more code/information if necessary.
解决方案
You must use an explicit cast, rather than an implicit one, as you have done.
Implicit cast:
KeyValuePair<System.Type, MyCustomArgs> kvp = e.Parameter as KeyValuePair<Type, MyCustomArgs>;
Explicit cast:
KeyValuePair<System.Type, MyCustomArgs> kvp = (KeyValuePair<System.Type, MyCustomArgs>)e.Parameter;
不隶属于 StackOverflow