2

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.

H.B.
  • 166,899
  • 29
  • 327
  • 400
tronious
  • 1,547
  • 2
  • 28
  • 45
  • I don't get what you're trying to put into the KVP. What is in e.Parameter? – Kittoes0124 Sep 19 '12 at 01:24
  • e.Parameter contains a combination of System.Type, CustomEventArgs. I have multiple view_models which post back to a Main View model. I have a Button which has Command="NextPage" CommandParameter="{Binding NextPageToLoad}". NextPageToLoad returns a KeyValuePair that reads as KeyValuePair. I need to extract the KeyValuePair from e.Parameter in the above Method. – tronious Sep 19 '12 at 01:25
  • (KeyValuePair)e.Parameter? – Kittoes0124 Sep 19 '12 at 01:36
  • I figured it out. Two things... 1. KeyValuePaur is a struct. I made it nullable. 2. The casting you posted above USIA correct. I could not use an "as" – tronious Sep 19 '12 at 02:17

1 Answers1

9

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; 
dylansweb
  • 674
  • 4
  • 8
  • 3
    Just a little note. They both are explicit. Implicit meas you dont write any syntax and cast happens automatically. Eg when going from lower precision to higher. (int b = 4; float c = b;) – Erti-Chris Eelmaa Sep 19 '12 at 04:42