Skip to main content
Home Forums Silverlight Programming Report a Silverlight Bug The issue while calling the Javascript from managed code
2 replies. Latest Post by mchlsync on December 29, 2007.
(0)
mchlsync
Star
14606 points
2,730 Posts
12-27-2007 9:52 PM |
I'm trying to answer this post and I got one issue that I think it is a bug.
What I'm trying to do is that trying to pass one value to Javascript function from Managed Code. As there is no parameter in EventArgs class, I created new class with parameter.It is working fine. then, The problem is that the property that I created "T Data" is not shown in Javascript. another thing that I notice is that sender is always null even we are sending the object.
The new EventArgs class with parameters
public class EventArgs<T> : EventArgs{ private T _data; public EventArgs(T args) { _data = args; } public T Data { get { return _data; } } }
Page.xaml.cs
[Scriptable] public partial class Page : Canvas {
public Page() { WebApplication.Current.RegisterScriptableObject("EntryPoint", this); MouseLeftButtonDown += Page_MouseLeftButtonDown; } void Page_MouseLeftButtonDown(object sender, MouseEventArgs e) { if (CallbackToBrowser != null) { CallbackToBrowser(this, new EventArgs<string>("my value")); } }
[Scriptable] public event EventHandler CallbackToBrowser;
}
TestPage.html.js
// JScript source code//contains calls to silverlight.js, example below loads Page.xamlfunction createSilverlight(){ Silverlight.createObjectEx({ source: "Page.xaml", parentElement: document.getElementById("SilverlightControlHost"), id: "SilverlightControl", properties: { width: "100%", height: "100%", version: "1.1", enableHtmlAccess: "true" }, events: { onLoad: OnLoaded } }); // Give the keyboard focus to the Silverlight control by default document.body.onload = function() { var silverlightControl = document.getElementById('SilverlightControl'); if (silverlightControl) silverlightControl.focus(); }}function OnLoaded(sender, args){ sender.Content.EntryPoint.CallbackToBrowser = onManagedCallback;}function onManagedCallback(sender, args){ if(args.Data){ //ERROR is here. args is nothing. sender is null. alert(args.Data); } }
Is that a bug or Did I miss something in my code? Thanks in advance.
swildermuth
8320 points
1,546 Posts
12-28-2007 6:29 PM |
I am not sure why the sender is always null, that might be a bug. But the bigger issue is the event argument. To fix this you must mark the EventArgs as scriptable:
[Scriptable] public class EventArgs<T> : EventArgs { private T _data;
public EventArgs(T args) { _data = args; }
[Scriptable] public T Data { get { return _data; } } }
HTH
12-29-2007 5:38 AM |
thanks.