【发布时间】:2015-08-03 08:56:50
【问题描述】:
我已经尝试了很多,但我不知道如何更新 GUI 元素,例如从树莓上 Windows IoT 的 Windows 通用应用程序上运行的任务中更新 TextBlock.Text。
有什么办法吗?
它应该在不停止的情况下完成正在运行的任务。
根据一个答案,我试过这个:
Task t1 = new Task(() =>
{
while (1 == 1)
{
byte[] writeBuffer = { 0x41, 0x01, 0 }; // Buffer to write to mcp23017
byte[] readBuffer = new byte[3]; // Buffer to read to mcp23017
SpiDisplay.TransferFullDuplex(writeBuffer, readBuffer); // Send writeBuffer to mcp23017 and receive Results to readBuffer
byte readBuffer2 = readBuffer[2]; // extract the correct result
string output = Convert.ToString(readBuffer2, 2).PadLeft(8, '0'); // convert result to output Format
// Update the frontend TextBlock status5 with result
Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
// Your UI update code goes here!
status6.Text = output;
});
}
});
t1.Start();
但我收到以下 2 个错误:
Error CS0103 The name 'CoreDispatcherPriority' does not exist in the current context
和
CS4014 Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.
我在使用代码时做错了吗?
【问题讨论】:
-
我对 Windows-universal 没有经验,但在 winforms/wpf 中有一个
Invoke方法可以做到这一点。以及异步版本 (InvokeAsync/BeginInvoke)。 -
我已经尝试过调用它,但它没有在通用 Windows 应用程序下运行。
-
CS0103:使用 Windows.UI.Core 添加(参见下面的编辑示例) CS4014:添加 await 关键字并标记范围异步(参见下面的编辑示例)
标签: c# raspberry-pi task win-universal-app iot