我是Windows 10 IoT的新手。
我将使用DragonBoard 410c作为白板应用程序进行申请。
我将按钮连接到GPIO。
并且编码如下,但是发生了错误。
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()。 但是发生了如下图所示的错误。
请帮助我!
答案 0 :(得分:1)
您会收到以下异常,因为您在非UI线程中访问UI元素(cutButton是Button右吗?)。
您需要将线程从当前正在执行的线程封送到UI线程。
Windows.UI.Core.CoreDispatcher可以用于此目的。这是一个示例:
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”