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.xaml
function 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.
(If this has answered your question, please click on "Mark as Answer" on this post. Thank you!)
Best Regards,
Michael Sync
Microsoft WPF & Silverlight Insider
Blog : http://michaelsync.net