Use the LayoutUpdated Event to Create UI Objects in Silverlight for Windows Embedded (Compact 7)

3/12/2014

When you create UI objects by using the Silverlight for Windows Embedded C++ class library, you can add them to your application when the application loads by creating an event handler method for the LayoutUpdated event.

To create UI objects in C++ code instead of in XAML, you can use the Silverlight for Windows Embedded classes. You can find code examples in the following reference topics.

The LayoutUpdated Event

To create UI objects when your Silverlight for Windows Embedded application loads, create an event handler method for the LayoutUpdated event and add code that creates the UI objects.

To hook up your event handler, call the IXRFrameworkElement::AddLayoutUpdatedEventHandler method from the OnLoaded event handler in the main page of your application. By adding the UI objects in response to the LayoutUpdated event, you can guarantee that the visual tree is fully instantiated before you attempt to add or modify UI objects in the visual tree.

Important

Silverlight for Windows Embedded raises the LayoutUpdated event each time that the visual layout is updated, so make sure that your code responds only to the first LayoutUpdated event that Silverlight for Windows Embedded raises when the application loads.

You can use the Windows Embedded Events window in Microsoft Visual Studio 2008 to quickly generate stub code for an event handler for this event. Open the XAML file for your main page, and then in the Windows Embedded Events window, next to the event name (LayoutUpdated), either type in a name for your event handler and press ENTER, or double-click the empty cell to generate a default name. The following image shows the results of double-clicking in the empty cell. A default name was generated (LayoutRoot_LayoutUpdated).

Add an event handler stub using the Events window

The following image shows the stub code that was automatically added to the C++ code file for the XAML file. Add code to create UI objects before the return hr; line.

Event handler stub code added to the C++ file

The following image shows the event handler hook code that was automatically added to the same C++ code file.

Event handler hookup code added to the C++ file

See Also

Concepts

Event Handling in Silverlight for Windows Embedded