在 Silverlight 中限制来自控件的事件

发布于 2024-12-29 21:39:54 字数 347 浏览 6 评论 0原文

我有一个使用 Bing 地图控件的 Silverlight 应用程序。当地图视图停止变化时就会加载数据。我看到一个例子有人使用 ASP.Net 版本的控件并能够实现这一目标。在 Silverlight 中也可以实现同样的功能吗?

Microsoft.Maps.Events.addThrottledHandler(map, 'viewchangeend', UpdatePOIData, 250);

I have a Silverlight app that utilizes the Bing Maps control. Data loads when ever the maps view stops changing. I saw an example where someone used the ASP.Net version of the control and was able to acheive this. Is this same thing possible in Silverlight?

Microsoft.Maps.Events.addThrottledHandler(map, 'viewchangeend', UpdatePOIData, 250);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

人心善变 2025-01-05 21:39:54

rx(除非我落后)尚未内置到 silverlight 中,并且让客户端下载所有 rx dll 只是为了限制似乎有点过分,除非您打算广泛使用它。

最简单的是,使用调度计时器创建您自己的节流类,该调度计时器将初始调用等待 x 秒,然后在执行操作之前检查是否有另一个调用进入。

抱歉,我手头没有任何代码

rx (unless Im behind) is not yet built into silverlight and seems a little overkill to have the client download all the rx dll just for throttling unless you are going to use it extensively.

At its simplest create your own throttling class using a dispatchtimer which takes the initial call waits x seconds and then checks if another call has come in since before executing your action.

Sorry I dont have any code to hand

蝶…霜飞 2025-01-05 21:39:54

您可以使用反应式扩展来做到这一点。 Throttle 方法就是为此目的而存在的:(

var observable =
    Observable.FromEventPattern<MapEventArgs>(
        handler => map.ViewChangeEnd += handler,
        handler => map.ViewChangeEnd -= handler);

observable.Throttle(TimeSpan.FromSeconds(1))
          .Subscribe(ev => map_ViewChangeEnd(ev.Sender, ev.EventArgs));


...

void map_ViewChangeEnd(object sender, MapEventArgs e)
{
    ...
}

未经测试)

You could do it with Reactive Extensions. The Throttle method exists for this purpose:

var observable =
    Observable.FromEventPattern<MapEventArgs>(
        handler => map.ViewChangeEnd += handler,
        handler => map.ViewChangeEnd -= handler);

observable.Throttle(TimeSpan.FromSeconds(1))
          .Subscribe(ev => map_ViewChangeEnd(ev.Sender, ev.EventArgs));


...

void map_ViewChangeEnd(object sender, MapEventArgs e)
{
    ...
}

(untested)

心奴独伤 2025-01-05 21:39:54

要解决使用 Subscribe 函数时无效的跨线程访问 ( UnauthorizedAccessExcecption) 错误,您将使用此代码。

使用以下内容:

using System.Reactive.Concurrency;
using System.Reactive.Linq;

var observable = Observable.FromEventPattern<MapEventArgs>(
                    handler => MyMap.ViewChangeEnd += handler,
                    handler => MyMap.ViewChangeEnd -= handler);
observable.Throttle(TimeSpan.FromSeconds(2)).ObserveOn(DispatcherScheduler.Current).Subscribe(ev => MyMap_ViewChangeEnd(ev.Sender, ev.EventArgs)); 

您必须添加 ObserveOn(DispatcherScheduler.Current) 才能使其正常工作。并添加对 System.Reactive.Core、System.Reactive.Interfaces、System.Reactive.Linq 和 System.Reactive 的引用。 Windows.Threading。

To get around the Invalid cross-thread access ( UnauthorizedAccessExcecption) while using Subscribe function error you'll get using this code.

Use the following:

using System.Reactive.Concurrency;
using System.Reactive.Linq;

var observable = Observable.FromEventPattern<MapEventArgs>(
                    handler => MyMap.ViewChangeEnd += handler,
                    handler => MyMap.ViewChangeEnd -= handler);
observable.Throttle(TimeSpan.FromSeconds(2)).ObserveOn(DispatcherScheduler.Current).Subscribe(ev => MyMap_ViewChangeEnd(ev.Sender, ev.EventArgs)); 

You have to add the ObserveOn(DispatcherScheduler.Current) to make it work. And add references for System.Reactive.Core, System.Reactive.Interfaces, System.Reactive.Linq and System.Reactive.Windows.Threading.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文