银光(Silverlight)在Windows嵌入式(SWE)中的数据绑定

3
请问有没有人可以给我提供一个在Silverlight for Windows Embedded(SWE)中实现数据绑定的可行示例?我看过一个展示(showcase),所以这应该是可行的。我也阅读了这里(here)的内容,需要实现IXRPropertyBag才能使其正常工作,但我尚未找到(有效的)说明如何实现它。请指教。
2个回答

5
我成功地实现了两个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();
};

MyPropertyBag.cpp:

#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.

在调用 Visual Host 的 StartDialog 之前,创建共享属性包并为两个切换按钮调用 BindDataToControl:
// 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);  

这是BindDataToControl的代码示例,用于设置DataContext和数据绑定:

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);
}

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接