forked from microsoft/Windows-universal-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scenario3_Porting.xaml.cs
114 lines (100 loc) · 4.66 KB
/
Scenario3_Porting.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
using SDKTemplate;
using System;
using Windows.Devices.Sensors;
using Windows.Foundation;
using System.Threading.Tasks;
using Windows.UI.Core;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkID=390556
namespace GyrometerCS
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class Scenario3_Porting : Page
{
// A pointer back to the main page. This is needed if you want to call methods in MainPage such
// as NotifyUser()
MainPage rootPage = MainPage.Current;
Gyrometer gyrometerWP;
Gyrometer gyrometerWindows;
public Scenario3_Porting()
{
this.InitializeComponent();
// Get two instances of the gyrometer:
// One that returns the raw gyrometer data
gyrometerWindows = Gyrometer.GetDefault();
// Other on which the 'ReadingTransform' is updated so that data returned aligns with the native WP orientation (portrait)
gyrometerWP = Gyrometer.GetDefault();
if (gyrometerWP == null || gyrometerWindows == null)
{
rootPage.NotifyUser("No gyrometer found", NotifyType.ErrorMessage);
}
else
{
// Assumption is that this app has been developed for Windows Phone 8.1 (or earlier)
// and hence assumes that the sensor returns readings in Portrait Mode, which may
// not be true when the app or sensor logic is being ported over to a
// Landscape-First Windows device
// While we encourage you to re-design your app as a universal app to gain access
// to many other advantages of developing a universal app, this scenario demonstrates
// a simple approach to let the runtime honor your assumption on the
// "native orientation" of the sensor.
gyrometerWP.ReadingTransform = Windows.Graphics.Display.DisplayOrientations.Portrait;
// If you were to go the route of universal app, make no assumptions about the
// native orientation of the device. Instead rely on using a display orientation
// (absolute or current) to enforce the reference frame for the sensor readings.
// (which is done by updating 'ReadingTransform' property with the appropriate orientation)
}
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached.
/// This parameter is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (gyrometerWP ==null || gyrometerWindows == null)
{
GetSampleButton.IsEnabled = false;
}
else
{
GetSampleButton.IsEnabled = true;
}
}
/// <summary>
/// Invoked when 'Get Sample' button is pressed.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void GetGyrometerSample(object sender, RoutedEventArgs e)
{
GyrometerReading wpReading;
GyrometerReading windowsReading;
GetSampleButton.IsEnabled = false;
// Establish the report interval
gyrometerWP.ReportInterval = gyrometerWindows.MinimumReportInterval;
gyrometerWindows.ReportInterval = gyrometerWindows.MinimumReportInterval;
wpReading = gyrometerWP.GetCurrentReading();
windowsReading = gyrometerWindows.GetCurrentReading();
if (null != wpReading)
{
ScenarioOutput_X_WP.Text = wpReading.AngularVelocityX.ToString();
ScenarioOutput_Y_WP.Text = wpReading.AngularVelocityY.ToString();
ScenarioOutput_Z_WP.Text = wpReading.AngularVelocityZ.ToString();
}
if (null != windowsReading)
{
ScenarioOutput_X_Windows.Text = windowsReading.AngularVelocityX.ToString();
ScenarioOutput_Y_Windows.Text = windowsReading.AngularVelocityY.ToString();
ScenarioOutput_Z_Windows.Text = windowsReading.AngularVelocityZ.ToString();
}
gyrometerWP.ReportInterval = 0;
gyrometerWindows.ReportInterval = 0;
GetSampleButton.IsEnabled = true;
}
}
}