skjagini
skjagini

Reputation: 3217

Creating Observable stream out of Event Pattern of Reactive Extensions

I am trying to create Observable stream using BoundItemUpdtedHandler which is used BoundItemUpdated event.

There are two subclasses of Base which are setting the datasource for the grid and in the Base class I am trying to create a stream if the BoundItemUpdate is raised.

public delegate void BoundItemUpdatedHandler<T>(T boundItem, IEnumerable<string> properties) where T : IBoundItem;

public class BindingList<T> : BindingList<T> where T : IBoundItem
{
 ..
 public event BoundItemUpdatedHandler<T> BoundItemUpdated;
}

public class Positions: Base
{
var datasource = new BindingList<PositionDTO>();
_grid.Datasource = datasource;
}

public class Orders: Base
{
var datasource = new BindingList<OrderDTO>();
_grid.DataSource = datasource
}

public class Base
{
  public IObservable<Stream> GetStream
  {
   //  How do I create stream using _grid? and event pattern?
  }
}

Upvotes: 0

Views: 1222

Answers (1)

J. Lennon
J. Lennon

Reputation: 3361

I did not understand exactly what you need, which will be the source of this stream, from where it will use, how will you raise it?

To manipulate events, the ideal is to follow the EventHandler standard:
http://msdn.microsoft.com/en-us/library/system.eventhandler.aspx (It is more easy to use with Rx, use other types of delegates with Rx is a bit more complicated to create the subscription)

But if you need subscribe a event with BoundItemUpdatedHandler<T> type, you can do it below (this is just an example)...

    [TestMethod]
    public void CustomEventWithRx()
    {
        var sx =
         Observable.FromEvent(
             new Func<Action<Tuple<IBoundItem, IEnumerable<string>>>, BoundItemUpdatedHandler<IBoundItem>>(
                 source => new BoundItemUpdatedHandler<IBoundItem>((s, e) => source(Tuple.Create(s, e)))),
             add => this.CustomHandler += add,
             rem => this.CustomHandler -= rem);

        sx.Select((item,index) => new { item,index}).Subscribe(next => Trace.WriteLine(next.index));

        OnCustomHandler(null, null);
    }

    public event BoundItemUpdatedHandler<IBoundItem> CustomHandler;

    protected virtual void OnCustomHandler(IBoundItem bounditem, IEnumerable<string> properties)
    {
        BoundItemUpdatedHandler<IBoundItem> handler = CustomHandler;
        if (handler != null) handler(bounditem, properties);
    }

    public delegate void BoundItemUpdatedHandler<T>(T boundItem, IEnumerable<string> properties) where T : IBoundItem;

Upvotes: 1

Related Questions