diff --git a/Uno.Gallery/Uno.Gallery.Shared/Views/SamplePages/ProximitySamplePage.xaml b/Uno.Gallery/Uno.Gallery.Shared/Views/SamplePages/ProximitySamplePage.xaml new file mode 100644 index 000000000..5457f90a9 --- /dev/null +++ b/Uno.Gallery/Uno.Gallery.Shared/Views/SamplePages/ProximitySamplePage.xaml @@ -0,0 +1,59 @@ + + + + + + + + + + + + + + Distance: + + mm + + + + + + + + + + + + + diff --git a/Uno.Gallery/Uno.Gallery.Shared/Views/SamplePages/ProximitySamplePage.xaml.cs b/Uno.Gallery/Uno.Gallery.Shared/Views/SamplePages/ProximitySamplePage.xaml.cs new file mode 100644 index 000000000..425acce5e --- /dev/null +++ b/Uno.Gallery/Uno.Gallery.Shared/Views/SamplePages/ProximitySamplePage.xaml.cs @@ -0,0 +1,145 @@ +using System; +using System.Linq; +using Uno.Gallery.ViewModels; +using Windows.ApplicationModel.Core; +using Windows.Devices.Enumeration; +using Windows.Devices.Sensors; +using Windows.UI.Core; +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Controls; +using System.Threading.Tasks; + +namespace Uno.Gallery.Views.Samples +{ + [SamplePage(SampleCategory.NonUIFeatures, "Proximity", + Description = "Represents a Proximit sensor. This sensor returns distance.", + DocumentationLink = "https://platform.uno/docs/articles/features/proximity-sensor.html", + DataType = typeof(ProximitySamplePageViewModel))] + public sealed partial class ProximitySamplePage : Page + { + public ProximitySamplePage () + { + this.InitializeComponent(); + Loaded += ProximitySamplePage_Loaded; + } + + private async void ProximitySamplePage_Loaded(object sender, RoutedEventArgs e) + { + if (!(((Sample)DataContext).Data is ProximitySamplePageViewModel viewModel)) + return; + await viewModel.OnLoaded(); + } + + private void ObserveReadingChangeButton_Click(object sender, RoutedEventArgs e) + { + if (!(((Sample)DataContext).Data is ProximitySamplePageViewModel viewModel)) + return; + + if (!viewModel.ObservingReadingChange) + { + viewModel.StartObserveReadingChange(); + } + else + { + viewModel.StopObservingReadingChange(); + } + } + } + + public class ProximitySamplePageViewModel : ViewModelBase + { + private const string _startObservingContent = "Start observing proximity reading changes"; + private const string _stopObservingContent = "Stop observing proximity reading changes"; + private string _notAvailable = "Proximity is not available on this device/platform"; + + private ProximitySensor _proximity = null; + + public string ButtonContent + { + get => GetProperty(); + set => SetProperty(value); + } + + public bool IsProximityAvailable + { + get => GetProperty(); + set => SetProperty(value); + } + + public bool ObservingReadingChange + { + get => GetProperty(); + set => SetProperty(value); + } + + public uint? Distance + { + get => GetProperty(); + set => SetProperty(value); + } + + public bool IsDetected + { + get => GetProperty(); + set => SetProperty(value); + } + + public bool ProximityAvailable => _proximity != null; + + + + public ProximitySamplePageViewModel() + { + IsProximityAvailable = false; + ButtonContent = _notAvailable; + } + + public void StartObserveReadingChange() + { + _proximity.ReadingChanged += Proximity_ReadingChanged; + ButtonContent = _stopObservingContent; + ObservingReadingChange = true; + } + + public void StopObservingReadingChange() + { + _proximity.ReadingChanged -= Proximity_ReadingChanged; + ButtonContent = _startObservingContent; + ObservingReadingChange = false; + } + + private async void Proximity_ReadingChanged(ProximitySensor sender, ProximitySensorReadingChangedEventArgs args) + { + await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => + { + Distance = args.Reading.DistanceInMillimeters; + IsDetected = args.Reading.IsDetected; + }); + } + + internal async Task OnLoaded() + { + var selector = ProximitySensor.GetDeviceSelector(); + var devices = await DeviceInformation.FindAllAsync(selector); + var device = devices.FirstOrDefault(); + if (device is not null) + { + +#if __ANDROID__ || !HAS_UNO + _proximity = ProximitySensor.FromId(device.Id); +#endif + } + + if (_proximity is not null) + { + await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => + { + IsProximityAvailable = true; + ButtonContent = _startObservingContent; + }); + return; + } + } + + } +}