I am new to Silverlight and XAML. While trying to learn the syntax and best practices I continue to come across a discrepancy (or at least to me it seems that way) in the way some implement event handlers.
In an example from MSDN I see the following code used:
<UserControl x:Class="DragAndDropSimple.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Canvas x:Name="rootCanvas"
Width="640"
Height="480"
Background="Gray"
>
<!-- You can drag this rectangle around the canvas. -->
<Rectangle
MouseLeftButtonDown="Handle_MouseDown"
MouseMove="Handle_MouseMove"
MouseLeftButtonUp="Handle_MouseUp"
Canvas.Left="30" Canvas.Top="30" Fill="Red"
Width="50" Height="50" />
</Canvas>
</UserControl>
Where the mouse handlers are set, however, in other code I have seen this method used in the code behind:
public Window1()
{
InitializeComponent();
TransformGroup group = new TransformGroup();
ScaleTransform xform = new ScaleTransform();
group.Children.Add(xform);
TranslateTransform tt = new TranslateTransform();
group.Children.Add(tt);
image.RenderTransform = group;
image.MouseWheel += image_MouseWheel;
image.MouseLeftButtonDown += image_MouseLeftButtonDown;
image.MouseLeftButtonUp += image_MouseLeftButtonUp;
image.MouseMove += image_MouseMove;
}
I would assume the example on MSDN is the recommended way, however, I tend to like the second method.
Is there a best practice for this situation?
Code-behind is a term used to describe the code that is joined with markup-defined objects, when a XAML page is markup-compiled. This topic describes requirements for code-behind as well as an alternative inline code mechanism for code in XAML.
In programming, an event handler is a callback routine that operates asynchronously once an event takes place. It dictates the action that follows the event. The programmer writes a code for this action to take place. An event is an action that takes place when a user interacts with a program.
Use the EventHandler delegate for all events that don't include event data. Use the EventHandler<TEventArgs> delegate for events that include data about the event. These delegates have no return type value and take two parameters (an object for the source of the event and an object for event data).
In general, an event handler has the name of the event, preceded by "on." For example, the event handler for the Focus event is onFocus. Many objects also have methods that emulate events. For example, button has a click method that emulates the button being clicked.
In the first approach there are two "code sites"
In the second there are 3 "code sites"
Personally I prefer the first approach. If I delete an element I need only find the event handlers that need removing, I don't then need to edit the class constructor as well.
Of course this is rule of thumb there will be many exceptions.
Unless I need to dynamically modify the event handlers for an object, I prefer to define it in the XAML itself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With