使用随时间变化的任意布尔条件过滤Touch.FrameReported IObservable [英] Filtering a Touch.FrameReported IObservable using arbitrary boolean condition that changes over time

查看:69
本文介绍了使用随时间变化的任意布尔条件过滤Touch.FrameReported IObservable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用Windows Phone 7中的Reactive Extensions(RX),并且非常接近可行的解决方案,但是却陷入了一个小细节.我正在尝试使用

I've been playing around with the Reactive Extensions (RX) in Windows Phone 7 and am very close to a working solution but got caught up on one small detail. I am trying to process the raw touch events using Touch.FrameReported and Observable.FromEvent (a bit of an educational quest to learn the Touch API and RX better), but I only want to process the events under certain conditions. For example I may want to filter the subscription to the touch down and touch up events only when a specific page is selected in a pivot control, but it could be any arbitrary condition that changes back and forth between true and false. Since the condition is a value that changes over time it feels like it should be another observable that gets merged with the touch event stream, but I can't for the life of me figure out how to do that.

相反,我有一个半实用的解决方案,使用IObservable的TakeWhileSkipUntil扩展名.我有一个流接收整个应用程序的所有触摸事件(oTouchApp),第二个流仅在过滤条件为true时才接受项目(oTouchPage),然后还有两个流将触摸过滤为触摸向下(oTouchDown)和向上(oTouchDown)操作.所有这些流都是IObservable<IEvent<TouchFrameEventArgs>>类型,因此可以轻松地将它们合并和比较以创建自定义手势.问题是,一旦过滤条件从true变为false,我就无法重新启动oTouchPage流.我必须手动重新创建流,因为我希望它以某种方式在打开和关闭之间切换.

Instead I have a semi-working solution using the TakeWhile and SkipUntil extensions for IObservable. I have a stream that receives all the touch events for the whole app (oTouchApp), a second stream that only takes items while my filtering condition is true (oTouchPage), and then two other streams that filter the touches into touch down (oTouchDown) and touch up (oTouchDown) actions. All of these streams are of type IObservable<IEvent<TouchFrameEventArgs>>, so they can easily be merged and compared to create custom gestures. The problem is that I can't get the the oTouchPage stream to restart once the filter condition changes from true to false. I'd have to manually recreate the stream, where as I would prefer that it somehow toggles itself between on and off.

这是我到目前为止的代码.对于如何使用布尔值(如开/关开关)过滤流的任何帮助,将不胜感激.

Here is the code I have so far. Any help on how to filter a stream using a boolean value (like an on/off switch) would be greatly appreciated.

var oTouchApp = Observable.FromEvent<TouchFrameEventHandler, TouchFrameEventArgs>(x => new TouchFrameEventHandler(x), ev => Touch.FrameReported += ev, ev => Touch.FrameReported -= ev);
//This stops working after TestCondition goes from True to False
var oTouchPage = oTouchApp.SkipWhile((x) => TestCondition == False).TakeWhile((x) => TestCondition == True);
var oTouchDown = from t in oTouchPage
                 let primarypoint = t.EventArgs.GetPrimaryTouchPoint(this)
                 where primarypoint.Action == TouchAction.Up
                 select t;
var oTouchUp = from t in oTouchPage
               let primarypoint = t.EventArgs.GetPrimaryTouchPoint(this)
               where primarypoint.Action == TouchAction.Down
               select t;
//Code for testing
var sub1 = oTouchPage.Subscribe(x =>{Debug.WriteLine("TouchPage");});
var sub2 = oTouchDown.Subscribe(x =>{Debug.WriteLine("TouchDown");});
var sub3 = oTouchUp.Subscribe(x =>{Debug.WriteLine("TouchUp");});

更新: 原来我所需要的只是向oTouchPage流添加一个简单的where子句:var oTouchPage = oTouchApp.Where((x) => TestCondition == True); 这可能不是最好的解决方案,因为每次生成商品时都会对TestCondition进行评估,但是它运作良好且易于阅读.如果测试条件基于事件或易于转换为IObservable的其他条件,则我认为下面提到的Window或SelectMany方法可能更好,但是您可能必须处理流的流".我现在正在与相关的内容中进行斗争问题.

UPDATE: Turns out all I needed was to add a simple where clause to the oTouchPage stream: var oTouchPage = oTouchApp.Where((x) => TestCondition == True); It may not be the best solution, since the TestCondition is evaluated each time an item is produced, but it works well and is easy to read. If the test condition was based on events or some other condition that was easy to convert into an IObservable, then I think the Window or SelectMany approaches mentioned below might be better, but then you may have to deal with a "Stream of streams". I'm fighting that right now in a related question.

推荐答案

TakeWhile当谓词返回false时触发完成,这时所有内容都被破坏了.

TakeWhile triggers completion when the predicate returns false, at which point everything is torn down.

您有两种选择:

最简单的解决方案是使用Repeat重新开始整个过程​​:

The simplest solution is to use Repeat to start the whole process again:

var oTouchPage = oTouchApp
    .SkipWhile((x) => TestCondition == false)
    .TakeWhile((x) => TestCondition == true)
    .Repeat();

另一种解决方案是使用SelectMany跟踪情况以触发每个回合"聆听的开始(这与拖放" WPF Rx示例非常相似):

The other solution would be to track the situation using SelectMany to trigger the start of each "round" of listening (this is very similar to the "drag-drop" WPF Rx Example):

// Think of Subject like an observable variable
// This can just be an IObservable<bool> if the triggers actually come from elsewhere
Subject<bool> testConditionValues = new Subject<bool>();

var startTrigger = testConditionValues
    .DistinctUntilChanged()
    .Where(v => v == true);

var endTrigger = testConditionValues
    .Where(v => v == false);

var values = from start in startTrigger
             from touch in touchEvents.TakeUntil(endTrigger)
             select touch;

// Start listening
testConditionValues.OnNext(true);

// Stop listening
testConditionValues.OnNext(false);

这篇关于使用随时间变化的任意布尔条件过滤Touch.FrameReported IObservable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆