Partager via


Convert Data Types for Properties in TPropertyBag<Derived> (Compact 7)

3/12/2014

When you receive new data from a data provider, you must verify that the data is represented by a data type that is appropriate for a TBoundPropertyBase<PropertyType,StoreType> derived object. Data from a provider is not guaranteed to be represented by a data type that is compatible with the TBoundPropertyBase<PropertyType,StoreType> derived objects that represent properties in Microsoft Silverlight for Windows Embedded. For example, not all strings in all data providers are of type BSTR. Then, you can add the data to a TBoundPropertyBase<PropertyType,StoreType> derived object.

The following list describes how to treat the various types of data:

The following example code defines a text string that emulates the property value of a PR_SENDER_NAME property from a MAPI message object, converts it to a BSTR, and sets a new value of the Name property in a TPropertyBag<Derived> data object.

Important

For readability, the following code example does not contain security checking or error handling. Do not use the following code in a production environment.

#include "XRPropertyBag.h"
#include "XamlRuntime.h"

LPWSTR EmailSenderName = TEXT("Sanjay Patel"); 

XRValue updatedxrvalue;
updatedxrvalue.bstrStringVal = SysAllocString(EmailSenderName);
updatedxrvalue.vType = VTYPE_BSTR; 

m_DataObject->SetValue(L"Name", &updatedxrvalue);

The following example code converts an interface pointer to an XRPtr<Interface> smart pointer, and then sets the smart pointer as the value of a TBoundPointerProperty<PropertyType> object in a TPropertyBag<Derived> object called ClassName.

Important

For readability, the following code example does not contain security checking or error handling. Do not use the following code in a production environment.

#include "XRPropertyBag.h"
#include "XamlRuntime.h"
#include "XRPtr.h"
#include "Data.h"

// Define global variable for data source object.
XRPtr<ClassName> m_Object;

HRESULT MainPage::ConvertType(IXRPropertyBag* pData)
{
      // Initialize smart pointer and assign it the value of an interface pointer.
     XRPtr<IXRPropertyBag> pXRObject;
     pXRObject = pData;

     // Set the value of a property field in m_Object to the smart pointer.
     m_Object->pObject = pXRObject;
    
     return S_OK;
}

Contents of Data.h file:
class _declspec(uuid("{0ED08C01-200A-42ed-BB7C-A4ED016B65B7}")) ClassName : public TPropertyBag<ClassName>
{
protected:
     // To create an instance of this class, use the TPropertyBag.CreateInstance method and not the default constructor.
     ClassName() {};

public:
     TBoundProperty<BSTR> bstrProp1;
     TBoundProperty<BSTR> bstrProp2;
     TBoundPointerProperty<IXRPropertyBag> pObject; 

     HRESULT InitializeProperties()
     {
          HRESULT hr = S_OK;
          hr = BeginRegisterProperties();
          if (FAILED(hr))
          {
               return hr;
          }
          hr = RegisterBoundProperty(L"Name", bstrProp1);
          hr = RegisterBoundProperty(L"Address", bstrProp2);
          hr = RegisterBoundProperty(L"Object1", pObject);
          hr = EndRegisterProperties();
          return hr;
     }
};

After you verify that the data is of the appropriate type, you can update a data source property or modify items in a data source collection.

See Also

Concepts

Connect to a Data Provider