English 中文(简体)
RX - 只有当主题符合标准时,才适用
原标题:RX - Run code only if subject meets criteria

I m using observables to handle keydown events. I d like to publish an observable, that will always set the key to handled iff the consumer has actually processed it. Is this possible?

这方面的一个例子是,我如何确定现在处理的关键:

KeyDown.Where(e => e.Key == Keys.W)
.Subscribe(e => { Console.WriteLine(e.Key); e.Handled = true; });

在此,我要举一个例子,说明我如何做(如果所处理的财产随后自动确定,我只能发表主旨,而不是关键人物:

HandledKeyDown.Where(k => k == Keys.W).Subscribe(k => Console.WriteLine(k));

Here s what I tried:

HandledKeyDown = Observable.Create<Key>(observer => {
    var keys = KeyDown.Subscribe(e => { e.Handled = true; observer.OnNext(e.Key); });
    return () => keys.Dispose();
});

该法典的问题是,无论消费者是否实际加工,钥匙总是按处理方式确定。

是否有办法知道该守则是否“符合”用户方法?

最佳回答

What you d like is to overload subscription on KeyEventArgs so that it makes an event as handled after the subscription. Sounds like an extension:

public static IDisposable SubscribeAndHandle(this IObservable<KeyEventArgs> input, Action<Keys> action)
{
    return input.Subscribe(e => { action(e.KeyCode); e.Handled = true; });
}

然后,你只使用<条码>(代谢/代码>,而不是<条码><>/代码>,该代码将规定<条码>Handled,在行动进行后方可使用,而行动可使用<条码>Keys而不是<条码>。

var sub = KeyDown
          .Where(e => e.KeyCode = Keys.W) // and so on
          .SubscribeAndHandle(k => Console.WriteLine(k));

请注意:您可使用<代码>。 Do , 相反,如果你不认为<代码>Handled在采取行动之前就已定论。 这是一种更清洁的副作用:

public static IDisposable SubscribeAndHandle(this IObservable<KeyEventArgs> input, Action<Keys> action)
{
    return input.Do(e => e.Handled = true)
                .Select(e => e.KeyCode)
                .Subscribe(e => action(e));
}

@Enigmatity的一份有益的评论认为,或许你不妨决定,在您的订阅中是否将真正确定。 这足够简单,只是修改了接收<代码>Func<Keys,bool>而不是的延伸方法。 Action<Keys>:

public static IDisposable SubscribeAndHandle(this IObservable<KeyEventArgs> input, Func<Keys,bool> action)
{
    return input.Subscribe(e => e.Handled = action(e.KeyCode));
}
问题回答

我期待着这样做:

var HandledKeyDown =
    KeyDown
        .Do(e => { /* Handling Code Here */ })
        .Where(e => e.Handled)
        .Where(e => e.Key == Keys.W)
        .Select(e => e.Key)
        .Subscribe(k => Console.WriteLine(k));

让我知道这是否有助于。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签