简体   繁体   English

Windows Phone 8.1加速度计和更新UI

[英]Windows phone 8.1 Accelerometer and updating UI

I having problems finding documentation or examples regarding the proper way to use the Accelerometer as well as updating a textbox using Dispatcher. 我在查找有关使用加速度计的正确方法以及使用分派器更新文本框的文档或示例时遇到问题。 Seems like it should be easy, but all the examples are 8.0 which apparently used using Microsoft.Devices.Sensors; 似乎应该很容易,但是所有示例都是8.0,显然使用了Microsoft.Devices.Sensors;。

Apparently you are supposed to use using Windows.Devices.Sensors; 显然您应该使用Windows.Devices.Sensors; which you would think are the same, but things like Start() and Stop methods no longer exist. 您会认为它们是相同的,但是Start()和Stop方法之类的东西不再存在。 I was trying to use a Microsoft AccelerometerHelper class from Microsoft here 我试图使用Microsoft AccelerometerHelper类从微软在这里

Also the older Dispatcher is now CoreDispatcher based on this ? 另外,较早的Dispatcher现在是基于此的 CoreDispatcher吗?

Also was trying to use this library, but it also uses the old namespace: http://code.msdn.microsoft.com/wpapps/Shake-Gesture-Library-04c82d5f 也试图使用此库,但它也使用旧的名称空间: http : //code.msdn.microsoft.com/wpapps/Shake-Gesture-Library-04c82d5f

Can anyone either provide an example or direct me to some information on how in 8.1, you would register the Accelerometer events and update the UI textbox with something like the x-axis? 谁能提供一个示例或向我介绍一些有关在8.1中如何注册加速计事件并使用x轴等内容更新UI文本框的信息?

Thank you very much! 非常感谢你!

Try this code for Windows Phone 8.1 Silverlight application. 对于Windows Phone 8.1 Silverlight应用程序,请尝试使用此代码。 It uses Accelerometer class from Microsoft.Devices.Sensors 它使用Microsoft.Devices.Sensors Accelerometer

// initialize
Accelerometer accelerometer = new Accelerometer();
accelerometer.CurrentValueChanged += accelerometer_CurrentValueChanged;
accelerometer.Start();

void accelerometer_CurrentValueChanged(object sender, SensorReadingEventArgs<AccelerometerReading> e)
{
    Dispatcher.BeginInvoke(() =>
    {
        XAxisText.Text = e.SensorReading.Acceleration.X.ToString();
    });
}

For Windows Phone Universal application use next code: 对于Windows Phone Universal应用程序,请使用以下代码:

Accelerometer accelerometer = Accelerometer.GetDefault();
accelerometer.ReadingChanged += accelerometer_ReadingChanged; 

async void accelerometer_ReadingChanged(Accelerometer sender, AccelerometerReadingChangedEventArgs args)
{
    await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    {
        XAxisText.Text = args.Reading.AccelerationX.ToString();
    });            
}

I've got the same problem and could't figure out how to make Shake event work. 我遇到了同样的问题,无法弄清楚Shake事件的工作方式。 Then I found this examples on Microsoft site http://code.msdn.microsoft.com/Windows-Phone-81-samples-08631ca7 In the Accelerometer solution I found very nice description "Windows Phone does not have an accelerometer shake event@. So it seems like we're have to write our own implementation of Shake Event) 然后,我在Microsoft网站http://code.msdn.microsoft.com/Windows-Phone-81-samples-08631ca7上找到了这个示例。因此,似乎我们必须编写自己的Shake Event实现)

I ended up using Silverlight 8.1 for this particular app per Microsoft. 我最终在每个Microsoft的特定应用程序中使用了Silverlight 8.1。 They are "looking" into why the phone API is the only one without a shake event. 他们正在“研究”为什么电话API是唯一一个没有震动事件的API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM