当Raspberry PI 3和Visual Studio的LED按钮亮起时,导航回到首页会崩溃

时间:2018-06-29 15:33:11

标签: c# uwp-xaml

我遇到了一些问题,需要帮助来解决。在我应用程序的第一页中,我有几个按钮可以打开或关闭Raspberry PI 3上的LED。这一切都很好,但是当我导航到第二页然后又导航回第一页时,应用程序崩溃。我认为是因为加载第一页时该按钮处于关闭状态,而导航回该页面时,该按钮处于打开状态而不是关闭状态,因此崩溃。我希望我在这里有道理:-)

这是我的代码:

public sealed partial class ProfileView : Page
{
    private int red_state = 1;
    private bool green_state = false;
    private const int RED = 3;
    private const int GREEN = 18;
    private GpioPin redPin;
    private GpioPin greenPin;

    public ProfileView()
    {
        this.InitializeComponent();
        InitGPIO();
    }

    private void InitGPIO()
    {
        var gpio = GpioController.GetDefault();
        redPin = gpio.OpenPin(RED);
        greenPin = gpio.OpenPin(GREEN);
        redPin.Write(GpioPinValue.High);
        greenPin.Write(GpioPinValue.Low);
        redPin.SetDriveMode(GpioPinDriveMode.Output);
        greenPin.SetDriveMode(GpioPinDriveMode.Output);
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        if (red_state == 0)
        {
            redPin.Write(GpioPinValue.High);
            red_state = 1;
        }
        else if (red_state == 1)
        {
            redPin.Write(GpioPinValue.Low);
            red_state = 0;
        }

    }
    private void ProfileView_Unloaded(object sender, object args)
    {
        redPin.Dispose();
        greenPin.Dispose();
    }
}
}

谢谢

编辑

这是我遇到的错误:

System.IO.FileLoadException: 'The process cannot access the file because it 
is being used by another process.

Pin ' is currently opened in an incompatible sharing mode. Make sure this pin 
is not already in use by this application or another 

在这一行

  redPin = gpio.OpenPin(RED);

EDIT2

通过将“ NavigationCacheMode =” Required“”添加到我的XAML文件中来解决此问题,现在一切正常。谢谢

0 个答案:

没有答案