Share via


Create a Data Source Collection (Compact 7)

3/12/2014

After you create a data class, you must use it to instantiate data source objects and add them to an XRObservableCollection<ItemType> object.

To create a data source collection

  1. In your subproject, open the source code file where you want to add functionality to create the data source collection. For example, in Solution Explorer, browse to Subprojects\< Subproject Name>\Source files, and open MainPage.cpp.

  2. Define an XRObservableCollection<ItemType> object variable and create an instance by calling XRObservableCollection.CreateInstance(XRObservableCollection * *).

  3. Create data source objects to add to the collection. For each data source object, do the following:

    1. Define an object variable.
    2. To block other threads from accessing the collection object, implement a thread safety mechanism. For example, initialize an XRAutoCriticalSection object and call EnterCriticalSection.
    3. To create an object instance of the data class, call TPropertyBag.CreateInstance(Derived**).
    4. On the collection instance, call its derived method XRValueCollectionT.Add(const ItemType&) to add the data source object to the collection.
    5. Release ownership of the thread. For example, call LeaveCriticalSection.
  4. (Optional) To support hierarchical data, do the following:

    1. Identify a data source object that must support hierarchical data and that includes a TBoundPointerProperty<PropertyType> property field.
    2. Create an XRObservableCollection<ItemType> sub-collection of items by repeating steps 1 and 2.
    3. To convert the sub-collection object into an XRValue object, create an XRValue object and call XRValue::SetValue(IXREnumerable, bool).
    4. To block other threads from accessing the collection object, implement a thread safety mechanism. For example, initialize an XRAutoCriticalSection object and call EnterCriticalSection.
    5. On the data source object, call TPropertyBag.SetValue(const WCHAR *,XRValue *) to set the XRObservableCollection<ItemType> sub-collection as the value of a property field of type TBoundPointerProperty<PropertyType>.
    6. On the collection instance, call its derived method XRValueCollectionT.Add(const ItemType&) to add the data source object to the collection.
    7. Release ownership of the thread. For example, call LeaveCriticalSection.
  5. In the shutdown code for your application, call the inherited method _XRValueCollectionBaseT.Clear to release references to objects in the data source collection. For example, add the code to the helper method App::OnExit in App.cpp.

The following code example creates a data source collection of data source objects. The code example assigns initial values to data source objects by using a custom method, Initialize, that initializes the ID and FirstName registered properties.

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 "Employee.h"

static XRPtr<MyEmployee> m_pModel;
    static HRESULT CreateModel()
    {
        HRESULT hr = S_OK;
       
        XRPtr<XRObservableCollection<MyEmployee*>,IXREnumerable> pCollection;
        XRObservableCollection<MyEmployee*>::CreateInstance(&pCollection);

        XRPtr<MyEmployee> pEmployee1;
        MyEmployee::CreateInstance(&pEmployee1);
        pEmployee1->Initialize(1, L"EmployeeOne");

        XRPtr<MyEmployee> pEmployee2;
        MyEmployee::CreateInstance(&pEmployee2);
        pEmployee2->Initialize(2, L"EmployeeTwo");

        pCollection->Add(pEmployee1);
        pCollection->Add(pEmployee2);
        
        MyEmployee::CreateInstance(&m_pModel);
        m_pModel->Initialize(3, L"EmployeeModel");
        m_pModel->m_pDirectReports = pCollection;

        return hr;
    }

See Also

Concepts

Populate the List with Data