Use the LayoutUpdated Event to Create UI Objects in Silverlight for Windows Embedded (Compact 7)
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.
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.
|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).
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.
The following image shows the event handler hook code that was automatically added to the same C++ code file.