Skip to main content

Microsoft Silverlight

Answered Question Can't get Silverlight event to call Javascript in VBRSS Feed

(0)

cenesdelavega
cenesdel...

Member

Member

0 points

5 Posts

Can't get Silverlight event to call Javascript in VB

This blog covers it pretty well in C#, but I can't get it to work in VB:

http://blogs.microsoft.co.il/blogs/alex_golesh/archive/2008/07/28/quick-silverlight-tip-silverlight-events-in-javascript-and-javascript-events-in-silverlight.aspx

This is what I have in my Page.xaml.vb:

<ScriptableMember()> Public Event ClickEvent()

Private Sub MainMap_MouseClick(ByVal sender As Object, ByVal e As Microsoft.VirtualEarth.MapControl.MapMouseEventArgs) Handles MainMap.MouseClick

RaiseEvent ClickEvent()

End Sub

and in my JS( called long after initialization has completed btw)

sc = document.getElementById('<%= Xaml1.ClientID %>');

if (sc == null) return;

sc.Content.Page.ClickEvent = GetWMSInfoSL;

where

function GetWMSInfoSL(sender,e) {

alert(“got here”);

}

The problem is that the line

sc.Content.Page.ClickEvent = GetWMSInfoSL;

doesn’t work, giving a message:

ystem.ArgumentException: Error binding to target method.
   at System.Delegate.CreateDelegate(Type type, Object firstArgument, MethodInfo method, Boolean throwOnBindFailure)
   at System.Delegate.CreateDelegate(Type type, Object firstArgument, MethodInfo method)
   at System.Windows.Hosting.ScriptingInterface.GetDelegateForScriptObject(Type eventHandlerType, ScriptObject obj)

 

Likely I'm doing something horribly wrong, but could only find C# examples.

 

Thanks!

Roger

 

ken tucker
ken tucker

All-Star

All-Star

15738 points

2,390 Posts

Re: Can't get Silverlight event to call Javascript in VB

 Events are suspose to have 2 arguments sender as object and e as eventargs.  I think you are getting the errors because it is not finding the 2 arguments

cenesdelavega
cenesdel...

Member

Member

0 points

5 Posts

Re: Can't get Silverlight event to call Javascript in VB

ken tucker:

 Events are suspose to have 2 arguments sender as object and e as eventargs.  I think you are getting the errors because it is not finding the 2 arguments

Well, the javascript already has two args, so I tried every VB combination I could think of, and nothing works. For example:

<ScriptableMember()> Public Event ClickEvent(ByVal t, ByVal a)

Private Sub MainMap_MouseClick(ByVal sender As Object, ByVal e As Microsoft.VirtualEarth.MapControl.MapMouseEventArgs) Handles MainMap.MouseClick

      RaiseEvent ClickEvent(Me, New EventArgs)

End Sub

 

grassBlade
grassBlade

Member

Member

8 points

4 Posts

Re: Re: Can't get Silverlight event to call Javascript in VB

Are you sure your 'sc' var has a valid value?

<script type='text/javascript'>

alert(sc.id)
for (i in sc) document.write (i + ' = ' + sc[ i ] + '</br>')
</script>

 that should be: "[" "i" "]"

Where the World Once Stood
the blades of grass
cut me still

cenesdelavega
cenesdel...

Member

Member

0 points

5 Posts

Re: Re: Can't get Silverlight event to call Javascript in VB

What I really need is someone to extend Alex's blog to VB.NET. I'm sure I'm doing something stupid, but I just can't see it. 

Thanks Grassblade, but I've verified this is ok because I've got a number of methods working fine that I can call in my Silverlight object using the same syntax for example:

 sc = document.getElementById('<%= Xaml1.ClientID %>');if (sc == null) return;

sc.Content.Page.ZoomIn();

 

Calls the vb method ZoomIn inside my Page.xaml.vb:

<ScriptableMember()> Public Sub ZoomIn()

MainMap.ZoomLevel = MainMap.ZoomLevel + 1

End Sub

 

 

grassBlade
grassBlade

Member

Member

8 points

4 Posts

Re: Re: Re: Can't get Silverlight event to call Javascript in VB

ok...first off, I'm really not a vb'er; that said:

afaik, ".Content.Page" is not a valid attribute in javascript.  Are you trying to access from a master page a click from a content page's control?  if so, the following link may help:

http://www.codeproject.com/KB/scripting/Masterpage-Javascript.aspx

 

Where the World Once Stood
the blades of grass
cut me still

cenesdelavega
cenesdel...

Member

Member

0 points

5 Posts

Re: Re: Re: Re: Can't get Silverlight event to call Javascript in VB

No, this isn't a master page, but a Silverlight control in a web page. Content.Page is how you access methods and properties inside a Silverlight control from Javascript. I'm able to do methods and properties, just cannot figure out how to handle an event that occurs in the Silverlight app in the web page's Javascript. Alex's blog shows how in C# but not in VB. Any VB experts out there listening?

cenesdelavega
cenesdel...

Member

Member

0 points

5 Posts

Answered Question

Re: Re: Re: Re: Re: Can't get Silverlight event to call Javascript in VB

Well, turns out there is an easier way, simply call a Javascript function from the VB silverlight OnClick (or whatever) code using HtmlPage.Window.Invoke("myJSMethod")

 Very easy, and can pass params too. See:

http://pietschsoft.com/post/2008/06/19/Silverlight-and-JavaScript-Interop-Basics.aspx

and

http://msdn.microsoft.com/en-us/library/system.windows.browser.scriptobject.invoke(VS.96).aspx

 

grassBlade
grassBlade

Member

Member

8 points

4 Posts

Re: Re: Re: Re: Re: Can't get Silverlight event to call Javascript in VB

yes, me Tongue Tied   I saw the msdn page the other day, but forgot to mention it (my brain is slowly but surely being concretized.)

Where the World Once Stood
the blades of grass
cut me still
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities