Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 请帮助我检查有关在Silverlight for Windows Embedded中进行数据绑定的代码_C++_Multithreading_Silverlight_Data Binding_Windows Embedded - Fatal编程技术网

C++ 请帮助我检查有关在Silverlight for Windows Embedded中进行数据绑定的代码

C++ 请帮助我检查有关在Silverlight for Windows Embedded中进行数据绑定的代码,c++,multithreading,silverlight,data-binding,windows-embedded,C++,Multithreading,Silverlight,Data Binding,Windows Embedded,很抱歉给你带来了很多麻烦来阅读我的问题,因为我是中国人,我的英语很差,但是这个问题困扰了我很长时间 当我构建一个关于数据绑定的测试代码时,就会遇到麻烦 我创建了第二个线程,该线程有一个生成数据源的周期,然后该线程在MainPage中调用一个方法来填充ListBoxItem 该方法使用两种方法来实现这一点。一个使用数据绑定,另一个使用ListBoxItem的SetContent() 结果是数据绑定运行了几十个周期,然后停止,但SetContent()可以一直运行到结束 下面是我的整个基于代码的Wi

很抱歉给你带来了很多麻烦来阅读我的问题,因为我是中国人,我的英语很差,但是这个问题困扰了我很长时间

当我构建一个关于数据绑定的测试代码时,就会遇到麻烦

我创建了第二个线程,该线程有一个生成数据源的周期,然后该线程在MainPage中调用一个方法来填充ListBoxItem

该方法使用两种方法来实现这一点。一个使用数据绑定,另一个使用ListBoxItem的SetContent()

结果是数据绑定运行了几十个周期,然后停止,但SetContent()可以一直运行到结束

下面是我的整个基于代码的Windows嵌入式Silverlight工具,并在CEPC中运行

MainPage.XAML

在App::GetWindowParameters中,如果不是,SetContent()可能看起来像数据绑定

在MainPage.h中声明

HRESULT MainPage::UpdateData();

DataValue.h:

#pragma once

#include <oleauto.h>

#include "XRCollection.h"

#include "XRPropertyBag.h"

class _declspec(uuid("{9C0158BE-D467-4284-A51A-327DEFE935C5}")) DataValue : public TPropertyBag<DataValue>

{

protected:

    // Protected, create by using TPropertyBag::CreateInstance to create an instance, then call Initialize();

    // this will CreateInstance will use XRObject to implement IUnknown, which saves us work.

    DataValue() {};

public:

    HRESULT Initialize(float l1,float l2)

    {

        HRESULT hr = InitializeProperties();

        m_L1 =l1;

        m_L2 =l2;

        return hr;

    }

     TBoundProperty<float> m_L1;

     TBoundProperty<float> m_L2;

    // Mapping TBoundProperty and property names.

    HRESULT InitializeProperties()

    {

        HRESULT hr = S_OK;

        hr = BeginRegisterProperties();

        if (FAILED(hr))

            return hr;   

        hr = RegisterBoundProperty(L"L1", m_L1);

        if (FAILED(hr))

            return hr;

        hr = RegisterBoundProperty(L"L2", m_L2);

        if (FAILED(hr))

            return hr;

        hr = EndRegisterProperties();

        return hr;

    }

};
#pragma一次
#包括
#包括“XRCollection.h”
#包括“XRPropertyBag.h”
类_declspec(uuid({9C0158BE-D467-4284-A51A-327DEFE935C5}))数据值:公共TPropertyBag
{
受保护的:
//Protected,使用TPropertyBag::CreateInstance创建实例,然后调用Initialize();
//这将创建一个实例,它将使用XRObject实现IUnknown,这将节省我们的工作。
DataValue(){};
公众:
HRESULT初始化(浮点l1、浮点l2)
{
HRESULT hr=初始化属性();
m_L1=L1;
m_L2=L2;
返回人力资源;
}
t边界属性m_L1;
TBoundProperty m_L2;
//映射TBoundProperty和属性名称。
HRESULT InitializeProperties()
{
HRESULT hr=S_正常;
hr=BeginRegisterProperties();
如果(失败(小时))
返回人力资源;
hr=RegisterBoundProperty(L“L1”,m_L1);
如果(失败(小时))
返回人力资源;
hr=RegisterBoundProperty(L“L2”,m_L2);
如果(失败(小时))
返回人力资源;
hr=EndRegisterProperties();
返回人力资源;
}
};
在MainPange.cpp中:

#include "stdafx.h"

#include "test6Generated.h"

#include "MainPage.h"

#include "App.h"

#include "resource.h"

#include "DataValue.h"

#include "oleauto.h"

#include "XRPropertyBag.h"

static XRPtr<DataValue> pDataValue;

static XRValue xrvalueNew;

static float i=0;

// ============================================================================

//  OnLoaded

//

//  Description: Calls InitializeComponent to bind member variables to named

//               elements, and attach event handlers specified in XAML

//

//  Parameters:  pRoot - The root dependency object.

// ============================================================================

HRESULT MainPage::OnLoaded(__in IXRDependencyObject* pRoot)

{

    UNREFERENCED_PARAMETER(pRoot);

    HRESULT hr = InitializeComponent();

    // Add calls to FindName or Add___EventHandler() methods after this comment.

    DataValue::CreateInstance(&pDataValue);

    pDataValue->Initialize(1,2);

    XRValue DataContext(pDataValue);

    m_pListBoxItem1->SetDataContext(&DataContext);

    //m_pListBoxItem2->SetDataContext(&DataContext);

    HANDLE hThread1;

    hThread1=CreateThread(NULL,0,Thread2,this,0,NULL);

    CloseHandle(hThread1);

    return hr;

} // OnLoaded

DWORD WINAPI Thread2 (PVOID pArg)

{   

    HRESULT hr = E_NOTIMPL;

    MainPage* pMainPage = (MainPage *)pArg;

    while(i<2000)   

    {

        ++i;

            Sleep(200);

    pMainPage->UpdateData();

        }

    return 0;

}

HRESULT MainPage::UpdateData() 

{ 

    xrvalueNew.SetValue(i); 

       XRAutoCriticalSection csObject; 

       EnterCriticalSection(&csObject); 

       //pItemValue->SetValue(L"ID",&xrvalueNew);

       pDataValue->m_L1=i;

       m_pListBoxItem2->SetContent(&xrvalueNew);

       LeaveCriticalSection(&csObject);

              return S_OK; 

}

#pragma region GeneratedCode

// ============================================================================

//  WARNING: DO NOT EDIT THIS ALWAYS-GENERATED CODE

// ============================================================================

HRESULT MainPage::InitializeComponent()

{

    HRESULT hr = E_FAIL;

    FindName(L"LayoutRoot", &m_pLayoutRoot);

    FindName(L"Button1", &m_pButton1);

    FindName(L"ListBoxItem1", &m_pListBoxItem1);

    FindName(L"ListBoxItem2", &m_pListBoxItem2);

    if (m_pLayoutRoot &&

        m_pButton1 &&

        m_pListBoxItem1 &&

        m_pListBoxItem2

       )

    {

        hr = S_OK;

    }

    return hr;

}

// ============================================================================

//  WARNING: DO NOT EDIT THIS ALWAYS-GENERATED CODE

// ============================================================================

#pragma endregion GeneratedCode
#包括“stdafx.h”
#包括“test6Generated.h”
#包括“MainPage.h”
#包括“App.h”
#包括“resource.h”
#包括“DataValue.h”
#包括“oleauto.h”
#包括“XRPropertyBag.h”
静态XRTR pDataValue;
静态XRValue xrvalueNew;
静态浮点数i=0;
// ============================================================================
//装载
//
//描述:调用InitializeComponent将成员变量绑定到命名的
//元素,并附加XAML中指定的事件处理程序
//
//参数:pRoot-根依赖项对象。
// ============================================================================
HRESULT主页::已加载(u在IXRDependencyObject*pRoot中)
{
未引用的_参数(pRoot);
HRESULT hr=初始化组件();
//在此注释后添加对FindName的调用或添加\uuuuuuu EventHandler()方法。
DataValue::CreateInstance(&pDataValue);
pDataValue->初始化(1,2);
XRValue数据上下文(pDataValue);
m_pListBoxItem1->SetDataContext(&DataContext);
//m_pListBoxItem2->SetDataContext(&DataContext);
句柄hThread1;
hThread1=CreateThread(NULL,0,Thread2,this,0,NULL);
CloseHandle(hThread1);
返回人力资源;
}//已加载
DWORD WINAPI线程2(PVOID pArg)
{   
HRESULT hr=E_NOTIMPL;
主页*pMainPage=(主页*)参数;
while(iUpdateData();
}
返回0;
}
HRESULT主页::UpdateData()
{ 
xrvalueNew.SetValue(i);
XRAU临界截面csObject;
EnterCriticalSection(&csObject);
//pItemValue->SetValue(L“ID”和&xrvalueNew);
pDataValue->m_L1=i;
m_pListBoxItem2->SetContent(&xrvalueNew);
LeaveCriticalSection(&csObject);
返回S_OK;
}
#pragma区域生成代码
// ============================================================================
//警告:不要编辑此始终生成的代码
// ============================================================================
HRESULT主页::InitializeComponent()
{
HRESULT hr=E_失败;
FindName(L“LayoutRoot”和m_pLayoutRoot);
FindName(L“Button1”和m_pButton1);
FindName(L“ListBoxItem1”和m_pListBoxItem1);
FindName(L“ListBoxItem2”和m_pListBoxItem2);
如果(m_pLayoutRoot&&
m_p按钮1&&
m_pListBoxItem1&&
m_pListBoxItem2
)
{
hr=S_OK;
}
返回人力资源;
}
// ============================================================================
//警告:不要编辑此始终生成的代码
// ============================================================================
#pragma-endregion-GeneratedCode
结果是:

如果我想使用数据绑定,具体的方法是什么?我想构建一个HMI,它可能有数百个值,每500毫秒可能刷新几十个项目


SetContent()是一种在生产环境中使用的可行方法吗?

您需要直接设置TPropertyBag。请参阅


在中使用sleep()的循环看起来有点不愉快……我尝试了这个方法,pDataValue->SetValue(L“L1”和&xrvalueNew);但问题是一样的。其他一切看起来都正常。有几件事我有点怀疑:1)ListBoxItem通常在ListBox中使用。我不知道是否涉及虚拟化技巧。试试文本框。2) 正在非主线程的线程上更改该值。这意味着事件处理程序也不在主线程上。也许这里有一种技术?我读过这篇文章和其他关于消息映射的文章,但是Silverlight for Windows Embedded不能提供MFC,而且它基于一个模态对话框,所以似乎没有消息循环。然后我阅读一篇文章,它使用一个基于计时器的故事板,然后UI线程获得值,而不是数据线程推送。我不知道这是不是个好主意。非常感谢!!根据这个文档,有一个消息循环哦,这个信息对你很有帮助。我试过了
DWORD WINAPI Thread2 (PVOID pArg);
#pragma once

#include <oleauto.h>

#include "XRCollection.h"

#include "XRPropertyBag.h"

class _declspec(uuid("{9C0158BE-D467-4284-A51A-327DEFE935C5}")) DataValue : public TPropertyBag<DataValue>

{

protected:

    // Protected, create by using TPropertyBag::CreateInstance to create an instance, then call Initialize();

    // this will CreateInstance will use XRObject to implement IUnknown, which saves us work.

    DataValue() {};

public:

    HRESULT Initialize(float l1,float l2)

    {

        HRESULT hr = InitializeProperties();

        m_L1 =l1;

        m_L2 =l2;

        return hr;

    }

     TBoundProperty<float> m_L1;

     TBoundProperty<float> m_L2;

    // Mapping TBoundProperty and property names.

    HRESULT InitializeProperties()

    {

        HRESULT hr = S_OK;

        hr = BeginRegisterProperties();

        if (FAILED(hr))

            return hr;   

        hr = RegisterBoundProperty(L"L1", m_L1);

        if (FAILED(hr))

            return hr;

        hr = RegisterBoundProperty(L"L2", m_L2);

        if (FAILED(hr))

            return hr;

        hr = EndRegisterProperties();

        return hr;

    }

};
#include "stdafx.h"

#include "test6Generated.h"

#include "MainPage.h"

#include "App.h"

#include "resource.h"

#include "DataValue.h"

#include "oleauto.h"

#include "XRPropertyBag.h"

static XRPtr<DataValue> pDataValue;

static XRValue xrvalueNew;

static float i=0;

// ============================================================================

//  OnLoaded

//

//  Description: Calls InitializeComponent to bind member variables to named

//               elements, and attach event handlers specified in XAML

//

//  Parameters:  pRoot - The root dependency object.

// ============================================================================

HRESULT MainPage::OnLoaded(__in IXRDependencyObject* pRoot)

{

    UNREFERENCED_PARAMETER(pRoot);

    HRESULT hr = InitializeComponent();

    // Add calls to FindName or Add___EventHandler() methods after this comment.

    DataValue::CreateInstance(&pDataValue);

    pDataValue->Initialize(1,2);

    XRValue DataContext(pDataValue);

    m_pListBoxItem1->SetDataContext(&DataContext);

    //m_pListBoxItem2->SetDataContext(&DataContext);

    HANDLE hThread1;

    hThread1=CreateThread(NULL,0,Thread2,this,0,NULL);

    CloseHandle(hThread1);

    return hr;

} // OnLoaded

DWORD WINAPI Thread2 (PVOID pArg)

{   

    HRESULT hr = E_NOTIMPL;

    MainPage* pMainPage = (MainPage *)pArg;

    while(i<2000)   

    {

        ++i;

            Sleep(200);

    pMainPage->UpdateData();

        }

    return 0;

}

HRESULT MainPage::UpdateData() 

{ 

    xrvalueNew.SetValue(i); 

       XRAutoCriticalSection csObject; 

       EnterCriticalSection(&csObject); 

       //pItemValue->SetValue(L"ID",&xrvalueNew);

       pDataValue->m_L1=i;

       m_pListBoxItem2->SetContent(&xrvalueNew);

       LeaveCriticalSection(&csObject);

              return S_OK; 

}

#pragma region GeneratedCode

// ============================================================================

//  WARNING: DO NOT EDIT THIS ALWAYS-GENERATED CODE

// ============================================================================

HRESULT MainPage::InitializeComponent()

{

    HRESULT hr = E_FAIL;

    FindName(L"LayoutRoot", &m_pLayoutRoot);

    FindName(L"Button1", &m_pButton1);

    FindName(L"ListBoxItem1", &m_pListBoxItem1);

    FindName(L"ListBoxItem2", &m_pListBoxItem2);

    if (m_pLayoutRoot &&

        m_pButton1 &&

        m_pListBoxItem1 &&

        m_pListBoxItem2

       )

    {

        hr = S_OK;

    }

    return hr;

}

// ============================================================================

//  WARNING: DO NOT EDIT THIS ALWAYS-GENERATED CODE

// ============================================================================

#pragma endregion GeneratedCode
// Don't do this
//pDataValue->m_L1=i;

// Do this instead
pDataValue->SetValue(L"L1", &i)