WP7仿真器上的加速度计支持(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/emulation/accelerometer-support-on-wp-emulator-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 9 分钟阅读 - 4449 个词 阅读量 0WP7仿真器上的加速度计支持(译文)
原文地址:https://www.codeproject.com/Articles/136488/Accelerometer-Support-on-WP-Emulator
原文作者:maciekgrabek
译文由本站 robot-v1.0 翻译
前言
The Windows Phone 7 emulator doesn’t support accelerometer. This project will help you to test accelerometer enabled applications on the Windows Phone 7 emulator!
Windows Phone 7模拟器不支持加速度计.该项目将帮助您在Windows Phone 7模拟器上测试启用了加速度计的应用程序!
内容(Contents)
- 介绍(Introduction)
- 怎么运行的? (最终用户观点)(How It Works? (End User Perspective))
- 引擎盖下(Under the Hood)
- 在哪里找到它?(Where to Find It?)
- 将来的版本中会出现什么?(What Will Appear in Future Versions?)
介绍(Introduction)
众所周知,Windows Phone 7 Emulator中不支持加速度计.如果要创建一些基于加速度计的应用程序,则必须将其部署到Windows Phone驱动的设备上,然后对其进行测试.当然,有些项目正在使用某些硬件(例如Wii或XBox 360控制器)来模拟移动设备的移动.但是当您尝试使用它时,您会发现他们有一个弱点-他们需要硬件!有一天,当我有更多的空闲时间时,我决定为此找到一些解决方案.几个小时后,我找到了一个,就是这样…我想请您介绍(As all we know, there is no support for accelerometer in Windows Phone 7 Emulator. If you are creating some accelerometer bases application, you have to deploy it to your Windows Phone powered device and then test it. Of course, there are projects which are using some hardware such as Wii or XBox 360 controller to emulate mobile device movement. But when you try to use it, it strikes you that they have one weak point - they need hardware! Some day, when I had more free time, I decided to find some solution for this. After a couple of hours, I have found one and this is it… I want you to introduce) WP7加速度计仿真器项目(WP7 Accelerometer Emulator project) .让我们仔细看看它是什么.(. Let’s take a closer look at what it is.)
加速度计力向量.资源:(Accelerometer force vectors. Source:) Windows Phone开发人员博客(The windows Phone Developer Blog)
怎么运行的? (最终用户观点)(How It Works? (End User Perspective))
解决方案由相互关联的三个项目组成:(Solution consists of three projects which are connected each other:)
MaciejGrabek.WP7Accelerometer
-这是在项目中使用的核心库.它包含(- This is the core library to use in your projects. It contains)WP7AccelerometerProvider
提供真正的加速度计提供的所有功能的类(class which provides all functionality given by real Accelerometer from)Microsoft.Devices.Sensors
,即(, i.e.,)Start
和(and)Stop
方法和(methods and)ReadingChanged
事件,它提供有关每个轴的重力的信息.没有(event which provides information about gravity for each axis. There is no)IAccelerometer
接口来实现,因此我只复制了加速器类清单中所有已使用项的名称.(interface to implement, so I simply copied names of all used items from accelerator class manifest.)MaciejGrabek.WP7AccelerometerEmulatorUI
-此项目使您可以通过简单地使用三个滚动条来旋转X,Y和Z来模拟手机的位置.它将所有必需的数据发送到代理.(- This project allows you to emulate position of phone by simple use of three scrolls for X, Y and Z rotation. It sends all required data to Proxy.)MaciejGrabek.WP7AccelerometerEmulatorProxy
–(-)该代理用于从UI接收数据,并允许从其中读取数据(This proxy is used to receive data from UI and allows to read it from)WP7AccelerometerProvider
.(.) 如果您正在模拟器上运行应用程序,则(If you are running your application on emulator, then)WP7AccelerometerProvider
正在尝试从Proxy读取数据-如果在检索此数据时出了点问题(例如,代理不可用),它只会切换到随机数据模式.您可以尝试通过简单地使用来尝试从代理接收数据(is trying to read data from Proxy - if something goes wrong on retrieving this data (for example, proxy is unavailable), it simply switches into random data mode. You can retry receiving data from proxy by simple use of)Stop
和(and)Start
方法.另一种方法是重新启动应用程序.当然,如果您将在设备上运行该应用程序,它将不会使用代理,但是它将为您提供内置于加速度计数据中的硬件.(methods. Another way is to restart application. Of course, if you will run the application on device it won’t use proxy, but it will provide you hardware built in accelerometer data.)
如何使用它?(How To Use It?)
这非常简单-就像使用真实的加速度计一样.(It is very simple - like using real accelerometer.)
创建WP7应用程序时,您需要做的第一件事就是添加对(When you create WP7 application, the first thing you need to do is add a reference to) MaciejGrabek.WP7Accelerometer
库(或项目).然后创建一个加速度计对象,如下所示:(library (or project). Then create an accelerometer object like below:)
using MaciejGrabek.WP7Accelerometer;
var acc = new WP7AccelerometerProvider();
现在只需添加处理(Now simply add handling to) AccelerometerProviderReadingChanged
事件并使用由提供的数据(event and consume data provided by) WP7AccelerometerReadingEventArgs
类:(class:)
acc.ReadingChanged += OnAccelerometerProviderReadingChanged;
private void OnAccelerometerProviderReadingChanged
(object sender, WP7AccelerometerReadingEventArgs args)
{
//do what you want with data
ProcessAccelerometerData(args.X, args.Y, args.Z, args.Timestamp);
}
开始(Start) WP7AccelerometerProvider
并准备好获取数据.(and be ready to get the data.)
try
{
acc.Start();
}
catch (Exception exc)
{
txtblk.Text = exc.Message;
}
这是您在应用程序中需要做的所有事情.现在让我们看一下控制面板.为此,您需要开始(This is all you need to do in the application. Now let’s see the control panel. To do this, you need to start) MaciejGrabek.WP7AccelerometerEmulatorUI
和(and) MaciejGrabek.WP7AccelerometerEmulatorProxy
(它应该自动以((it should start automatically with) MaciejGrabek.WP7AccelerometerEmulatorUI
,但如果不能确保手动执行此操作-否则,您将获得随机数据).在仿真器和其他设备上运行WP7应用程序(, but if not be sure to do this manually - otherwise, you will get random data). Run your WP7 application on emulator and on) MaciejGrabek.WP7AccelerometerEmulatorUI
标记"(mark “)发送数据到模拟器(Send data to emulator)“复选框,祝您玩得开心!如果您不勾选,就不要打扰”(” checkbox and have fun! If you will leave unchecked “)发送数据到模拟器(Send data to emulator)"(") AccelerometerProvider
只会读取代理上的内容,而无需更改接口.结果如下所示:(will simply read what is on proxy without any change on interface. The result is shown below:)
如您所见,它非常简单直观.(As you can see, it is very simple and intuitive to use.)
在本部分的最后,让我们比较一下(For the end of this part, let’s compare) Microsoft.Devices.Sensors.Accelerometer
和(and) MaciejGrabek.WP7Accelerometer.WP7AccelerometerProvider
:(:)
加速度计(Accelerometer) | WP7加速度计提供商(WP7AccelerometerProvider) |
---|---|
如您所见,这两个类非常相似.没有要实现的接口(例如(As you can see, both classes are very similar. There is no interface to implement (for example) IAccelerometer
),因此唯一的方法是创建"相似"类.这对于此仿真器的"最终用户"来说很重要.现在让我们看看它是如何工作的. :)() so the only way was to create “similar” class. This is all that is important for the “end user” of this emulator. Now let’s see how it really works. :))
引擎盖下(Under the Hood)
首先,我应该解释一下有关该项目中数据流的更多信息.如下所示,主要沟通是一种方式.这意味着对于UI项目而言,重要的是将数据发送到代理.从另一面看(First of all, I should explain a little bit more about data flow in this project. As you can see below, the main communication is one way. It means that all that is important for UI project is to send data to the proxy. From the other side, the) WP7AccelerometerProvider
负责从代理或设备(优先级更高)中读取此数据.(is responsible to read this data from proxy or from device (which has higher priority).)
代理(Proxy)
作为代理,我决定使用WCF服务,该服务允许使用简单的”(As a proxy, I decided to use WCF service that allows to get data using simple “) HTTP GET
“.这是服务合同及其实施的外观.(”. Here is how service contract and its implementation look.)
[ServiceContract(Namespace = "http://maciejgrabek.com/WP7AccelerometerEmulatorProxy")]
public interface IAccelerometerEmulatorProxy
{
[OperationContract]
[WebGet]
string GetData();
[OperationContract]
void SetData(float x, float y, float z);
}
[AspNetCompatibilityRequirements(RequirementsMode =
AspNetCompatibilityRequirementsMode.Allowed)]
public class AccelerometerEmulatorProxy : IAccelerometerEmulatorProxy
{
public string GetData()
{
AccelerometerVector v = HttpRuntime.Cache
[AccelerometerVector.CacheKey] as AccelerometerVector;
if (v != null)
{
return v.ToString();
}
else
{
return AccelerometerVector.Empty.ToString();
}
}
public void SetData(float x, float y, float z)
{
AccelerometerVector v = new AccelerometerVector()
{
X = x,
Y = y,
Z = z
};
HttpRuntime.Cache.Remove(AccelerometerVector.CacheKey);
HttpRuntime.Cache.Add(AccelerometerVector.CacheKey,
v,
null,
DateTime.MaxValue,
Cache.NoSlidingExpiration,
CacheItemPriority.Default,
delegate { });
}
}
重要的是要使用(The important thing is to use) [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
和(and) [WebGet]
以便与WCF服务进行这种通信.(to enable this kind of communication with WCF service.)
如您所见,为了从UI存储数据,我使用内置的缓存,因为它既快速又有效.整个逻辑不是火箭科学,因为我遵循"保持简单"的规则. :)(As you can see, to store data from UI, I am using built in cache because it is quick and efficient. The whole logic is not any kind of rocket science, because I followed the “Keep It Simple” rule. :))
用户界面(UI)
上(On) MaciejGrabek.WP7AccelerometerEmulatorUI
,有一些基于XNA类的计算,这些都是获得重力基础向量的正确值所必需的. 3D图形计算是一个完全不同的主题,因此我会让XNA爱好者使用.将所有操作数据发送到Proxy之后,该数据将在此存储并准备好传输到Windows Phone仿真器.所有这些计算和数据传输每秒执行10次.如果要更改,有一种方法叫做(, there are some XNA classes based calculations, which are required to get proper values of gravity force base vectors. 3D graphic computations are quite a different topic so I will let it for XNA fans. After all operations data is sent to Proxy where it is stored and ready to transfer to Windows Phone Emulator. All these calculations and data transfer is executed 10 times per second. If you want to change, there is method called) InitializeAndRunTimer
您可以在其中更改计时器的频率,但我认为10Hz可以.(where you can change frequency of timer, but I thing that 10Hz is quite ok.)
private void InitializeAndRunTimer()
{
_timer = new DispatcherTimer();
_timer.Interval = TimeSpan.FromMilliseconds(100);
_timer.Tick += delegate
{
PerformSingleStep();
};
_timer.Start();
}
核心(Core)
现在是时候查看负责从"适当"源读取数据的核心库了.我当然在想(This is the time to see the core library which is responsible for reading data from “proper” source. Of course, I am thinking about) MaciejGrabek.WP7Accelerometer
和(and) WP7AccelerometerProvider
类.正如我之前提到的,它类似于真实的加速度计定义.最重要的是认识当前环境.您可以在这种情况下使用(class. As I mentioned before, it is similar to real Accelerometer definition. The most important thing is to recognize current environment. You can use in this case) Microsoft.Devices.Environment.DeviceType
属性和(property and ) Microsoft.Devices.DeviceType
`` 枚举器.如果等于(enumerator. If it is equal to) DeviceType.Device
,那么我们使用的是加速度计,否则((, then we are using Accelerometer, otherwise () DeviceType.Emulator
)我们正在初始化() we are initializing) WebClient
从代理获取数据.例如,我们可以使用(to get data from Proxy. As an example, we can use) Stop()
方法实现:(method implementation:)
[SecuritySafeCritical]
public void Stop()
{
if (Microsoft.Devices.Environment.DeviceType == DeviceType.Device)
{
_accelerometer.Stop();
}
else
{
this._timer.Stop();
}
}
在创建时(When you are creating) WP7AccelerometerProvider
实例,您可以指定两个参数.首先是(instance, you can specify two parameters. First of them is) dataUrl
具有默认值(with default value) http://localhost:9876/AccelerometerEmulatorProxy.svc/GetData
第二个是(and the second one is) refreshRate
设置为100(表示10Hz).当然,将这个值设置为小于33是没有意义的,因为当前的显示刷新率为每秒33次,因此即使我们读取数据,用户也看不到它.(which is set to 100 (it means 10Hz). Of course, there is no sense to set this value less than 33, because current display refresh rate is 33 times per second so even if we will read data, it won’t be visible for user.)
在测试此应用程序时,我在模拟器或Web服务器端发现了一些缓存问题.即使服务器上的数据已更改,也无法获取新版本.这就是使用一些技巧的原因:(When I was testing this application, I found some caching problem on emulator or web server side. Even if the data was changed on server, there was no way to get new version of it. That was the reason to use a little trick:)
if (!this._dataUrlError)
{
if (!this._client.IsBusy)
this._client.DownloadStringAsync(new Uri(String.Format("{0}?{1}",
this._dataUrl, this._rand.NextDouble())), null);
}
当我们向URL添加一些随机项时,可以确定我们的请求将返回未缓存的数据.如果从代理读取数据有问题(When we add some random item to URL, then we can be sure that our request will return not cached data. If there is some problem with data reading from proxy) WP7AccelerometerProvider
正在使用随机生成器来获取一些数据.重试从代理读取的唯一方法是致电(is using Random generator to get some data. The only way to retry reading from proxy is to call) Stop()
和(and) Start()
方法(methods on) WP7AccelerometerProvider
实例或只是重新启动应用程序.(instance or simply restarting application.)
在哪里找到它?(Where To Find It?)
我已经在二进制文件中附加了代码,但是将来我会对其进行修改和扩展.如果要获取当前版本,只需(I have attached code with binaries, but I will modify and extend it in future. If you want to get the current version, you can simply) 下载MaciejGrabek.WP7加速度计仿真器(download MaciejGrabek.WP7AccelerometerEmulator) 并在您的项目中使用它.我会将所有代码更新都放在这个地方,所以如果您有兴趣的话(and use it in your projects. I will put all updates of code on this place so if you are interested follow) 项目现场(project site) .当然,有一些演示可以确切地了解如何使用它,并将其附加到提供的解决方案中.(. There is, of course, some demo to see exactly how to use it and it is attached to the provided solution.)
将来的版本中会出现什么?(What Will Appear in Future Versions?)
- 有时,您会看到云台锁定效果-我正在尝试解决(Sometimes, you can see Gimbal Lock effect - I am currently trying to solve it)
- 添加鼠标控件进行旋转(Add mouse control for rotation)
- 添加运动序列(您可以记录运动(例如摇动,滚动等),保存运动并在需要时快速运行)(Add movement sequences (you can record movement like shaking, rolling, etc., save it, and quick run every time you need))
- 增加运动的额外加速度(抖动等)(Add additional acceleration for movement (shake, etc.)) 随时发表评论!(Fell free to comment!)
有用的链接(Useful Links)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
C# Windows-Phone-7 Mobile 新闻 翻译