protected override void OnNavigatedTo(NavigationEventArgs e)
 {
     base.OnNavigatedTo(e);
     if (EventHandlerForDevice.Current.IsDeviceConnected)
     {
         SetAllControlsActive(true);
         _actionQueue = EventHandlerForDevice.Current.ActionQueue;
         DisabledReason.Visibility = Visibility.Collapsed;
     }
     else
     {
         SetAllControlsActive(false);
         DisabledReason.Visibility = Visibility.Visible;
     }
 }
        /// <summary>
        /// This method opens the device using the WinRT Serial API. After the device is opened, save the device
        /// so that it can be used across scenarios.
        ///
        /// It is important that the FromIdAsync call is made on the UI thread because the consent prompt can only be displayed
        /// on the UI thread.
        ///
        /// This method is used to reopen the device after the device reconnects to the computer and when the app resumes.
        /// </summary>
        /// <param name="deviceInfo">Device information of the device to be opened</param>
        /// <param name="deviceSelector">The AQS used to find this device</param>
        /// <returns>True if the device was successfully opened, false if the device could not be opened for well known reasons.
        /// An exception may be thrown if the device could not be opened for extraordinary reasons.</returns>
        public async Task <bool> OpenDeviceAsync(DeviceInformation deviceInfo, string deviceSelector)
        {
            Device = await SerialDevice.FromIdAsync(deviceInfo.Id);

            bool       successfullyOpenedDevice = false;
            NotifyType notificationStatus;
            string     notificationMessage;

            // Device could have been blocked by user or the device has already been opened by another app.
            if (Device != null)
            {
                successfullyOpenedDevice = true;

                DeviceInformation = deviceInfo;
                DeviceSelector    = deviceSelector;

                Connection  = new DeviceConnection(Device);
                ActionQueue = new DeviceActionQueue(Connection);

                notificationStatus  = NotifyType.StatusMessage;
                notificationMessage = "Device " + DeviceInformation.Properties["System.ItemNameDisplay"] + " opened";

                // Notify registered callback handle that the device has been opened
                OnDeviceConnected?.Invoke(this, DeviceInformation);

                if (_appSuspendEventHandler == null || _appResumeEventHandler == null)
                {
                    RegisterForAppEvents();
                }

                // Register for DeviceAccessInformation.AccessChanged event and react to any changes to the
                // user access after the device handle was opened.
                if (_deviceAccessEventHandler == null)
                {
                    RegisterForDeviceAccessStatusChange();
                }

                // Create and register device watcher events for the device to be opened unless we're reopening the device
                if (_deviceWatcher == null)
                {
                    _deviceWatcher = DeviceInformation.CreateWatcher(deviceSelector);

                    RegisterForDeviceWatcherEvents();
                }

                if (!_watcherStarted)
                {
                    // Start the device watcher after we made sure that the device is opened.
                    StartDeviceWatcher();
                }
            }
            else
            {
                notificationStatus = NotifyType.ErrorMessage;

                var deviceAccessStatus = DeviceAccessInformation.CreateFromId(deviceInfo.Id).CurrentStatus;

                switch (deviceAccessStatus)
                {
                case DeviceAccessStatus.DeniedByUser:
                    notificationMessage = "Access to the device was blocked by the user : "******"System.ItemNameDisplay"];
                    break;

                case DeviceAccessStatus.DeniedBySystem:
                    // This status is most likely caused by app permissions (did not declare the device in the app's package.appxmanifest)
                    // This status does not cover the case where the device is already opened by another app.
                    notificationMessage = "Access to the device was blocked by the system : " + deviceInfo.Properties["System.ItemNameDisplay"];
                    break;

                default:
                    notificationMessage = "Unknown error, possibly opened by another app : " + deviceInfo.Properties["System.ItemNameDisplay"];
                    break;
                }
            }

            MainPage.Current.NotifyUser(notificationMessage, notificationStatus);

            return(successfullyOpenedDevice);
        }