简体   繁体   English

如何使用Reactive绑定到条目的“焦点”事件?

[英]How can I bind to an Entry's “focused” event with Reactive?

I'm trying to bind to an text Entry fields "focused" event using reactive but my code is failing to compile. 我正在尝试使用反应式绑定到文本输入字段“焦点”事件,但是我的代码无法编译。

Here's what I'm doing now, which works fine: 这是我现在正在做的,很好用:

Entry _qty; // at class level
_qty.Focused += (s, e) => { /* do stuff */ };

Attempt 尝试

But I'd like to do something like this instead: 但是我想做这样的事情:

// class level

IObservable<string> _qtyFocusObservable;
Entry _qty;


// in a setup function

_qtyFocusObservable =
    Observable 
        .FromEventPattern<EventHandler<FocusEventArgs>>(
            x => _qty.Focused += x,
            x => _qty.Focused -= x
        );

Problem 问题

I've tried quite a few variations of the code above and I get compiler errors saying that the compiler can't implicitly convert from whatever type I specify to System.EventHandler<System.EventHandler<Xamarin.Forms.FocusEventArgs>> , even if the type I specify is indeed System.EventHandler<System.EventHandler<Xamarin.Forms.FocusEventArgs>> . 我已经尝试了上面代码的很多变体,但遇到编译器错误,即编译器无法将我指定的任何类型隐式转换为System.EventHandler<System.EventHandler<Xamarin.Forms.FocusEventArgs>> ,即使我指定的类型确实是System.EventHandler<System.EventHandler<Xamarin.Forms.FocusEventArgs>>

Question

How do I bind to my Entry 's Focused event using reactive? 如何使用反应式绑定到EntryFocused事件?

So to get a basic observable working from an event I usually structure like so: 因此,为了从事件中获得基本的可观察的工作,我通常采用如下结构:

var focusObservable = Observable.FromEventPattern<EventHandler, FocusEventArgs>( x => _qty.Focused += x.Invoke, x => _qty.Focused -= x.Invoke);

Then when I need to do something from that observable event I link a command to it like so: 然后,当我需要从该可观察事件中执行某些操作时,可以将命令链接到该事件,如下所示:

var doStuffCommand = ReactiveCommand.CreateAsyncTask(DoStuffAsync); focusObservable.InvokeCommand(doStuffCommand);

With a DoStuffAsync implementation of something like this: 通过DoStuffAsync实现如下所示:

public async Task DoStuffAsync(object value, CancellationToken token = default(CancellationToken)) { // Do stuff here }

I'm still fairly new to Reactive as well but this (should?) get you going in the right direction. 我对Reactive还是很陌生,但这(应该吗?)使您朝着正确的方向前进。

Cheers, and happy coding! 干杯,祝您编程愉快!

So, after a year of using ReactiveUI, this is how I fire an event when focusing an input. 因此,在使用ReactiveUI一年后,这就是我在集中输入时触发事件的方式。

var focusedObservable =
    Observable
        .FromEventPattern<FocusEventArgs>(
            x => _totalBirds.Focused += x,
            x => _totalBirds.Focused -= x)
        .Select(x => x.EventArgs.IsFocused);

    // fires when focused
    focusedObservable 
        .WhenIsTrue() // extension method, basically .Where(x => x == true)
        .ObserveOn(RxApp.MainThreadScheduler)
        .InvokeCommand(this, x => DoSomething)
        .DisposeWith(ControlBindings); // extension that uses composite disposable

    // fires when changing state back to unfocused
    focusedObservable
        .WhenIsFalse() // extension method, basically .Where(x => x == false)
        .ObserveOn(RxApp.MainThreadScheduler)
        .InvokeCommand(this, x => x.ViewModel.DoSomethingElse)
        .DisposeWith(ControlBindings); // extension that uses composite disposable

This is pretty straight forward, if you need to see any additional code, let me know. 这很简单,如果您需要查看其他代码,请告诉我。 Also, if you want to snag the .DisposeWith extension you can grab it here . 另外,如果您想获取.DisposeWith扩展名,则可以在此处获取它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM