-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #698 from morning4coffe-dev/dev/doti/barometer-sample
feat: Add Barometer sample
- Loading branch information
Showing
2 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
Uno.Gallery/Uno.Gallery.Shared/Views/SamplePages/BarometerSamplePage.xaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<Page x:Class="Uno.Gallery.Views.Samples.BarometerSamplePage" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
xmlns:smtx="using:ShowMeTheXAML" | ||
xmlns:local="using:Uno.Gallery" | ||
mc:Ignorable="d"> | ||
|
||
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> | ||
<local:SamplePageLayout IsDesignAgnostic="True"> | ||
<local:SamplePageLayout.DesignAgnosticTemplate> | ||
<DataTemplate> | ||
<StackPanel> | ||
<smtx:XamlDisplay UniqueKey="BarometerSamplePage_Sample" | ||
smtx:XamlDisplayExtensions.IgnorePath="XX" | ||
smtx:XamlDisplayExtensions.Header="Barometer"> | ||
<Grid> | ||
<Grid.RowDefinitions> | ||
<RowDefinition Height="auto" /> | ||
<RowDefinition Height="auto" /> | ||
</Grid.RowDefinitions> | ||
<!-- C# code | ||
var barometer = Barometer.GetDefault(); | ||
barometer.ReadingChanged += Barometer_ReadingChanged; | ||
private async void Barometer_ReadingChanged(Barometer sender, BarometerReadingChangedEventArgs args) | ||
{ | ||
// If you want to update the UI in some way, ensure the Dispatcher is used, | ||
// as the ReadingChanged event handler does not run on the UI thread. | ||
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => | ||
{ | ||
OutputTextBlock.Text = $"Sensor reading in hPa = {args.Reading.StationPressureInHectopascals}, " + | ||
$"timestamp = {args.Reading.Timestamp}"; | ||
}); | ||
} | ||
--> | ||
|
||
<TextBlock Visibility="{Binding Data.BarometerAvailable}" | ||
Margin="0,0,0,20"> | ||
<LineBreak /> | ||
<Span FontWeight="Bold">Pressure (hPA): </Span> | ||
<Run Text="{Binding Data.Pressure}" /> | ||
<LineBreak /> | ||
<Span FontWeight="Bold">Last read timestamp: </Span> | ||
<Run Text="{Binding Data.LastReadTimestamp}" /> | ||
</TextBlock> | ||
|
||
<Button IsEnabled="{Binding Data.BarometerAvailable}" | ||
Grid.Row="1" | ||
Click="ObserveReadingChangeButton_Click"> | ||
<TextBlock Text="{Binding Data.ButtonContent}" | ||
VerticalAlignment="Stretch" | ||
TextAlignment="Center" /> | ||
</Button> | ||
</Grid> | ||
</smtx:XamlDisplay> | ||
</StackPanel> | ||
</DataTemplate> | ||
</local:SamplePageLayout.DesignAgnosticTemplate> | ||
|
||
</local:SamplePageLayout> | ||
</Grid> | ||
</Page> |
85 changes: 85 additions & 0 deletions
85
Uno.Gallery/Uno.Gallery.Shared/Views/SamplePages/BarometerSamplePage.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<double>(); set => SetProperty(value); } | ||
public string ButtonContent { get => GetProperty<string>(); set => SetProperty(value); } | ||
public DateTimeOffset? LastReadTimestamp { get => GetProperty<DateTimeOffset?>(); set => SetProperty(value); } | ||
public bool ObservingReadingChange { get => GetProperty<bool>(); 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; | ||
Check warning on line 68 in Uno.Gallery/Uno.Gallery.Shared/Views/SamplePages/BarometerSamplePage.xaml.cs GitHub Actions / Build and Deploy Job
|
||
ButtonContent = _stopObservingContent; | ||
ObservingReadingChange = true; | ||
} | ||
|
||
public void StopObservingReadingChange() | ||
{ | ||
_barometer.ReadingChanged -= Barometer_ReadingChanged; | ||
Check warning on line 75 in Uno.Gallery/Uno.Gallery.Shared/Views/SamplePages/BarometerSamplePage.xaml.cs GitHub Actions / Build and Deploy Job
|
||
ButtonContent = _startObservingContent; | ||
ObservingReadingChange = false; | ||
} | ||
|
||
private void Barometer_ReadingChanged(Barometer sender, BarometerReadingChangedEventArgs args) | ||
{ | ||
LastReadTimestamp = args.Reading.Timestamp; | ||
Pressure = args.Reading.StationPressureInHectopascals; | ||
} | ||
} |