user2623966
user2623966

Reputation: 13

EventHandler with a different type of sender

I'm trying to implement drag and drop for a specific object of a Type that I've created in c# for windows phone 8. I'm using Manipulation Events like this :

deck[r[i, j]].card.ManipulationCompleted += new EventHandler<ManipulationCompletedEventArgs>(ImageManipulationCompleted);

private void ImageManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
{
    //something
}

How can I change object to the type that I want ?

Upvotes: 1

Views: 883

Answers (4)

Kevin Gosse
Kevin Gosse

Reputation: 39007

keyboardP's solution will work just fine. But I personally prefer to store the information I need in the Tag property of the control, which has been designed for this very purpose.

deck[r[i, j]].card.Tag = deck[r[i, j]];
deck[r[i, j]].card.ManipulationCompleted += ImageManipulationCompleted;

private void ImageManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
{
    var deck = (Deck)((FrameworkElement)sender).Tag;
}

The good side of keyboardP's approach is that, since you receive directly the desired object as a parameter of your method, it's easier to read. The downside is that you have to declare a custom delegate for every event you need, and you lose the ability to assign event handlers directly from the XAML. My solution is a bit harder to read, but addresses this point.

In the end, which solution is better really depends on your tastes and your needs.

Upvotes: 1

keyboardP
keyboardP

Reputation: 69372

What you could do is just call a method that takes in your type instead of using the standard ImageManipulationCompleted handler. I don't know what the deck[r[i, j]] type is but you can replace MyType below with the correct type.

deck[r[i, j]].card.ManipulationCompleted += delegate(object s, ManipulationCompletedEventArgs e){ CardManipulated(s, e, deck[r[i, j]]); };

private void CardManipulated(object sender, ManipulationCompletedEventArgs e, MyType selectedObject)
{
    //you know have access to selectedObject which is of type deck[r[i, j]],
    //the ManipluationCompletedEvents properties if needed,
    //and the actual card Image object (sender).
}

Upvotes: 1

Rohit Vats
Rohit Vats

Reputation: 81253

ManipulationCompletedEventHandler signature is using object in its first parameter

public delegate void ManipulationCompletedEventHandler(object sender, 
                         ManipulationCompletedRoutedEventArgs e);

So, you can't change the signature but you can use delegate to typecast object always to your type like this -

deck[r[i, j]].card.ManipulationCompleted += (s, e) => 
           ManipulateMe_ManipulationCompleted((YourType)s, e);

private void ImageManipulationCompleted(YourType sender,
                               ManipulationCompletedEventArgs e)
{
    //something
}

Replace YourType with your desired Type (TextBox or something whichever you want)

Upvotes: 0

AgentFire
AgentFire

Reputation: 9780

You cant.

Since you are subscribing to an event with this code new EventHandler<>(..), you cannot change the type of sender because in the description of EventHandler<> there is only object sender:

public delegate EventHandler<T>(object sender, T eventArgs) where T : EventArgs

If you need to create your own delegate, you could make a factory or simply write this:

public delegate EventHandler<T, TArgs>(T sender, TArgs eventArgs) where TTArgs : EventArgs

Upvotes: 0

Related Questions