using GenFlashLight; using System; using Windows.ApplicationModel.Core; using Windows.Devices.Enumeration; using Windows.Media.Capture; using Windows.UI.Core; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Navigation; // Pour en savoir plus sur le modèle d’élément Page vierge, consultez la page http://go.microsoft.com/fwlink/?LinkID=390556 namespace G_PhoneLocalizeApp { /// /// Une page vide peut être utilisée seule ou constituer une page de destination au sein d'un frame. /// public sealed partial class LightScreen : Page { public static bool IsCallLight = false; public static MediaCapture MediaPhoneCapture = null; private bool isOff = true; private bool IsOn = false; CoreDispatcher dispatcher = Windows.UI.Core.CoreWindow.GetForCurrentThread().Dispatcher; public LightScreen() { this.InitializeComponent(); } private DependencyObject FindChildControl(DependencyObject control, string ctrlName) { int childNumber = VisualTreeHelper.GetChildrenCount(control); for (int i = 0; i < childNumber; i++) { DependencyObject child = VisualTreeHelper.GetChild(control, i); FrameworkElement fe = child as FrameworkElement; // Not a framework element or is null if (fe == null) return null; if (child is T && fe.Name == ctrlName) { // Found the control so return return child; } else { // Not found it - search children DependencyObject nextLevel = FindChildControl(child, ctrlName); if (nextLevel != null) return nextLevel; } } return null; } private async void SwitchOn() { try { if (CoreApplication.MainView.CoreWindow == null) return; var dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher; if (dispatcher == null) return; await dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () => { if (MediaPhoneCapture != null) { ButtonLight.IsEnabled = true; MediaPhoneCapture.VideoDeviceController.TorchControl.Enabled = true; } MediaPhoneCapture = new MediaCapture(); var videoDevices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture); DeviceInformation rearCamera = null; //get rear camera foreach (DeviceInformation d in videoDevices) { if (d.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back) { rearCamera = d; break; } } if (rearCamera == null) { //await dispatcher.RunAsync(CoreDispatcherPriority.High, () => //{ // var result = TxtHelpChildren.Text + " : Problem => Torch not supported"; // TxtHelpChildren.Text = result; //}); return; } MediaCaptureInitializationSettings cameraSettings = new MediaCaptureInitializationSettings(); cameraSettings.VideoDeviceId = rearCamera.Id; await MediaPhoneCapture.InitializeAsync(cameraSettings); if (MediaPhoneCapture.VideoDeviceController.TorchControl.Supported == false) { //await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => //{ // var result = TxtHelpChildren.Text + " : Problem => Torch not supported"; // TxtHelpChildren.Text = result; //}); return; } var captureElement = new CaptureElement(); captureElement.Source = MediaPhoneCapture; await MediaPhoneCapture.StartPreviewAsync(); ButtonLight.IsEnabled = true; MediaPhoneCapture.VideoDeviceController.TorchControl.Enabled = true; }); } catch (Exception) { MediaPhoneCapture = null; } finally { ButtonLight.IsEnabled = true; } } private async void SwitchOff() { try { if (CoreApplication.MainView.CoreWindow == null) return; var dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher; if (dispatcher == null) return; await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { if (MediaPhoneCapture == null) return; try { ButtonLight.IsEnabled = true; isOff = true; MediaPhoneCapture.VideoDeviceController.TorchControl.Enabled = false; MediaPhoneCapture.Dispose(); MediaPhoneCapture = null; } catch (Exception) { } }); } finally { ButtonLight.IsEnabled = true; } } /// /// Invoqué lorsque cette page est sur le point d'être affichée dans un frame. /// /// Données d'événement décrivant la manière dont l'utilisateur a accédé à cette page. /// Ce paramètre est généralement utilisé pour configurer la page. protected override void OnNavigatedTo(NavigationEventArgs e) { IsOn = int.Parse(e.Parameter.ToString()) == 1; if (IsOn) { isOff = false; SwitchOn(); } } private async void ButtonLight_Click(object sender, RoutedEventArgs e) { await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { ButtonLight.IsEnabled = false; if (isOff) { Border b = (VisualTreeHelper.GetChild(sender as Button, 0) as Border); b.BorderBrush = new SolidColorBrush(Windows.UI.Colors.Cyan); if (b.Child is Windows.UI.Xaml.Shapes.Path) { (b.Child as Windows.UI.Xaml.Shapes.Path).Stroke = new SolidColorBrush(Windows.UI.Colors.Cyan); } SwitchOn(); } else { Border b = (VisualTreeHelper.GetChild(sender as Button, 0) as Border); b.BorderBrush = new SolidColorBrush(Windows.UI.Colors.Gold); if (b.Child is Windows.UI.Xaml.Shapes.Path) { (b.Child as Windows.UI.Xaml.Shapes.Path).Stroke = new SolidColorBrush(Windows.UI.Colors.Gold); } SwitchOff(); } isOff = !isOff; }); } private void BCancel_Click(object sender, RoutedEventArgs e) { isOff = true; IsOn = false; SwitchOff(); Frame.Navigate(typeof(MainPage)); } } }