我有以下代码:

Observable.FromEvent<ModelEventArgs>(
        h => ValuesController.ModelAdded += h,
        h => ValuesController.ModelAdded -= h)
    .Subscribe(m => context.Connection.Broadcast(m));

Observable.FromEvent<ModelEventArgs>(
        h => ValuesController.ModelDeleted += h,
        h => ValuesController.ModelDeleted -= h)
    .Subscribe(m => context.Connection.Broadcast(m));

什么会更清洁:
Observable.[SOMETHING](
        Observable.FromEvent<ModelEventArgs>(
                h => ValuesController.ModelAdded += h,
                h => ValuesController.ModelAdded -= h),
        Observable.FromEvent<ModelEventArgs>(
                h => ValuesController.ModelDeleted += h,
                h => ValuesController.ModelDeleted -= h))
    .Subscribe(m => context.Connection.Broadcast(m));

我似乎无法弄清楚 [SOMETHING] 需要是什么,我不想等待 ModelAdded 完成,我只希望它们都为每个事件发出 (m)。

放轻松,我是 Rx 新手

最佳答案

我认为您正在寻找 Merge :

Observable.Merge(
        Observable.FromEvent<ModelEventArgs>(
                h => ValuesController.ModelAdded += h,
                h => ValuesController.ModelAdded -= h),
        Observable.FromEvent<ModelEventArgs>(
                h => ValuesController.ModelDeleted += h,
                h => ValuesController.ModelDeleted -= h))
    .Subscribe(m => context.Connection.Broadcast(m));

关于c# - 复合可观察对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13042633/

10-12 06:23