Create a Data Class (Compact 7)
3/12/2014
To populate a list with data from a collection, you must first create a class or set of classes to represent your collection data.
The data provider may organize its data in a variety of ways, including data in event structures, property values, classes, data fields, and so on.
To prepare the collection data for a Microsoft Silverlight for Windows Embedded application, you must represent it by creating a data class that uses the programming elements in the Silverlight for Windows Embedded class library.
To create a data class
In Platform Builder, open the subproject you created in Create the Expression Blend Project and the Windows Embedded Silverlight Tools Subproject.
Implement a custom data class that implements the TPropertyBag<Derived> interface.
In the data class, add a property field for each data-source property you identified in Create the Expression Blend Project and the Windows Embedded Silverlight Tools Subproject. The property fields must have data types of Silverlight for Windows Embedded classes that represent properties, which include TBoundProperty<PropertyType>, TBoundProperty<BSTR>, and TBoundPointerProperty<PropertyType>.
Note
TBoundProperty<PropertyType>, TBoundProperty<BSTR>, and TBoundPointerProperty<PropertyType> provide Get and Set methods, so you do not have to implement your own Get and Set methods in the data class, as you do for classes that implement IXRPropertyBag.
Implement functionality in the data class to register the properties by using TPropertyBag.BeginRegisterProperties, TPropertyBag.RegisterBoundProperty, and TPropertyBag.EndRegisterProperties, as follows:
- Block other threads by calling TPropertyBag.BeginRegisterProperties.
- Call TPropertyBag.RegisterBoundProperty for each property field, and pass the data-source property name that you identified earlier in this tutorial into the PropertyName input parameter.
- Stop blocking other threads by calling TPropertyBag.EndRegisterProperties.
(Optional) To support displaying hierarchical data, define another property field of type TBoundPointerProperty<PropertyType>. The property field represents a pointer to a collection object that belongs to the data class.
Important
Set the PropertyType template parameter to an interface that XRObservableCollection<Derived> supports, such as IXRList, IXREnumerable, or IXRValueCollection.
The following example code shows a data class that has a property field, m_pManager, for storing hierarchical data.
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 "XRCollection.h"
#include "XRPropertyBag.h"
class _declspec(uuid("{557BFE61-D018-41ce-AF28-06EB6812DBFC}")) MyEmployee : public TPropertyBag<MyEmployee>
{
protected:
MyEmployee() {};
public:
HRESULT Initialize(int ID, const WCHAR* pFirstName)
{
HRESULT hr = InitializeProperties();
m_ID = ID;
m_FirstName = SysAllocString(pFirstName);
return hr;
}
TBoundProperty<int> m_ID;
TBoundProperty<BSTR> m_FirstName;
TBoundPointerProperty<TPropertyBag> m_pManager;
TBoundPointerProperty<IXRList> m_pDirectReports;
HRESULT InitializeProperties()
{
HRESULT hr = S_OK;
hr = BeginRegisterProperties();
if (FAILED(hr))
return hr;
hr = RegisterBoundProperty(L"ID", m_ID);
if (FAILED(hr))
return hr;
hr = RegisterBoundProperty(L"FirstName", m_FirstName);
if (FAILED(hr))
return hr;
hr = RegisterBoundProperty(L"Manager", m_pManager);
if (FAILED(hr))
return hr;
hr = RegisterBoundProperty(L"DirectReports", m_pDirectReports);
if (FAILED(hr))
return hr;
hr = EndRegisterProperties();
return hr;
}
};