[英]Pin is currently opened in an incompatible sharing mode
I am currently using UWP Toolkit to navigate among app pages. 我目前正在使用UWP Toolkit在应用程序页面之间导航。 There is a page that is being used for initializing and opening RaspberryPi GPIO pins.
有一个页面用于初始化和打开RaspberryPi GPIO引脚。 The following error occurs after navigating away from that page then trying to navigate back to it again.
离开该页面然后尝试再次导航回该页面后,将发生以下错误。
The process cannot access the file because it is being used by another process .\\r\\n\\r\\nPin ' is currently opened in an incompatible sharing mode.
该进程无法访问该文件,因为该文件正在被另一个进程使用。\\ r \\ n \\ r \\ nPin'当前以不兼容的共享模式打开。 Make sure this pin is not already in use by this application or another application
确保此应用程序或其他应用程序尚未使用此引脚
I can see that the constructor is being called each time the page is visited and hence there is an attempt to open pins that are already opened. 我可以看到每次访问页面时都会调用构造函数,因此尝试打开已经打开的引脚。 What is the best way to overcome this issue?
解决此问题的最佳方法是什么?
You may add NavigationCacheMode = NavigationCacheMode.Required;
您可以添加
NavigationCacheMode = NavigationCacheMode.Required;
to the ctor of the page so your app will not create a new instance of it when you navigate there. 到页面ctor的位置,因此当您浏览该页面时,您的应用不会为其创建新实例。
What I always do is let a class deal with managing pins so that your user code can request pins to act on. 我一直做的是让一个类处理管脚,以便您的用户代码可以请求管脚进行操作。
public class IO
{
private readonly GpioController _gpioController;
private readonly Dictionary<int, GpioPin> _pins;
public IO(GpioController gpioController)
{
_gpioController = gpioController;
_pins = new Dictionary<int, GpioPin>();
}
public GpioPin OpenPin(int pin, GpioSharingMode mode)
{
if (_pins.ContainsKey(pin))
{
var gpioPin = _pins[pin];
if (gpioPin.SharingMode == mode)
{
return gpioPin;
}
throw new ArgumentException($"Pin '{pin}' is already configured in mode '{gpioPin.SharingMode}'");
}
else
{
var gpioPin = _gpioController?.OpenPin(pin, mode);
_pins[pin] = gpioPin;
return gpioPin;
}
}
} }
Then my viewmodels simply request a pin as follows 然后我的视图模型只要求如下图钉
public MainViewModel()
{
_io = ServiceContainer.Instance.Get<IO>();
_brakingPin = _io.OpenPin(4, GpioSharingMode.Exclusive);
_io.SetDriveMode(_brakingPin, GpioPinDriveMode.Output);
_io.Write(_brakingPin, GpioPinValue.Low);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.