请帮助我检查Silverlight for Windows Embedded中有关数据绑定的代码
Please help me check the code about Databinding in Silverlight for Windows Embedded
很抱歉给你读我的问题带来了很多麻烦,因为我是中国人,我的英语很差,但这个问题困扰了我很长时间。
当我构建一个关于数据绑定的测试代码时,就会遇到麻烦。
我创建了第二个线程,它有一个周期来生成数据源,然后该线程调用MainPage中的一个方法来填充ListBoxItem。
这个方法有两种方法。一个使用Databinding,另一个使用ListBoxItem的SetContent()。
结果是,Databinding运行大约几十个周期,然后停止,但SetContent()可以运行到结束。
以下是我的全部基于代码的Windows嵌入式Silverlight工具,并在CEPC 中运行
主页.XAML
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="test6.MainPage"
Width="640" Height="480">
<Grid x:Name="LayoutRoot" Background="White">
<Button x:Name="Button1" Height="43" HorizontalAlignment="Right" Margin="0,111,83,0" VerticalAlignment="Top" Width="117" Content="Button"/>
<ListBoxItem x:Name="ListBoxItem1" HorizontalAlignment="Left" Margin="84,111,0,0" Width="138" Content="{Binding L1}" Height="52" VerticalAlignment="Top" Background="#CEFFA3A3" FontSize="16"/>
<ListBoxItem x:Name="ListBoxItem2" Height="43" HorizontalAlignment="Left" Margin="84,187,0,0" VerticalAlignment="Top" Width="138" Content="{Binding L2}" Background="#6971FF6E" FontSize="16"/>
</Grid>
首先添加
pWindowParameters->AllowsMultipleThreadAccess = true;
在App::GetWindowParameters中,如果没有,SetContent()可能会像DataBinding一样出现。
在MainPage.h 中声明
HRESULT MainPage::UpdateData();
和
DWORD WINAPI Thread2 (PVOID pArg);
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;
}
};
在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
结果是这样的:
http://social.msdn.microsoft.com/Forums/nl-BE/winembnatapp/thread/52e0fd82-e81b-448f-a518-1a8bef5dd8e4
如果我想使用DataBinding,具体的方法是什么?我想建立一个HMI,它可能有数百个价值,每500毫秒可能会刷新几十个项目。
SetContent()是一种在生产环境中使用的可行方法吗?
您需要直接设置TPropertyBag。请参阅MSDN
// Don't do this
//pDataValue->m_L1=i;
// Do this instead
pDataValue->SetValue(L"L1", &i)
相关文章:
- 尝试通过OCI例程从Oracle获取blob数据,但出现错误:ORA-01008:并非所有变量都绑定
- 如何理解此C++代码中的动态绑定?
- 编译器如何在编译时执行数据绑定?
- 学生信息的数据绑定
- 为什么VAO可以使用VBO数据绑定它?
- 在 Mac 上C++编译代码时处理“dyld:惰性符号绑定失败:找不到符号”错误
- 在 ZeroMQ 中绑定订阅者套接字并连接发布者套接字会在代码运行时给出错误.为什么
- 如何在没有大量代码的情况下将类 A 中的纯虚拟方法绑定到两者的子类中 B 类的方法?
- 是否可以将匿名 lambda 函数绑定到对象以允许 lambda 中的代码访问对象的成员?
- 从高度动态的C 数据模型中更新QML:计时器与属性绑定
- | 9 |错误:非静态数据成员'matrix :: row'| 9 |错误:数组绑定不是在']']'令牌|之前的整数常数
- Boost.Python是否需要为其他Boost库绑定代码
- DDX 数据绑定与 Windows 窗体找不到类方法
- 代码合成XSD解析/数据绑定xml字符串,而不是xml文件
- 请帮助我检查Silverlight for Windows Embedded中有关数据绑定的代码
- XML数据绑定-可以自动完成读/写功能
- c++中的XML数据绑定,具有简单的非侵入性数据模型
- C++的XML数据绑定,类似于java的JAXB
- Windows 8 APP,用数据绑定改变XAML文本框的文本.游戏循环工作线程导致问题
- 数据绑定- XML数据绑定和c++代码生成器