Using Accelerometer in Windows and Windows Phone Applications
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
public sealed partial class MainPage : Page | |
{ | |
private Accelerometer _accelerometer; | |
public MainPage() | |
{ | |
this.InitializeComponent(); | |
this.NavigationCacheMode = NavigationCacheMode.Required; | |
} | |
/// <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) | |
{ | |
_accelerometer = Accelerometer.GetDefault(); | |
if (_accelerometer != null) | |
{ | |
// Establish the report interval | |
uint minReportInterval = _accelerometer.MinimumReportInterval; | |
var desiredReportInterval = minReportInterval > 16 ? minReportInterval : 16; | |
_accelerometer.ReportInterval = desiredReportInterval; | |
try | |
{ | |
_accelerometer.ReadingChanged += async (a, b) => | |
{ | |
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => | |
{ | |
txtXAxis.Text = "X axis: " + b.Reading.AccelerationX.ToString("F"); | |
txtYAxis.Text = "Y axis: " + b.Reading.AccelerationY.ToString("F"); | |
txtZAxis.Text = "Z axis: " + b.Reading.AccelerationZ.ToString("F"); | |
}); | |
}; | |
} | |
catch (Exception ex) | |
{ | |
throw; | |
} | |
} | |
else | |
{ | |
new Windows.UI.Popups.MessageDialog("Accelerometer not found", "Error:").ShowAsync(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment