diff --git a/Uno.Gallery/Uno.Gallery.Shared/Views/SamplePages/BarometerSamplePage.xaml b/Uno.Gallery/Uno.Gallery.Shared/Views/SamplePages/BarometerSamplePage.xaml new file mode 100644 index 000000000..68100967d --- /dev/null +++ b/Uno.Gallery/Uno.Gallery.Shared/Views/SamplePages/BarometerSamplePage.xaml @@ -0,0 +1,64 @@ + + + + + + + + + + + + + + + + + + Pressure (hPA): + + + Last read timestamp: + + + + + + + + + + + + + diff --git a/Uno.Gallery/Uno.Gallery.Shared/Views/SamplePages/BarometerSamplePage.xaml.cs b/Uno.Gallery/Uno.Gallery.Shared/Views/SamplePages/BarometerSamplePage.xaml.cs new file mode 100644 index 000000000..ec34e9f90 --- /dev/null +++ b/Uno.Gallery/Uno.Gallery.Shared/Views/SamplePages/BarometerSamplePage.xaml.cs @@ -0,0 +1,85 @@ +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Controls; +using System; +using Uno.Gallery.ViewModels; +using Windows.Devices.Sensors; + +namespace Uno.Gallery.Views.Samples +{ + [SamplePage(SampleCategory.NonUIFeatures, "Barometer", + Description = "Represents a Barometer sensor. This sensor returns pressure in hectopascals (hPa).", + DocumentationLink = "https://platform.uno/docs/articles/features/barometer.html", + DataType = typeof(BarometerSamplePageViewModel))] + public sealed partial class BarometerSamplePage : Page + { + public BarometerSamplePage() + { + this.InitializeComponent(); + } + + private void ObserveReadingChangeButton_Click(object sender, RoutedEventArgs e) + { + if (!(((Sample)DataContext).Data is BarometerSamplePageViewModel viewModel)) + return; + + + if (!viewModel.ObservingReadingChange) + { + viewModel.StartObserveReadingChange(); + } + else + { + viewModel.StopObservingReadingChange(); + } + } + } +} + +public class BarometerSamplePageViewModel : ViewModelBase +{ + private const string _startObservingContent = "Start observing barometer reading changes"; + private const string _stopObservingContent = "Stop observing barometer reading changes"; + private string _notAvailable = "Barometer is not available on this device/platform"; + + private readonly Barometer _barometer; + + public double Pressure { get => GetProperty(); set => SetProperty(value); } + public string ButtonContent { get => GetProperty(); set => SetProperty(value); } + public DateTimeOffset? LastReadTimestamp { get => GetProperty(); set => SetProperty(value); } + public bool ObservingReadingChange { get => GetProperty(); set => SetProperty(value); } + + public bool BarometerAvailable => _barometer != null; + + public BarometerSamplePageViewModel() + { + _barometer = Barometer.GetDefault(); + + if (BarometerAvailable) + { + ButtonContent = _startObservingContent; + return; + } + + ButtonContent = _notAvailable; + } + + public void StartObserveReadingChange() + { + _barometer.ReadingChanged += Barometer_ReadingChanged; + ButtonContent = _stopObservingContent; + ObservingReadingChange = true; + } + + public void StopObservingReadingChange() + { + _barometer.ReadingChanged -= Barometer_ReadingChanged; + ButtonContent = _startObservingContent; + ObservingReadingChange = false; + } + + private void Barometer_ReadingChanged(Barometer sender, BarometerReadingChangedEventArgs args) + { + LastReadTimestamp = args.Reading.Timestamp; + Pressure = args.Reading.StationPressureInHectopascals; + } +}