diff --git a/Uno.Gallery/Views/SamplePages/SimpleOrientationSamplePage.xaml.cs b/Uno.Gallery/Views/SamplePages/SimpleOrientationSamplePage.xaml.cs index 2c3907a36..d8b424729 100644 --- a/Uno.Gallery/Views/SamplePages/SimpleOrientationSamplePage.xaml.cs +++ b/Uno.Gallery/Views/SamplePages/SimpleOrientationSamplePage.xaml.cs @@ -2,12 +2,17 @@ using System.ComponentModel; using Uno.Gallery.ViewModels; using Windows.Devices.Sensors; +using Windows.ApplicationModel.Core; +using Windows.UI.Core; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; namespace Uno.Gallery.Views.Samples { - [SamplePage(SampleCategory.NonUIFeatures, "Simple Orientation", Description = "This sensor detects the current quadrant orientation of the specified device as well as its face-up or face-down status.", DocumentationLink = "https://learn.microsoft.com/en-us/uwp/api/windows.devices.sensors.simpleorientationsensor")] + [SamplePage( + SampleCategory.NonUIFeatures, + "Simple Orientation", Description = "This sensor detects the current quadrant orientation of the specified device as well as its face-up or face-down status.", + DocumentationLink = "https://learn.microsoft.com/en-us/uwp/api/windows.devices.sensors.simpleorientationsensor")] public sealed partial class SimpleOrientationSamplePage : Page { public SimpleOrientationSamplePage() @@ -17,15 +22,15 @@ public SimpleOrientationSamplePage() private void ObserveReadingChangeButton_Click(object sender, RoutedEventArgs e) { - if ((sender as Button)?.DataContext is SimpleOrientationSamplePageViewModel viewModel) + if (sender is Button { DataContext: SimpleOrientationSamplePageViewModel viewModel }) { - if (!viewModel.ObservingReadingChange) + if (viewModel.ObservingReadingChange) { - viewModel.StartObserveReadingChange(); + viewModel.StopObservingReadingChange(); } else { - viewModel.StopObservingReadingChange(); + viewModel.StartObserveReadingChange(); } } } @@ -35,34 +40,37 @@ public class SimpleOrientationSamplePageViewModel : ViewModelBase { private const string _startObservingContent = "Start observing orientation changes"; private const string _stopObservingContent = "Stop observing orientation changes"; - private const string _noSensorAvailable = "SimpleOrientationSensor is not available on this device/platform"; - - private readonly SimpleOrientationSensor _simpleOrientationSensor; + private const string _noSensorAvailableContent = "SimpleOrientationSensor is not available on this device/platform"; + private readonly SimpleOrientationSensor? _simpleOrientationSensor; public DateTimeOffset? LastReadTimestamp { get => GetProperty(); set => SetProperty(value); } - public string ButtonContent { get => GetProperty(); set => SetProperty(value); } public bool ObservingReadingChange { get => GetProperty(); set => SetProperty(value); } public SimpleOrientation Orientation { get => GetProperty(); set => SetProperty(value); } - public bool SimpleOrientationAvailable => _simpleOrientationSensor != null; + public bool SimpleOrientationAvailable => _simpleOrientationSensor is not null; public SimpleOrientationSamplePageViewModel() { _simpleOrientationSensor = SimpleOrientationSensor.GetDefault(); - if (_simpleOrientationSensor != null) + if (SimpleOrientationAvailable) { ButtonContent = _startObservingContent; } else { - ButtonContent = _noSensorAvailable; + ButtonContent = _noSensorAvailableContent; } } public void StartObserveReadingChange() { + if (_simpleOrientationSensor is null) + { + return; + } + _simpleOrientationSensor.OrientationChanged += SimpleOrientationSensor_OrientationChanged; ButtonContent = _stopObservingContent; ObservingReadingChange = true; @@ -70,15 +78,22 @@ public void StartObserveReadingChange() public void StopObservingReadingChange() { - _simpleOrientationSensor.OrientationChanged -= SimpleOrientationSensor_OrientationChanged; + if (_simpleOrientationSensor is not null) + { + _simpleOrientationSensor.OrientationChanged -= SimpleOrientationSensor_OrientationChanged; + } + ButtonContent = _startObservingContent; ObservingReadingChange = false; } - private void SimpleOrientationSensor_OrientationChanged(SimpleOrientationSensor sender, SimpleOrientationSensorOrientationChangedEventArgs args) + private async void SimpleOrientationSensor_OrientationChanged(SimpleOrientationSensor sender, SimpleOrientationSensorOrientationChangedEventArgs args) { - LastReadTimestamp = args.Timestamp; - Orientation = args.Orientation; + await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => + { + LastReadTimestamp = args.Timestamp; + Orientation = args.Orientation; + }); } } }