Skip to content

Commit e8f76fa

Browse files
committed
xamarin essentials example
1 parent 5467031 commit e8f76fa

File tree

3 files changed

+152
-1
lines changed

3 files changed

+152
-1
lines changed

ReactiveExtensionExamples/Pages/NavigationContainerPage.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ public NavigationContainerPage ()
7777
case NavigationPages.RxUiLogin:
7878
selectedPage = new Pages.ReactiveUiLogin();
7979
break;
80+
case NavigationPages.RxUiXamarinEssentials:
81+
selectedPage = new Pages.ReactiveUiEssentials();
82+
break;
8083
default:
8184
break;
8285
}
@@ -108,7 +111,8 @@ enum NavigationPages {
108111
RxUiLogin,
109112
StandardSearch,
110113
SearchWithReactiveExtensions,
111-
RxUiSearch
114+
RxUiSearch,
115+
RxUiXamarinEssentials
112116
}
113117

114118
class NavListPage : ContentPage {
@@ -226,6 +230,12 @@ public NavListPage () {
226230
Command = new Command(() => selectedNavigation.OnNext(NavigationPages.RxUiLogin)),
227231
};
228232
navigationContainer.Children.Add(rxuiLogin);
233+
234+
var rxuiEssentials = new Button {
235+
Text = "RxUI - Xamarin Essentials",
236+
Command = new Command(() => selectedNavigation.OnNext(NavigationPages.RxUiXamarinEssentials)),
237+
};
238+
navigationContainer.Children.Add(rxuiEssentials);
229239

230240
var reactiveLogo = new Image {
231241
Source = "reactive_logo.png",
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
using System.Reactive.Disposables;
3+
using System.Reactive.Linq;
4+
using ReactiveUI;
5+
using Xamarin.Forms;
6+
using System.Threading.Tasks;
7+
using ReactiveUI.XamForms;
8+
9+
namespace ReactiveExtensionExamples.Pages
10+
{
11+
public class ReactiveUiEssentials : ReactiveContentPage<ViewModels.XamarinEssentials>
12+
{
13+
BoxView colorDisplay;
14+
15+
Slider x, y, z;
16+
17+
public ReactiveUiEssentials ()
18+
{
19+
ViewModel = new ReactiveExtensionExamples.ViewModels.XamarinEssentials ();
20+
21+
Title = "RxUI - Xamarin Essentials";
22+
23+
Content = new StackLayout {
24+
Padding = new Thickness(40d),
25+
Children = {
26+
(colorDisplay = new BoxView{ HeightRequest = 250 }),
27+
28+
new Label{ Text = "X"},
29+
(x = new Slider(-1, 1, 0){ InputTransparent = true }),
30+
31+
new Label{ Text = "Y"},
32+
(y = new Slider(-1, 1, 0){ InputTransparent = true }),
33+
34+
new Label{ Text = "Z"},
35+
(z = new Slider(-1, 1, 0){ InputTransparent = true })
36+
}
37+
};
38+
39+
this.WhenActivated((CompositeDisposable disposables) =>
40+
{
41+
this.OneWayBind (ViewModel, vm => vm.X, c => c.x.Value)
42+
.DisposeWith(disposables);
43+
44+
this.OneWayBind (ViewModel, vm => vm.Y, c => c.y.Value)
45+
.DisposeWith(disposables);
46+
47+
this.OneWayBind (ViewModel, vm => vm.Z, c => c.z.Value)
48+
.DisposeWith(disposables);
49+
50+
this.WhenAnyValue (x => x.ViewModel.Color)
51+
.ObserveOn (RxApp.MainThreadScheduler)
52+
.Select (color => Color.FromRgba (color.R, color.G, color.B, color.A))
53+
.BindTo (this, x => x.colorDisplay.BackgroundColor)
54+
.DisposeWith(disposables);
55+
});
56+
}
57+
}
58+
}
59+
60+
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using System.Linq;
2+
using ReactiveUI;
3+
using System.Reactive.Disposables;
4+
using System.Reactive.Linq;
5+
using Xamarin.Essentials;
6+
using System.Numerics;
7+
using System;
8+
using System.Drawing;
9+
10+
namespace ReactiveExtensionExamples.ViewModels
11+
{
12+
public class XamarinEssentials : ViewModelBase
13+
{
14+
ObservableAsPropertyHelper<double> _x;
15+
public double X => _x.Value;
16+
17+
ObservableAsPropertyHelper<double> _y;
18+
public double Y => _y.Value;
19+
20+
ObservableAsPropertyHelper<double> _z;
21+
public double Z => _z.Value;
22+
23+
ObservableAsPropertyHelper<Color> _color;
24+
public Color Color => _color?.Value ?? default(Color);
25+
26+
public XamarinEssentials ()
27+
{
28+
this.WhenActivated(
29+
(CompositeDisposable disposables) =>
30+
{
31+
var gyroscopeChanged =
32+
Observable
33+
.FromEvent<AccelerometerChangedEventHandler, AccelerometerChangedEventArgs>(
34+
x => Accelerometer.ReadingChanged += x,
35+
x => Accelerometer.ReadingChanged -= x)
36+
.SubscribeOn(RxApp.TaskpoolScheduler)
37+
.Select(x => x.Reading.Acceleration)
38+
.StartWith(Vector3.Zero)
39+
.Do(x =>
40+
{
41+
if (!Accelerometer.IsMonitoring)
42+
Accelerometer.Start(SensorSpeed.Ui);
43+
})
44+
.Finally(() => {
45+
Accelerometer.Stop();
46+
})
47+
.Throttle(TimeSpan.FromMilliseconds(20))
48+
.Do(x => System.Diagnostics.Debug.WriteLine($"ACC: {x}"))
49+
.Publish()
50+
.RefCount();
51+
52+
gyroscopeChanged
53+
.Select(x => (double)x.X)
54+
.ToProperty(this, x => x.X, out _x)
55+
.DisposeWith(disposables);
56+
57+
gyroscopeChanged
58+
.Select(x => (double)x.Y)
59+
.ToProperty(this, x => x.Y, out _y)
60+
.DisposeWith(disposables);
61+
62+
gyroscopeChanged
63+
.Select(x => (double)x.Z)
64+
.ToProperty(this, x => x.Z, out _z)
65+
.DisposeWith(disposables);
66+
67+
gyroscopeChanged
68+
.Select(x => {
69+
var r = (int)Math.Min(255, Math.Abs(x.X * 255));
70+
var g = (int)Math.Min(255, Math.Abs(x.Y * 255));
71+
var b = (int)Math.Min(255, Math.Abs(x.Z * 255));
72+
return Color.FromArgb(r, g, b);
73+
})
74+
.ToProperty(this, x => x.Color, out _color)
75+
.DisposeWith(disposables);
76+
77+
});
78+
}
79+
}
80+
}
81+

0 commit comments

Comments
 (0)