0

我正在尝试使用一对主题类来触发 2 组事件序列。该应用程序是一个绘图应用程序,其中一个主体在用户单击时触发 onNext,而另一个主体在用户双击时触发 OnNext。我已经编写了 GetClick 和 GetDoubleClick 方法,它们为上述情况返回一个 observable 并且似乎工作正常。下面代码中的问题是,如果在第一个触发点击序列的主题上调用 onNext,双击 observable 永远不会被调用。如果我注释掉第一个主题的 onNext 调用,双击 observable 确实会按预期触发。任何人都可以看看下面的代码和他们的想法/想法吗?我在下面的问题代码行中添加了注释

public static KeyValuePair<IObservable<MapPoint>, IObservable<PointCollection>> 
    DrawPointsDynamic(this Map map)
{
    PointCollection pc = new PointCollection();
    Subject<Point> ptSubject = new Subject<Point>();
    Subject<PointCollection> ptsSubject = new Subject<PointCollection>();

    IObservable<Point> ptObs = ptSubject.Hide();
    IObservable<PointCollection> ptsObs = ptsSubject.Hide();

    map.GetClick()
        .Subscribe(next =>
            {
                var p = map.ScreenToMap(next.EventArgs.GetPosition(map));
                ptSubject.OnNext(p); //If I leave this line in, the subscription to the doubleClick below does not get called. If comment it out, the subscription below does get called as expected;
                pc.Add(p);

            });

    map.GetDoubleClick()
        .Subscribe(next =>
        {
            ptsSubject.OnNext(pc);
            pc = new ESRI.ArcGIS.Client.Geometry.PointCollection();
        });

    KeyValuePair<IObservable<MapPoint>, IObservable<ESRI.ArcGIS.Client.Geometry.PointCollection>> obs =
        new KeyValuePair<IObservable<MapPoint>, IObservable<ESRI.ArcGIS.Client.Geometry.PointCollection>>
            (ptObs, ptsObs);

    return obs;
}

另外,我不太确定 Hide() 是做什么的。我只是在使用它,因为所有示例似乎都有 em。隐藏身份的真正含义是什么?

4

1 回答 1

1

我建议不要尝试解决您现在遇到的问题,而是对其进行 rx-ify 处理。第一次迭代非常简单:

public static KeyValuePair<IObservable<MapPoint>, IObservable<PointCollection>> 
    DrawPointsDynamic(this Map map) 
{
    var pc = new PointCollection();
    var ptSubject = map.GetClick().Select(next => map.ScreenToMap(next.EventArgs.GetPosition(map)).Publish();
    var ptsSubject = map.GetDoubleClick().Publish();

    ptSubject.Subscribe(pc.Add);
    ptsSubject.Subscribe(_ =>  pc = new PointCollection());

    ptSubject.Connect();
    ptsSubject.Connect();

    return new KeyValuePair<IObservable<MapPoint>, IObservable<PointCollection>>(ptObs, ptsObs);
}

现在,通过查看此内容,我怀疑您真正想要的是:

public static IObservable<PointCollection> DrawPointsDynamic(this Map map)
{
    var pcs = map.GetDoubleClick().Select(_ => new PointCollection()).Publish();
    var ps = map.GetClick().Select(next => map.ScreenToMap(next.EventArgs.GetPosition(map)));
    var ppcs = pcs.SelectMany(pc => ps.Select(p => { pc.Add(p); return pc; }).TakeUntil(pcs));

    var obs = pcs.Merge(ppcs);

    pcs.Connect();

    return obs;
}

这将返回一个 observable,它会在单击或双击时产生 PointCollection,其中有或没有点。

于 2010-06-25T15:59:19.620 回答