Share via


Work with Event Handlers Ported from an Expression Blend Project (Windows Embedded Compact 2013)

3/20/2013

You can import event handlers from an Microsoft Expression Blend 3 project into a XAML for Windows Embedded application subproject in Platform Builder. After you import the event handlers, you can add custom code to the XAML for Windows Embedded application to implement the event handlers. We use the UI element example from Event Handling in XAML for Windows Embedded (as shown in the following figure) to illustrate how to create user-defined event handlers for a XAML for Windows Embedded application's UI elements.

MLBD Default Event Handling

  1. Step 1: Create Your TBD
  2. Step 2: Import the Expression Blend project into a Silverlight for Windows Embedded application subproject
  3. Step 3: Add custom code to your application to implement your event handler
  4. Step 4: Build the Application
  5. Step 5: Order of Events

Step 1: Create Your TBD

TBD

To create a UI element like the one in the example

  1. Create a project in Expression Blend using the Windows Embedded Silverlight project template.

    For more information, see TBD.

  2. Add a ListBox element that contains four CheckBox elements as children as shown in the following figure.

    For more information, see TBD.

    Show the design in Expression Blend

  3. Save your project so that you can import it into a Platform Builder project in Microsoft Visual Studio 2012 Update 1.

Step 2: Import the Expression Blend project into a Silverlight for Windows Embedded application subproject

TBD

To import the Expression Blend project as an application subproject

  1. In Visual Studio, open a Platform Builder OS design that supports XAML for Windows Embedded.

    For more information about OS designs that support XAML for Windows Embedded, see TBD.

  2. On the Tools menu, click Windows Embedded Silverlight Tools, and then click Create Platform Builder Subproject to open the Platform Builder Subproject Application Wizard.

  3. Step through the wizard to create the Platform Builder application subproject directory and to import the UI project you saved in step 1.

  4. To add this newly created subproject to your OS design, on the TBD, right-click Subprojects, and then click Add Existing Subproject. Browse to the .pbxml file or .vcproj file for the converted project, and then click Open.

Step 3: Add custom code to your application to implement your event handler

Next, add the C++ code for your event handler to your XAML for Windows Embedded application.

To add a customized event handler

  1. In Visual Studio, in the Resource folder of your application subproject, open the MainPage.xaml file.

  2. On the Tools menu, click Windows Embedded Silverlight Tools, and then click Windows Embedded Events to open the Windows Embedded Events window.

    The upper pane of the window shows the UI elements for the application. The lower pane of the windows lists the possible events for a selected UI element.

  3. To add code for your event handler by using the Windows Embedded Events window, select a check box control in the upper pane of the Windows Embedded Events window and type the name of the event handler method in the Handler column for the MouseLeftButtonDown event as shown in the following figure..

When you press the ENTER key or otherwise move the mouse away from the Handler entry field, the system automatically adds C++ code to the MainPage.h and MainPage.cpp files for the named event handler. You then edit this code to implement your customized event handling functionality.

Windows Embedded Events Window

When you enter the handler name for the control and handler pictured in the preceding figure, it causes the system to add the following method definition to MainPage.h:

HRESULT CheckBox2_OnMouseLeftButtonDown (IXRDependencyObject* pSender, XRMouseButtonEventArgs* pArgs);

The system adds the following call to AddMouseLeftButtonDownEventHandler for CheckBox2 in the MainPage::OnLoaded event handler of MainPage.cpp:

if (m_pCheckBox2)
{
    m_pCheckBox2->AddClickEventHandler(CreateDelegate(this, &MainPage::CheckBox2_OnMouseLeftButtonDown));
}

The system also adds a stub for the event handling method itself to the MainPage.cpp file:

// ============================================================================
//  OnMouseLeftButtonDown
// 
//  Description: Event handler implementation
//
//  Parameters:  pSender - The dependency object that raised the click event.
//               pArgs - Event specific arguments.
// ============================================================================
HRESULT MainPage::CheckBox2_OnMouseLeftButtonDown (IXRDependencyObject* pSender, XRMouseButtonEventArgs* pArgs)
{
    HRESULT hr = E_NOTIMPL;

    if ((NULL == pSender) || (NULL == pArgs))
    {
        hr = E_INVALIDARG;
    }
   return hr;
}

You then add to and modify the code to implement the functionality that you want for the event handler. Note that any modifications that you make to code in MainPage.h and MainPage.cpp will be overwritten if you use the Update Silverlight for Windows Embedded Project menu option to update your application with changes made in the Expression Blend UI project. Also note that modifications that you make to the application code by using Visual Studio are not recognized by the Windows Embedded Events window.

For the purpose of our example here, add code to the above event handler that sets Handled = true in the XREventArgs-derived structure for the control. Assume the following line is in the code immediately before the return statement:

   pArgs->Handled = true;

Step 4: Build the application

TBD

To build your XAML for Windows Embedded application

  1. In Visual Studio, on the Build menu, click Build All Subprojects.

Step 5: Learn the Order of Events

Suppose you execute the sample application that you created from the procedures in this topic, then you move your mouse into the second CheckBox and press the left mouse button, as indicated by the arrow in the following figure. The relevant events for this action are MouseEnter, MouseLeftButtonDown, and GotFocus. In this case, the event sequence is:

  1. MouseEnter on the ListBox
  2. MouseEnter on the CheckBox
  3. MouseLeftButtonDown on the CheckBox

In this scenario, your event handler for MouseLeftButtonDown in the CheckBox executes first and sets the handled flag to true. The default MouseLeftButtonDown event handler then executes and attempts to process the event, but because the handled flag is already set, no further actions are taken. Note also that there is no change in focus in this scenario because the default event processing does not occur until after the handled flag is set.

MLBD User-Defined Event Handling

If we continue the scenario, when the MouseLeftButtonUp event occurs on the CheckBox, the default object event handler executes, but since the handled flag is set, no Click or Checked events are generated. When the mouse is moved back out of the CheckBox and ListBox, only the following events occur:

  1. MouseLeftButtonUp on the CheckBox
  2. MouseLeave on the CheckBox
  3. MouseLeave on the ListBox

The state of the controls after this scenario ends is shown in the next figure.

MLBU User-Defined Event Handling

See Also

Concepts

Event Handling in XAML for Windows Embedded