Partager via


Update Data Source Properties (Compact 7)

3/12/2014

You can update the data source properties of an existing object, such as an object that represents a media file, in the data source collection that you created in Create a Data Source Collection.

For example, consider a user who edits media files in a playlist and adds metadata for album name and album artist. When the user reloads the playlist on a device running Windows Embedded Compact, the media application detects the changes and updates the AlbumName and AlbumArtist properties of items in an existing collection.

To update data source properties

  1. In Platform Builder, open your OS design project.

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

  3. Obtain a pointer to the XRObservableCollection<ItemType> collection.

  4. To block other threads from accessing the collection object, implement a thread safety mechanism. For example, create an XRAutoCriticalSection object, and call EnterCriticalSection.

  5. Obtain a pointer to a data-source object in the collection. For example, call the derived method XRValueCollectionT.GetItem(int,Obj**) to obtain a pointer to an item in the collection.

  6. To obtain the current value of a property in the data source object, call TPropertyBag.GetValue(const WCHAR *,XRValue *).

  7. To set a new value for a property, call TPropertyBag.SetValue(const WCHAR *,XRValue *).

  8. Release ownership of the thread. For example, call LeaveCriticalSection.

The following example code obtains the value of a property of an item in a data source collection and updates its value.

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 "XRCollection.h"
#include "oleauto.h"

HRESULT MainPage::UpdateData()
{
       // Create a data source collection with one item
       XRPtr<XRObservableCollection<ClassName*>,IXREnumerable> pCollection;
       XRObservableCollection<ClassName*>::CreateInstance(&pCollection);
 
       XRPtr<ClassName> pItem;
       pItem->InitializeProperties();
       pCollection->Add(pItem); 

       // Define variables for the data source object and property.
       XRPtr<ClassName> pObj;
       XRValue xrvalueCurrent;
       XRValue xrvalueNew;
       TBoundProperty<BSTR> bstrProp;

       // Set a BSTR string for the new XRValue object. 
       bstrProp = SysAllocString(L"UpdatedStringValue");
       xrvalueNew.bstrStringVal = bstrProp;

       // Define a critical section object.
       XRAutoCriticalSection csObject;
     
       // Obtain an item from the collection.
       pCollection->GetItem(0, &pObj);

       // Request mutually exclusive access to the thread.
       EnterCriticalSection(&csObject);

       // Update a data source property.
       pObj->GetValue(L"PropName1", &xrvalueCurrent);

       if (xrvalueCurrent.bstrStringVal != bstrProp)
       {
          pObj->SetValue(L"PropName1", &xrvalueNew);
       }

       // Release the critical section object.
       LeaveCriticalSection(&csObject);

       return S_OK;
}

See Also

Concepts

Update the Data (Optional)