Parameter less delegate in Reactive Extensions in a Universal C# Windows App

I have an eventhanler:

 public delegate void OnSizeEventHandler();

And I have the class that triggers the event of that type

CustomObject.OnSize += OnResize;

As you can see it's just a parameterless delegate.

I want to use it from reactive extensions like:

    var eventAsObservable = Observable.FromEvent<OnSizeEventHandler>(           
       x => CustomObject.OnSize += x,
       x => CustomObject.OnSize -= x);

I get this error:

Error   CS0029  Cannot implicitly convert type 'System.Action<CustomObject.OnSizeEventHandler>' to 'CustomObject.OnSizeEventHandler'

What am I doing wrong? It works pretty fine with EventHandlers but when it comes to a custom event, it's so hard to figure out how to make it work.

Thanks.

Jon Skeet
people
quotationmark

If you look at the overloads of FromEvent, you'll see there aren't any that match your current call.

You might want to try using this instead:

var eventAsObservable = Observable.FromEvent<OnSizeEventHandler>(           
   (Action x) => CustomObject.OnSize += new OnSizeEventHandler(x),
   (Action x) => CustomObject.OnSize -= new OnSizeEventHandler(x));

Basically, that's attempting to take the Rx support for Action-based events, and wrap the action that's passed in an OnSizeEventHandler each time.

I wouldn't like to guarantee that it'll work, but it's worth a try.

people

See more on this question at Stackoverflow