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?