【IT168技术】Reactive Extension for .Net framework
.Net反应性框架,在之前的.net框架中就已经有了,没想到也被应用到Windows phone7中来了。
程序员为指定异步操作指定回调,或事件处理程序的时候使用反应性编程。在异步操作完成或者事件触发的时候,就会调用方法并作为对该事件的反应。在这个模型中,数据流、异步请求、事件都可以做为可观察序列,并且接收异步消息,并通过LINQ来合成这些序列。
不过在Phone7中,这个框架只有一部分有用,主要用在:filtering events、composing multiple asynchronous web service requests、emulating data streams.
对于目前来说,在模拟器上没有办法测试Accelerometer和Location的API,就是因为得不到数据。现在可以利用这个框架来实现数据模拟了。
模拟Accelerometer数据:
Thread thread = new Thread(StartAccelerometerEmulation);
thread.Start();
am = new Accelerometer();
var aro = Observable.FromEvent
eh => am.ReadingChanged += eh,
eh => am.ReadingChanged -= eh);
try
{
am.Start();
}
catch (AccelerometerFailedException ae)
{
}
void InvokeAccelerometerReadingChanged(Vector3 data)
{
Deployment.Current.Dispatcher.BeginInvoke(() => AccelerometerReadingChanged(data));
}
void AccelerometerReadingChanged(Vector3 data)
{
this.textBlock1.Text = “x: ” + data.X.ToString(“0.00″);
this.textBlock2.Text = ” y: ” + data.Y.ToString(“0.00″);
this.textBlock3.Text = ” z: ” + data.Z.ToString(“0.00″);
}
private void StartAccelerometerEmulation()
{
var emulationDataAsObservable = EmulateAccelerometerReading().ToObservable();
emulationDataAsObservable.Subscribe(args => InvokeAccelerometerReadingChanged(args));
}
static IEnumerable
{
Random random = new Random();
for (double theta = 0; ; theta += .1)
{
Vector3 reading = new Vector3((float)Math.Sin(theta), (float)Math.Cos(theta * 1.1), (float)Math.Sin(theta * .7));
reading.Normalize();
if (random.NextDouble() > .95)
{
reading = new Vector3((float)(random.NextDouble() * 3.0 – 1.5),
(float)(random.NextDouble() * 3.0 – 1.5),
(float)(random.NextDouble() * 3.0 – 1.5));
}
yield return reading;
Thread.Sleep(200);
}
}

模拟Location数据:
Thread t = new Thread(StartLocationEmulation);
t.Start();
watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default);
watcher.MovementThreshold = 35;
var gcwo = Observable.FromEvent>(
eh =>watcher.PositionChanged +=eh,
eh =>watcher.PositionChanged -=eh);
watcher.Start();
private void StartLocationEmulation()
{
var position = EmulatePositionChangedEvents().ToObservable();
position.Subscribe(ev => wait_PositionChanged(null, ev));
}
static IEnumerable> EmulatePositionChangedEvents()
{
Random random = new Random();
while (true)
{
double latitude = (random.NextDouble() * 180.0) – 90.0; // latitude is between -90 and 90
double longitude = (random.NextDouble() * 360.0) – 180.0; // longitude is between -180 and 180
yield return new GeoPositionChangedEventArgs
new GeoPosition
Thread.Sleep(random.Next(2000));
}
}
private void wait_PositionChanged(object sender, GeoPositionChangedEventArgs
{
Deployment.Current.Dispatcher.BeginInvoke(()=>MyPositionChanged(e));
}
private void MyPositionChanged(GeoPositionChangedEventArgs
{
this.textBlock1.Text = “Time: ” + e.Position.Timestamp. ToString(“yyyy-MM-dd hh:mm:ss”);
this.textBlock2.Text = “Latitude: ” + e.Position.Location.Latitude.ToString(“0.00″);
this.textBlock3.Text = “Longitude: ” + e.Position.Location.Longitude.ToString(“0.00″);
}
