我正在制作的wp7应用程序中使用反应式扩展,我希望以特定的间隔(间隔基于用户设置)获得当前位置。在这个例子中,假设每5秒钟后,我希望从GeoCoordinateWatcher中知道当前位置。
我读过一些我可以使用的地方。延迟(5秒),但这不只是延迟位置变化的流吗?由于我只在当前位置之后,会。延迟(5秒)。Last()为我想要的工作吗?
到目前为止我的代码
if (LocationServices == null)
LocationServices = new GeoCoordinateWatcher(GeoPositionAccuracy.High)
{
MovementThreshold = 2
};
// Take the first ready status from the GeoCoordinateWatcher
var status = (from o in Observable.FromEvent<GeoPositionStatusChangedEventArgs> LocationServices, "StatusChanged")
where o.EventArgs.Status == GeoPositionStatus.Ready
select o);
status.Subscribe();
var pos = (from s in status
from p in Observable.FromEvent<GeoPositionChangedEventArgs<GeoCoordinate>>(LocationServices, "PositionChanged")
select p.EventArgs.Position); // Do something here to delay?
pos.Subscribe(LastPos =>
{
// Do something with LastPos
}
);
LocationServices.Start();
我在想这样的东西行吗?
var pos = (from s in status
from p in Observable.FromEvent<GeoPositionChangedEventArgs<GeoCoordinate>>(LocationServices, "PositionChanged")
select p.EventArgs.Position).Delay( var pos = (from s in status
from p in Observable.FromEvent<GeoPositionChangedEventArgs<GeoCoordinate>>(LocationServices, "PositionChanged")
select p.EventArgs.Position).TakeLast(1).Delay(new TimeSpan(0,0,5));
pos.Subscribe(Lastpos =>
{
// Do something with Lastpos
}
);;
编辑:不,它不工作
RX中有一些不同的速率限制运算符。Sample
最接近您所描述的内容,但如果源没有生成新通知(换句话说:Sample="不超过"),它不会每5秒生成一次通知。
您应该能够通过组合其他一些运算符来获得轮询效果。首先,我们需要Interval
来获取刻度,CombineLatest
来对刻度进行采样。但是,CombineLatest将同时输出来自原始源的记号和通知的结果。为了处理这个问题,我们可以使用Scan
、Where
和Select
的组合。最后你应该有这样的东西:
IObservable<T> Poll<T>(this IObservable<T> source, TimeSpan interval)
{
//error checking goes here
return source.CombineLatest(Observable.Interval(interval),
Tuple.Create)
.Scan(Tuple.Create(string.Empty, -1L, -1L),
(a, t) => Tuple.Create(t.Item1, t.Item2, a.Item2))
.Where(t => t.Item2 != t.Item3)
.Select(t => t.Item1);
}
关于你发布的代码的一些注意事项:
var pos = (from s in status
from p in Observable.FromEvent<GeoPositionChangedEventArgs<GeoCoordinate>>(LocationServices, "PositionChanged")
select p.EventArgs.Position);
这使得每当状态事件被触发为就绪时,都会对PositionChanged
事件进行新的订阅。这最终会导致职位变动被多次报告,这可能不是你想要的。你可能想要更像的东西
var status = Observable.FromEvent<...>(LocationServices, "StatusChanged");
var readys = status.Where(o => o.EventArgs.Status == GeoPositionStatus.Ready);
var notReadys = status.Where(o => o.EventArgs.Status != GeoPositionStatus.Ready);
var positions = Observable.FromEvent<...>)(LocationServices, "PositionChanged");
var readyPositions = from r in readys
from p in positions.TakeUntil(notReadys)
select p;
//now you can use the Poll operator
readyPositions = readyPositions.Poll(TimeSpan.FromSeconds(5));
编辑经过进一步审查,如果您只想每隔一段时间对职位进行一次民意调查,则无需处理任何一个事件。您可以简单地检查计时器上的属性。
var readyPositions = from tick in Observable.Interval(TimeSpan.FromSeconds(5))
where LocationServices.Status == GeoPositionStatus.Ready
select LocationServices.Position;
如果您只希望计时器在观察者"就绪"时运行,则可以使用状态事件,但仍然不需要使用位置事件。
//using variable definitions from above (readys, notReadys)
var readyPositions = from r in readys
from i in Observable.Interval(TimeSpan.FromSeconds(5))
.TakeUnitl(notReadys)
select LocationServices.Position;