C++ Silverlight for Windows Embedded(SWE)中的数据绑定

C++ Silverlight for Windows Embedded(SWE)中的数据绑定,c++,data-binding,silverlight-3.0,windows-ce,C++,Data Binding,Silverlight 3.0,Windows Ce,有人可以给我举一个工作示例,说明我应该如何在Silverlight for Windows Embedded(SWE)中进行数据绑定。我已经看到了很多,所以这似乎是可能的。我已经读到我需要实现IXRPropertyBag以使其工作,但还没有找到(工作)如何操作的说明。我设法使数据绑定在两个ToggleButton元素的IsChecked属性之间工作,基于WCE7 CTP附带的帮助文件中发现的疯狂的坏例子。我本来希望在XAML中设置数据上下文和数据绑定,但文档告诉我要编写代码 首先,您必须创建一个

有人可以给我举一个工作示例,说明我应该如何在Silverlight for Windows Embedded(SWE)中进行数据绑定。我已经看到了很多,所以这似乎是可能的。我已经读到我需要实现IXRPropertyBag以使其工作,但还没有找到(工作)如何操作的说明。

我设法使数据绑定在两个
ToggleButton
元素的
IsChecked
属性之间工作,基于WCE7 CTP附带的帮助文件中发现的疯狂的坏例子。我本来希望在XAML中设置数据上下文和数据绑定,但文档告诉我要编写代码

首先,您必须创建一个实现
IXRPropertyBag
的类。然后,您必须从代码中将数据上下文和数据绑定设置为此属性包的实例

很抱歉代码中没有命名约定,但它仍然比Microsoft提供的示例要好得多。它也不像可能的那样通用,但我将把重构留给您

MyPropertyBag.h:

#pragma once
#include "windows.h"
#include "XamlRuntime.h"
#include "XRCustomEvent.h"
#include "XRPtr.h"

class __declspec(uuid("3C6FFC6F-17A8-4976-B034-B4FE3BFF530A"))
MyPropertyBag : public IXRPropertyBag
{
private:
    LONG m_cRef;
    IXRCustomEvent<XRPropertyChangedCustomEventArgs, IXRPropertyBag> *pRadioEvent;
    XRThreeState RadioState;

public:
    MyPropertyBag(void);

    HRESULT STDMETHODCALLTYPE GetValue(__in const WCHAR* pstrPropertyName, __out XRValue *pValue);
    HRESULT STDMETHODCALLTYPE SetValue(__in const WCHAR* pstrPropertyName, __in XRValue *pValue);
    HRESULT STDMETHODCALLTYPE GetPropertyChangedEvent(__out IXRCustomEvent<XRPropertyChangedCustomEventArgs, IXRPropertyBag>** ppEvent);

    HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, LPVOID * ppvObj);
    ULONG STDMETHODCALLTYPE AddRef();
    ULONG STDMETHODCALLTYPE Release();
};

您还可以使用TBoundProperty—请参阅本Hands on labs,了解将椭圆宽度绑定到滑块的简单示例

MSDN虚拟实验室:Silverlight for Windows Embedded中的数据绑定

#include "StdAfx.h"
#include "MyPropertyBag.h"

extern "C" const GUID __declspec(selectany) IID_MyPropertyBag = __uuidof(IXRPropertyBag);

MyPropertyBag::MyPropertyBag(void)
{
    RadioState = XRThreeState_Unchecked;
    pRadioEvent = CreateCustomEvent<XRPropertyChangedCustomEventArgs, IXRPropertyBag>();
}

// IXRPropertyBag implementation:

HRESULT MyPropertyBag::GetValue(__in const WCHAR* pstrPropertyName, __out XRValue *pValue)
{
    HRESULT hr = E_FAIL;
    if (0 == wcscmp(pstrPropertyName, L"RadioState"))
    {
        pValue->vType = VTYPE_INT;
        pValue->IntVal = (int)RadioState;
        hr = S_OK;
    }

    return hr;
}

HRESULT MyPropertyBag::SetValue(__in const WCHAR* pstrPropertyName, __in XRValue *pValue)
{
    HRESULT hr = E_FAIL;

    if (0 == wcscmp(pstrPropertyName, L"RadioState"))
    {
        RadioState = (XRThreeState)pValue->IntVal;
        XRPropertyChangedCustomEventArgs eventArgs;
        eventArgs.PropertyName = pstrPropertyName;
        pRadioEvent->Raise(this, &eventArgs);
        hr = S_OK;
    }

    return hr;
}

HRESULT MyPropertyBag::GetPropertyChangedEvent(IXRCustomEvent<XRPropertyChangedCustomEventArgs, IXRPropertyBag>** ppEvent)
{
    *ppEvent = pRadioEvent;
    return S_OK;
}
// end of IXRPropertyBag implementation.

// IUnknown implementation:

HRESULT MyPropertyBag::QueryInterface(REFIID riid, LPVOID * ppvObj)
{
    if (!ppvObj)
        return E_INVALIDARG;

    *ppvObj = NULL;
    if (riid == IID_IUnknown || riid == IID_MyPropertyBag)
    {
        *ppvObj = (LPVOID)this;
        AddRef();
        return NOERROR;
    }

    return E_NOINTERFACE;
}

ULONG MyPropertyBag::AddRef()
{
    InterlockedIncrement(&m_cRef);
    return m_cRef;
}

ULONG MyPropertyBag::Release()
{
    ULONG ulRefCount = InterlockedDecrement(&m_cRef);
    if (0 == m_cRef)
    {
        delete this;
    }
    return ulRefCount;
}
// end of IUnknown implementation.
// Data bindings
XRPtr<MyPropertyBag> viewModel(new MyPropertyBag());
BindDataToControl(pTb1, viewModel);
BindDataToControl(pTb2, viewModel);

// save the exit code for WinMain
hr = m_pVisualHost->StartDialog(&uiExitCode);
SetWinMainResultCode(uiExitCode);  
inline void App::BindDataToControl(IXRFrameworkElement* pElement, IXRPropertyBag* pPropertyBag)
{
    // Set the binding value and source property
    XRBinding binding;
    binding.Mode = XRBindingMode_TwoWay;
    binding.Path = L"RadioState";
    pElement->SetBinding(L"IsChecked", &binding); 

    // Convert the data source object to an XRValue
    XRValue dataContext;
    dataContext.vType = VTYPE_PROPERTYBAG;
    dataContext.pPropertyBagVal = pPropertyBag;

    // Set the data source object as the data context for the option button
    pElement->SetDataContext(&dataContext);
}