The issue while calling the Javascript from managed code
Last post 12-29-2007 5:38 AM by mchlsync. 2 replies.
Sort Posts:
12-27-2007 9:52 PM
The issue while calling the Javascript from managed code

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

Blog : http://michaelsync.net
Feed : http://michaelsync.net/feed

mchlsync

Loading...
Joined on 09-16-2005
Singapore
Posts 2,335
12-28-2007 6:29 PM
Marked as Answer
Re: The issue while calling the Javascript from managed code

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

(If this has answered your question, "Mark as Answer")

Shawn Wildermuth
C# MVP, MCSD, Speaker and Author

Silverlight 2 Workshop
October 22-24, 2008 - Atlanta, GA
November 3-5, 2008 - San Diego, CA
http://silverlight-tour.com

swildermuth

Loading...
Joined on 10-13-2003
Atlanta, GA
Posts 1,229
12-29-2007 5:38 AM
Re: Re: The issue while calling the Javascript from managed code

thanks. 

(If this has answered your question, please click on "Mark as Answer" on this post. Thank you!)

Best Regards,
Michael Sync

Blog : http://michaelsync.net
Feed : http://michaelsync.net/feed

mchlsync

Loading...
Joined on 09-16-2005
Singapore
Posts 2,335
Page view counter