ValueChanged没有用C#Win10点燃 [英] ValueChanged not firing with C# Win10 Iot
问题描述
看起来像 Win10 IoT - RaspBerry Pi2 :当GPIO发生变化时,ValueChanged不被调用$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ p> 带领传感器和快速表示GpioPinValue在高和低...所以应该得到打击... 当我将其设置为输入后检索驱动器模式。它告诉我它实际上被设置为输入: 如链接堆栈溢出问题的注释中所建议的那样。那我做错了什么?更重要的是:为什么? 当Run方法结束时,除非创建延期对象, 参考:开发背景应用程序 It seems exactly like Win10 IoT - RaspBerry Pi2: ValueChanged not called when GPIO change
I have a raspberry pi 2 with win10 IoT (creator version) and have this C# code: led on sensor and quickwatch say the GpioPinValue does alternate between high and low... so should get hit... When I retrieve the drive mode after setting it to input. It tells me it actually is set to input: as was suggested in the comment of the linked stack overflow issue. So what am I doing wrong? And more important: why? When the Run method ends, unless a deferral object is created, the
Background Application ends. The common practice, for asynchronous
programming is to take a deferral like this:
Ref:Developing Background Applications 这篇关于ValueChanged没有用C#Win10点燃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! public sealed class StartupTask:IBackgroundTask
{
private const int SENSOR_PIN = 17;
私人GpioPin pinSensor;
public void Run(IBackgroundTaskInstance taskInstance)
{
taskInstance.Canceled + = TaskInstance_Canceled; //destructor
var gpio = GpioController.GetDefault();
if(gpio!= null)
{
pinSensor = gpio.OpenPin(SENSOR_PIN); //也尝试过GpioSharingMode.SharedReadOnly
var r = pinSensor.Read(); //如果传感器发生变化,就进行更改用quickwatch验证
pinSensor.SetDriveMode(GpioPinDriveMode.Input);
pinSensor.DebounceTimeout = TimeSpan.FromMilliseconds(20);
pinSensor.ValueChanged + = PinIn_ValueChanged;
}
}
private void PinIn_ValueChanged(GpioPin sender,GpioPinValueChangedEventArgs args)
{
//从未得到...
}
private void TaskInstance_Canceled(IBackgroundTaskInstance sender,BackgroundTaskCancellationReason reason)
{
pinSensor.Dispose();
}
}
var dm = pinSensor.GetDriveMode();
背景应用程序结束。异步
编程的常见做法是采取如下推迟:
var deferval = taskInstance.GetDeferral();
public sealed class StartupTask : IBackgroundTask
{
private const int SENSOR_PIN = 17;
private GpioPin pinSensor;
public void Run(IBackgroundTaskInstance taskInstance)
{
taskInstance.Canceled += TaskInstance_Canceled; // "destructor"
var gpio = GpioController.GetDefault();
if (gpio != null)
{
pinSensor = gpio.OpenPin(SENSOR_PIN); // also tried with GpioSharingMode.SharedReadOnly
var r = pinSensor.Read(); // works and changes if sensor changes. Verified with quickwatch
pinSensor.SetDriveMode(GpioPinDriveMode.Input);
pinSensor.DebounceTimeout = TimeSpan.FromMilliseconds(20);
pinSensor.ValueChanged += PinIn_ValueChanged;
}
}
private void PinIn_ValueChanged(GpioPin sender, GpioPinValueChangedEventArgs args)
{
// never gets hit...
}
private void TaskInstance_Canceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
{
pinSensor.Dispose();
}
}
var dm = pinSensor.GetDriveMode();
var deferval = taskInstance.GetDeferral();