我在WINCE7系统下做了一个Silverlight的小程序,主要主要功能是在一个线程中循环产生数字,在界面中显示出来。
代码是使用Windows Embedded Silverlight Tools生成的,我在MainPage中创建一个线程,里面循环调用MainPage的一个方法,这个方法主要功能就是刷新界面上的ListBoxItem,我采用了两种方法实现:一是采用数据绑定,然后刷新这个DataBinding;另一种是直接调用listBoxItem的SetContent()
结果在虚拟机中,DataBinding走到几十就停止了,而SetContent()可以一直循环,
请大家帮我分析一下我是哪里做错了,按照微软的说法还是应该使用DataBinding作为数据的接口,但是现在没法正常工作
代码如下:
MainPage.xaml:
------------------------------
<UserControl
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>
</UserControl>
-----------------------------------
App::GetWindowParameters中添加
pWindowParameters->AllowsMultipleThreadAccess = true;
没有这句,SetContent()也没法运行到最后.
在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:
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;
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;
HRESULT MainPage::OnLoaded(__in IXRDependencyObject* pRoot)
{
UNREFERENCED_PARAMETER(pRoot);
HRESULT hr = InitializeComponent();
DataValue::CreateInstance(&pDataValue);
pDataValue->Initialize(1,2);
XRValue DataContext(pDataValue);