首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >线程错误Windows10 IoT核

线程错误Windows10 IoT核
EN

Stack Overflow用户
提问于 2018-08-21 06:32:30
回答 1查看 50关注 0票数 1

我是一个新的Windows 10 IoT。

我将制作一个应用程序作为白板应用与DragonBoard 410 c。

我把按钮连接到GPIO。

并按以下方式编码,但错误发生了。

代码语言:javascript
复制
private void InitGPIO()
{
    var gpio = GpioController.GetDefault();

    if(gpio == null)
    {

        var dialog2 = new MessageDialog("Please Check GPIO");
        dialog2.ShowAsync();

        return;
    }
    BTN_UP = gpio.OpenPin(BTN_UP_NUMBER);

    BTN_UP.SetDriveMode(GpioPinDriveMode.Input);

    BTN_UP.DebounceTimeout = TimeSpan.FromMilliseconds(50);
    BTN_UP.ValueChanged += btn_up_pushed;

    var dialog = new MessageDialog("GPIO Ready");
    dialog.ShowAsync();
}

private void btn_up_pushed(GpioPin sender, GpioPinValueChangedEventArgs e)
{
    int but_width = 0;
    int but_height = 0;

    but_width = (int)cutButton.Width;
    but_height = (int)cutButton.Height;
}

当我按下名为btn_up_pushed()的按钮时。但是错误发生在下面的图片中。

在这里输入图像描述

请帮帮我!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-21 08:33:46

获得以下异常是因为您访问了UI元素(cutButton是按钮,对吗?)在非UI线程中。

您需要将线程从当前正在执行的线程封送到UI线程。

Windows.UI.Core.CoreDispatcher可以适应这种情况。下面是一个示例:

代码语言:javascript
复制
using Windows.ApplicationModel.Core;

    private async void btn_up_pushed(GpioPin sender, GpioPinValueChangedEventArgs e)
    {
        int but_width = 0;
        int but_height = 0;

        await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => {
            but_width = (int)cutButton.Width;
            but_height = (int)cutButton.Height;
        });
    }

参考文献:"CoreDispatcher.RunAsync

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51942858

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档