C#Windows IoT-从任务更新GUI

C#Windows IoT-从任务更新GUI,c#,raspberry-pi,task,win-universal-app,iot,C#,Raspberry Pi,Task,Win Universal App,Iot,我已经尝试了很多,但我不知道如何从Raspberry上Windows Universal App for Windows IoT的运行任务中更新GUI元素,例如TextBlock.Text 有没有办法做到这一点 它应该在不停止任务的情况下从正在运行的任务中退出 根据我的回答,我试过这样做: Task t1 = new Task(() => { while (1 == 1) { byte[] w

我已经尝试了很多,但我不知道如何从Raspberry上Windows Universal App for Windows IoT的运行任务中更新GUI元素,例如
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(); 
但我得到以下两个错误:

Error   CS0103  The name 'CoreDispatcherPriority' does not exist in the current context


我是否在使用代码时出错?

我不确定您的问题在哪里,我想可能是不同线程的问题。 尝试使用调度程序。您需要集成Windows.UI.Core命名空间:

using Windows.UI.Core;
这是您的电话(稍作修改以便于开箱即用)

小提示:while(1=1)对我来说就像一个无限循环。 另一个提示:我添加了“wait Task.Delay(1000);”以在循环过程中稍微休息一下

还可以查看关于dispatcher的回答。

我对Windows universal没有经验,但在winforms/wpf中有一个
调用
方法来实现这一点。以及异步版本(
InvokeAsync
/
BeginInvoke
)。我用invoke尝试过,但在通用Windows应用程序下无法运行。CS0103:使用Windows.UI.Core添加(请参见下面编辑的示例)CS4014:添加等待关键字并标记作用域异步(请参见下面编辑的示例)Thx以获取帮助,我已经尝试过使用你的代码,但它不起作用(我已经在问题中添加了代码)。
using Windows.UI.Core;
private void DoIt()
        {

            Task t1 = new Task(async () =>
            {
                while (1 == 1)
                {
                    await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                                        () =>
                                        {
                                            // Your UI update code goes here!
                                            status6.Text = "Hello" + DateTime.Now;
                                        });
                    await Task.Delay(1000);
                }
            });
            t1.Start();

        }