Skip to main content
Home Forums Silverlight Programming Programming with JavaScript Can't get Silverlight event to call Javascript in VB
8 replies. Latest Post by grassBlade on June 7, 2009.
(0)
cenesdel...
Member
0 points
5 Posts
06-03-2009 8:25 AM |
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;
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
alert(“got here”);
}
The problem is that the line
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
All-Star
16276 points
2,479 Posts
06-03-2009 1:30 PM |
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
06-03-2009 3:25 PM |
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:
grassBlade
8 points
4 Posts
06-05-2009 10:04 AM |
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" "]"
06-05-2009 10:23 AM |
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.Content.Page.ZoomIn();
Calls the vb method ZoomIn inside my Page.xaml.vb:
MainMap.ZoomLevel = MainMap.ZoomLevel + 1
06-05-2009 3:01 PM |
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
06-06-2009 10:12 AM |
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?
06-07-2009 11:39 AM |
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
06-07-2009 1:33 PM |
yes, me I saw the msdn page the other day, but forgot to mention it (my brain is slowly but surely being concretized.)