使用随时间变化的任意布尔条件过滤 Touch.FrameReported IObservable

发布于 2024-10-14 14:23:18 字数 2488 浏览 4 评论 0 原文

我一直在尝试 Windows Phone 7 中的响应式扩展 (RX),并且非常接近可行的解决方案,但遇到了一个小细节。我正在尝试使用 Touch.FrameReportedObservable.FromEvent (有点教育探索,以更好地学习 Touch API 和 RX),但是我只想在特定条件下处理事件。例如,我可能希望仅当在枢轴控件中选择特定页面时过滤对触摸和触摸事件的订阅,但它可以是在 true 和 false 之间来回变化的任意条件。由于条件是一个随时间变化的值,所以感觉它应该是另一个与触摸事件流合并的可观察值,但我一生都无法弄清楚如何做到这一点。

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

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

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 方法可能会更好,但随后您可能必须处理“流的流”。我现在正在与 相关的内容进行斗争问题

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.

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尝蛊 2024-10-21 14:23:18

当谓词返回 false 时,TakeWhile 触发完成,此时所有内容都将被拆除。

您有两种选择:

最简单的解决方案是使用 Repeat 再次启动整个过程:

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

另一种解决方案是使用 SelectMany 来跟踪情况以触发每个过程的启动“一轮”监听(这与“拖放”WPF Rx 示例非常相似):

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

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

You have two choices:

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

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);
海夕 2024-10-21 14:23:18

除了 Richard Szalays 的答案之外,您还可以研究 Window 运算符(尽管我认为他的第二个解决方案可能是正确的)。您有希望接收触摸事件的时间窗口。

private void SetupStream()
{
    Subject<bool> testConditionValues = new Subject<bool>();

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

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

    touchEvents.Window(startTrigger, _ => endTrigger)
        .Subscribe(HandleNewWindow);

    // Open window
    testConditionValues.OnNext(true);

    // Close window
    testConditionValues.OnNext(false);

}

public void HandleNewWindow(IObservable<IEvent<EventArgs>> events)
{
    events.Subscribe(mousEvent => Trace.WriteLine(mousEvent));
}

In addition to Richard Szalays answer you could also look into the Window operator (although I think his second solution is probably the right one). You have windows of time that you'd like to receive touch events.

private void SetupStream()
{
    Subject<bool> testConditionValues = new Subject<bool>();

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

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

    touchEvents.Window(startTrigger, _ => endTrigger)
        .Subscribe(HandleNewWindow);

    // Open window
    testConditionValues.OnNext(true);

    // Close window
    testConditionValues.OnNext(false);

}

public void HandleNewWindow(IObservable<IEvent<EventArgs>> events)
{
    events.Subscribe(mousEvent => Trace.WriteLine(mousEvent));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文