3

At the moment, I'm just using the plain old delegates/events observer pattern for click events on my buttons. So, I thought I would go Rx.

My first attempt was balls. I thought I'd go for the simplest overload that you see in WinForms examples:

Observable.FromEventPattern<EventArgs>(__navToTestButton, 'click');

But I got an exception that Android buttons don't have a click event. That is a reflection fail of epic proportions. I'm guessing perhaps I'm missing a dll (great, another dll to make my apk even bigger).

Anyhow, I thought I'd forge on with a different overload. Now it looks like this:

navToTest = Observable.FromEventPattern<EventArgs>(
    x => _navToTestButton.Click += _navToTestButton_Click,
    x => _navToTestButton.Click -= _navToTestButton_Click
).ObserveOn(SynchronizationContext.Current);

Still a fail. No exception. But when I click the button, just crickets and tumbleweeds. Am I meant to subscribe to that? What would that look like, as the subscription seems to have already happened in that overload?

Xamarin is tricky enough, but Rx just makes it bloody hard. Especially with the lack of abundance of people blogging about it (at least my Google searches turned up nothing too useful. I based my code on the few bits and pieces I did find.

Thanks

onefootswill
  • 3,707
  • 6
  • 47
  • 101

1 Answers1

3

I do not see any subscriptions on your observable(s):

Reflection:

Observable.FromEventPattern(aButton, "Click")
  .Subscribe((obj) => Log.Debug(TAG, $"StackOverflow: {obj}"));

Handler:

Observable.FromEventPattern(
    handler => aButton.Click += handler,
    handler => aButton.Click -= handler
)
.Subscribe(
  (obj) => Log.Debug(TAG, $"StackOverflow: {obj}")
);

Make a double tap observable:

var fromInterval = TimeSpan.FromMilliseconds(250);
var toInterval = TimeSpan.FromMilliseconds(500);

var buttonTapped = Observable.FromEventPattern(
    handler => aButton.Click += handler,
    handler => aButton.Click -= handler
);
var doubleTaps = buttonTapped
        .TimeInterval()
        .SkipWhile(t => t.Interval < toInterval)
        .FirstAsync(t => t.Interval < fromInterval)
        .Repeat();          
doubleTaps
        .Subscribe(
          (obj) => Log.Debug(TAG, $"Double Tap: {obj}")
     );
SushiHangover
  • 73,120
  • 10
  • 106
  • 165
  • Thanks. That worked. As with all things Rx, I don't really understand the API. It's very much "monkey see, money do". – onefootswill Dec 09 '17 at 03:54