本文介绍了ValueChanged没有用C#Win10点燃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看起来像


It seems exactly like Win10 IoT - RaspBerry Pi2: ValueChanged not called when GPIO changeI have a raspberry pi 2 with win10 IoT (creator version) and have this C# code:

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 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:

var dm = pinSensor.GetDriveMode();

as was suggested in the comment of the linked stack overflow issue. So what am I doing wrong? And more important: why?

解决方案
var deferval = taskInstance.GetDeferral();

Ref:Developing Background Applications

这篇关于ValueChanged没有用C#Win10点燃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-09 21:45