ValueChanged 未使用 C# Win10 Iot 触发

ValueChanged not firing with C# Win10 Iot

看起来很像Win10 IoT - RaspBerry Pi2: ValueChanged not called when GPIO change 我有一个带有 win10 IoT(创建者版本)的 raspberry pi 2,并且有这个 C# 代码:

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();
    }
}

led on sensor and quickwatch 说 GpioPinValue 确实在高低之间交替...所以应该被击中...

当我将其设置为输入后检索驱动模式时。它告诉我它实际上设置为输入:

var dm = pinSensor.GetDriveMode();

正如链接堆栈溢出问题的评论中所建议的那样。那我做错了什么?更重要的是:为什么?

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:

var deferval = taskInstance.GetDeferral();

参考:Developing Background Applications