Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event handlers in XAML or Code Behind

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?

like image 393
Irwin M. Fletcher Avatar asked Jan 14 '10 16:01

Irwin M. Fletcher


People also ask

What is code behind XAML?

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.

What is an event handler in code?

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.

When would you use an event handler?

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).

What are event handlers example?

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.


2 Answers

In the first approach there are two "code sites"

  1. the XAML where the UI elements are defined and the events are wired up in the same place.
  2. The event handler procedures in code behind

In the second there are 3 "code sites"

  1. the XAML where the UI elements are defined
  2. the constructor where the events are wired up
  3. The event handler procedures in code behind

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.

like image 121
AnthonyWJones Avatar answered Sep 28 '22 03:09

AnthonyWJones


Unless I need to dynamically modify the event handlers for an object, I prefer to define it in the XAML itself.

like image 24
Parrots Avatar answered Sep 28 '22 04:09

Parrots