views:

339

answers:

0

I'm using Silverlight as the client for an application that uses the Google Maps API.

I use the HTML bridge to talk to the JS API and that works quite well. One of my issues was that I couldn't use closures.
In a full JS environment, I would register an event on the map thus:

google.maps.Event.addListener(marker, "click", function() { alert(someClosureVariable); });

and it would work. With the HTML bridge, no such luck. I can't pass in a function reference. I just realised today that I've been completely overlooking some very simple way to do it. It may be because it wasn't working in SL2 when I first tried it or that I hadn't thought of it.

If there's a method handler on the SL client

[scriptableMember]
public void OnMarkerClick(MyObject closureParam)
{
// do stuff
}

I can just call the event like that:

var googleEvent = (ScriptObject)HtmlPage.Window.Eval("google.maps.Event"); googleEvent.Invoke("addListener", marker, "click", new Action(() => OnMarkerClick(myClosureObject)));

I'm putting this here in case someone spends some time like me trying to get this to work.

If there's any better option, I would be quite happy to hear about it.