Skip to main content

Microsoft Silverlight

Answered Question The issue while calling the Javascript from managed codeRSS Feed

(0)

mchlsync
mchlsync

Star

Star

14606 points

2,730 Posts

Silverlight MVP

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!)

Regards,
Michael Sync
Silverlight MVP

Blog : http://michaelsync.net


swildermuth
swildermuth

Star

Star

8320 points

1,546 Posts

Answered Question

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 3 Workshop
Miami, FL: Oct 12-14th
Portlant, OR: Dec 2-4th
Atlanta, GA: Dec 7-9th
http://silverlight-tour.com

mchlsync
mchlsync

Star

Star

14606 points

2,730 Posts

Silverlight MVP

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!)

Regards,
Michael Sync
Silverlight MVP

Blog : http://michaelsync.net


  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities