Adding extra parameters to events
-
Okay so here's the deal. I have an event that generates a number of xaml components on the fly. One of these xaml components also has some events hooked up to it. In order to do this I am using the following line of code
Holder.ManipulationDelta += new ManipulationDeltaEventHandler(HolderManipulationDelta);
This is then handled with
private void HolderManipulationDelta(object sender, Windows.UI.Xaml.Input.ManipulationDeltaRoutedEventArgs e) {//code here }
What I need is to be able to pass in an extra parameter in the event such as...
private void HolderManipulationDelta(object sender, Windows.UI.Xaml.Input.ManipulationDeltaRoutedEventArgs e, CompositeTransform mv) {//code here }
However this seems to be invalid. I've tried googling and got some stuff on delegates etc but I cant get my head around it. Can anyone help me out?
How does the CompositeTransform get created?
-
How does the CompositeTransform get created?
I'll add a bit more of the code. The compositetransform is created along with one of the xaml components.
public void NewButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{Canvas newcanvas = new Canvas();
CompositeTransform mv = new CompositeTransform();
AppGrid.Children.Add(newcanvas);Ellipse Holder = new Ellipse();
Holder.ManipulationDelta += new ManipulationDeltaEventHandler(HolderManipulationDelta);}
private void HolderManipulationDelta(object sender, Windows.UI.Xaml.Input.ManipulationDeltaRoutedEventArgs e){ //code here}
This code compiles, but I need to add the third parameter into the holdermanipulationdelta event, and when i do that i get the error message above.
-
By default there is no third parameter and when i try and add one I get the error message: "No overload for 'HolderManipulationDelta' matches delegate 'Windows.UI.Xaml.Input.ManipulationDeltaEventHandler'" I'm looking for a way to add the third parameter in.
You may want to look into using
override
. I can't find any tutorials that might help you and I'm not exactly an expert on the subject. But it might be worth while to search on your own as you'll know better what you need. Edit: The override function is used for changing things in classes and such, from what I can gather. -
I'll add a bit more of the code. The compositetransform is created along with one of the xaml components.
public void NewButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{Canvas newcanvas = new Canvas();
CompositeTransform mv = new CompositeTransform();
AppGrid.Children.Add(newcanvas);Ellipse Holder = new Ellipse();
Holder.ManipulationDelta += new ManipulationDeltaEventHandler(HolderManipulationDelta);}
private void HolderManipulationDelta(object sender, Windows.UI.Xaml.Input.ManipulationDeltaRoutedEventArgs e){ //code here}
This code compiles, but I need to add the third parameter into the holdermanipulationdelta event, and when i do that i get the error message above.
Why don't you make "mv" a class member? If you don't want to go that route for some "insane" :) reason, you can do what you want with lambdas:
Canvas newcanvas = new Canvas();
CompositeTransform mv = new CompositeTransform();AppGrid.Children.Add(newcanvas);
Ellipse Holder = new Ellipse();
Holder.ManipulationDelta += (x, y) =>
{
HolderManipulationData(x, y, mv);
}That'll work, but I'd make it a class member since that's more "proper" IMO :).
-
Why don't you make "mv" a class member? If you don't want to go that route for some "insane" :) reason, you can do what you want with lambdas:
Canvas newcanvas = new Canvas();
CompositeTransform mv = new CompositeTransform();AppGrid.Children.Add(newcanvas);
Ellipse Holder = new Ellipse();
Holder.ManipulationDelta += (x, y) =>
{
HolderManipulationData(x, y, mv);
}That'll work, but I'd make it a class member since that's more "proper" IMO :).
-
You may want to look into using
override
. I can't find any tutorials that might help you and I'm not exactly an expert on the subject. But it might be worth while to search on your own as you'll know better what you need. Edit: The override function is used for changing things in classes and such, from what I can gather.There's nothing to override here. If you change the signature of a method, you are adding a new method, not overriding an existing one.
*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
-
Okay so here's the deal. I have an event that generates a number of xaml components on the fly. One of these xaml components also has some events hooked up to it. In order to do this I am using the following line of code
Holder.ManipulationDelta += new ManipulationDeltaEventHandler(HolderManipulationDelta);
This is then handled with
private void HolderManipulationDelta(object sender, Windows.UI.Xaml.Input.ManipulationDeltaRoutedEventArgs e) {//code here }
What I need is to be able to pass in an extra parameter in the event such as...
private void HolderManipulationDelta(object sender, Windows.UI.Xaml.Input.ManipulationDeltaRoutedEventArgs e, CompositeTransform mv) {//code here }
However this seems to be invalid. I've tried googling and got some stuff on delegates etc but I cant get my head around it. Can anyone help me out?
The standard convention in .NET for event delegates is to have just 2 parameters, the first is the object that generated the event and the second is a class derived from
EventArgs
that provides more information about the event. Note that this is not a hard and fast rule and you can have as many parameters as you like, but sticking to the convention makes your code cleaner, readable and maintainable. You can compose yourCompositeTransform
class insideManipulationDeltaRoutedEventArgs
as an instance field. -
Okay so here's the deal. I have an event that generates a number of xaml components on the fly. One of these xaml components also has some events hooked up to it. In order to do this I am using the following line of code
Holder.ManipulationDelta += new ManipulationDeltaEventHandler(HolderManipulationDelta);
This is then handled with
private void HolderManipulationDelta(object sender, Windows.UI.Xaml.Input.ManipulationDeltaRoutedEventArgs e) {//code here }
What I need is to be able to pass in an extra parameter in the event such as...
private void HolderManipulationDelta(object sender, Windows.UI.Xaml.Input.ManipulationDeltaRoutedEventArgs e, CompositeTransform mv) {//code here }
However this seems to be invalid. I've tried googling and got some stuff on delegates etc but I cant get my head around it. Can anyone help me out?
I consider SledgeHammer's response, using a Lambda, as the best, elegant solution (and I'm not competent in WPF/XAML), but I am curious, if you are defining a custom EventArgs class for use with your EventHandler: Why could you not add another parameter, of whatever 'mv's Type is, to that custom EventArgs class; then in your explicit EventHandler implementation which is "wired-up" to the Event, you can test if that instance of 'mv is null, and ignore, or, if not null: do whatever you want to do ? If this question is (and I fear it may be) way out-of-focus, I appreciate being corrected ! best, Bill
"Everything we call real is made of things that cannot be regarded as real." Niels Bohr
-
The standard convention in .NET for event delegates is to have just 2 parameters, the first is the object that generated the event and the second is a class derived from
EventArgs
that provides more information about the event. Note that this is not a hard and fast rule and you can have as many parameters as you like, but sticking to the convention makes your code cleaner, readable and maintainable. You can compose yourCompositeTransform
class insideManipulationDeltaRoutedEventArgs
as an instance field.This is a good solution (+5), however if the OP is working with the System.Windows.Shapes.Ellipse then they may need to look at overriding the OnManipulationDelta[^] Method so that they can use their extended ManipulationDeltaRoutedEventArgs.
-
I consider SledgeHammer's response, using a Lambda, as the best, elegant solution (and I'm not competent in WPF/XAML), but I am curious, if you are defining a custom EventArgs class for use with your EventHandler: Why could you not add another parameter, of whatever 'mv's Type is, to that custom EventArgs class; then in your explicit EventHandler implementation which is "wired-up" to the Event, you can test if that instance of 'mv is null, and ignore, or, if not null: do whatever you want to do ? If this question is (and I fear it may be) way out-of-focus, I appreciate being corrected ! best, Bill
"Everything we call real is made of things that cannot be regarded as real." Niels Bohr
This is actually a standard event argument for "the technology formerly known as Metro" style apps raised by the subsystem. It's not a custom one the user has created.
*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
-
The standard convention in .NET for event delegates is to have just 2 parameters, the first is the object that generated the event and the second is a class derived from
EventArgs
that provides more information about the event. Note that this is not a hard and fast rule and you can have as many parameters as you like, but sticking to the convention makes your code cleaner, readable and maintainable. You can compose yourCompositeTransform
class insideManipulationDeltaRoutedEventArgs
as an instance field. -
Cheers for your response. As I said I'm fairly new to this and I wasn't aware this was an option. I don't suppose you have a link to any good resources/tutorials on doing this so that I can read up on it?
-
I consider SledgeHammer's response, using a Lambda, as the best, elegant solution (and I'm not competent in WPF/XAML), but I am curious, if you are defining a custom EventArgs class for use with your EventHandler: Why could you not add another parameter, of whatever 'mv's Type is, to that custom EventArgs class; then in your explicit EventHandler implementation which is "wired-up" to the Event, you can test if that instance of 'mv is null, and ignore, or, if not null: do whatever you want to do ? If this question is (and I fear it may be) way out-of-focus, I appreciate being corrected ! best, Bill
"Everything we call real is made of things that cannot be regarded as real." Niels Bohr
Yeah, the lamba solution I provided was a "quick fix". I wouldn't do it that way in "professional" code. Making it a member was cleaner as noted. Unfortunately, I think OP is going down the entirely wrong path creating objects in code, but when you post those kind of responses on CodeProject, you tend to get 1 voted by people who "don't like your attitude", so I stopped bothering :). There is a danger with the lamba solution, that you could be passing an unintended instance of the wrong object if you don't pay attention :).