私は中国語なので、質問を読むのに大変なご迷惑をおかけして申し訳ありません。英語が苦手ですが、質問は長い間悩まされてきました。
データバインディングに関するテストコードを作成すると、問題が発生します。
データソースを生成するサイクルを持つ2番目のスレッドを作成し、次にスレッドがMainPageのメソッドを呼び出してListBoxItemにデータを入力します。
この方法では、これを行うために2つの方法を使用します。1つはデータバインディングを使用し、もう1つはListBoxItemのSetContent()を使用します。
その結果、データバインディングは約数十サイクル実行されてから停止しますが、SetContent()は実行して終了できます。
以下は、私のコード全体に基づいたWindows Embedded Silverlight Toolsであり、CEPCで実行されます。
MainPage.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
そのような結果:
DataBindingを使用する場合の正確な方法は何ですか?HMIを構築したいのですが、おそらく数百の価値があり、500ミリ秒ごとに数十のアイテムを更新する可能性があります。
SetContent()は、実稼働環境で実行可能な方法で使用できますか?