Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Barometer sample #698

Merged
4 changes: 3 additions & 1 deletion Uno.Gallery/Uno.Gallery.Shared/Uno.Gallery.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,9 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</_Globbled_Page>
<Page Include="@(_Globbled_Page)" />
<Page Include="@(_Globbled_Page)">
<SubType>Designer</SubType>
</Page>
<_Globbed_Compile Include="$(MSBuildThisFileDirectory)**/*.xaml.cs" Exclude="@(Compile)">
<DependentUpon>%(Filename)</DependentUpon>
</_Globbed_Compile>
Expand Down
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"
morning4coffe-dev marked this conversation as resolved.
Show resolved Hide resolved
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>
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;
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;
}
}