Partager via


Type-Safety in Silverlight for Windows Embedded (Windows Embedded CE 6.0)

1/6/2010

The Silverlight for Windows Embedded C++ API provides many methods that return objects, such as IXRApplication::CreateObject, IXRFrameworkElement::FindName, and IXRFrameworkElement::GetParent. These methods typically return generic object types, such as IXRDependencyObject. However, to access or change the characteristics of the UI object that you retrieved, you usually must work with a specific object type.

To work with a specific type of object, you must use a derived interface type instead of IXRDependencyObject.

There are two ways to ensure that an object is a specific type:

  • Use a helper template overload method to retrieve a type-safe object. A helper template overload method allows you to supply the specific object type to the method, and the method retrieves the object and converts it to the specified type so you can use the object immediately. This approach simplifies your code and saves you time.

    Note

    Helper template overload methods target C++ programming and are not available in Microsoft Silverlight 2 for Web applications.

  • Call IUnknown::QueryInterface on the object. Use this approach when the method does not support a helper template overload version.

    Note

    The QueryInterface approach can be time-consuming and add process overhead to your application.

Helper Template Overload Method Example

The following example shows the signature of a template helper version of the IXRFrameworkElement::FindName method, defined in XamlRuntime.h.

template<typename XRObj> HRESULT FindName(
  __in const WCHAR *pName,
  __out XRObj** ppFoundObject
  )
    { return __ts_nmdo_converter(
        this,
        &IXRFrameworkElement::FindName,
        pName,
        ppFoundObject); 
    }

The following example shows how to use a helper template overload method to automatically convert a type from a generic interface to IXRButton.

HRESULT hr;
IXRButton* pButton = NULL;
 
hr = pRoot->FindName(L"OKButton", &pButton);
 if(SUCCEEDED(hr))
{
// Do something with your button
      pButton->Release();
}

To reduce your code and ensure correct reference counting, you can use smart pointers that manage the reference count and ownership lifetime. When you use smart pointers, you do not have to call IUnknown::Release to release an interface to an object. The following example shows how to automatically convert a type by using smart pointers with helper methods.

HRESULT hr;
IXRButtonPtr pButton;
hr = pRoot->FindName(L"OKButton", &pButton);
if(pButton)
{
    // Use the button
}

For more information about smart pointers, see XRPtr<Interface>.

QueryInterface Example

The following code example shows how to retrieve an object using the QueryInterface method, and then obtain the derived interface type. We recommend that you use this method only when a helper template overload method is unavailable.

HRESULT hr;
IXRDependencyObject* pDO = NULL;
 hr = pPanel->GetChildren(&pDO);
 
if(SUCCEDED(hr))
{
    IXRUIElementCollection* pCollection = NULL;
     hr = pDO->QueryInterface(IID_IXRUIElementCollection,(void**)&pCollection);
 
    if(SUCCEDED(hr))
     {
        // Do something with your collection
         pCollection->Release();
    }
 
    pDO->Release();
}

See Also

Concepts

Silverlight for Windows Embedded