快捷搜索:  as  test  1111  test aNd 8=8  test++aNd+8=8  as++aNd+8=8  as aNd 8=8

亚博娱乐国际最新版本:深入学习Windows Phone7(一. Reactive Extension)



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.FromEventAccelerometerReadingEventArgs>(

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();

em亚博娱乐国际最新版本ulationDataAsObservable.Subscribe(args => InvokeAccelerometerReadingChanged(args));

}

static IEnumerableVector3> EmulateAccelerometerReading()

{

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 Ge亚博娱乐国际最新版本oCoordinateWatcher(GeoPositionAccuracy.Default);

watcher.MovementThreshold = 35;

var gcwo = Observable.FromEventGeoPositionChangedEventArgsGeoCoordinate>>(

eh =>watcher.PositionChanged +=eh,

eh =>watcher.PositionChanged -=eh);

watcher.Start();

private void StartLocationEmulation()

{

var position = EmulatePositionChangedEvents().ToObservable();

position.Subscribe(ev => wait_PositionChanged(null, ev));

}

stati亚博娱乐国际最新版本c IEnumerableGeoPositionChangedEventArgsGeoCoordinate>> 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 GeoPositionChangedEventArgsGeoCoordinate>(

new GeoPositionGeoCoor亚博娱乐国际最新版本dinate>(DateTimeOffset.Now, new GeoCoordinate(latitude, longitude)));

Thread.Sleep(random.Next(2000));

}

}

private void wait_PositionChanged(object sender, GeoPositionChangedEventArgsGeoCoordinate> e)

{

Deployment.Current.Dispatcher.BeginInvoke(()=>MyPositionChanged(e));

}

private void MyPositionChanged(GeoPositionChangedEventArgsGeoCoordinate> e)

{

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");

}

转自:http://www.cnblogs.com/randylee/archive/2010/09/01/1814654.html

免责声明:以上内容源自网络,版权归原作者所有,如有侵犯您的原创版权请告知,我们将尽快删除相关内容。

您可能还会对下面的文章感兴趣: