Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

The issue while calling the Javascript from managed code RSS

2 replies

Last post Dec 29, 2007 09:38 AM by mchlsync

(0)
  • mchlSync

    mchlSync

    Star

    14968 Points

    2799 Posts

    The issue while calling the Javascript from managed code

    Dec 28, 2007 01:52 AM | LINK

    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

    8438 Points

    1547 Posts

    Re: The issue while calling the Javascript from managed code

    Dec 28, 2007 10:29 PM | LINK

    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

    Shawn Wildermuth
    MVP, Speaker and Author

    Web Workshop (HTML5/CSS/MVC4)
    San Fran, CA - Mar 28-30, 2012
    Dallas, TX: Apr 29-May 1, 2012
    https://agilitrain.com/Workshop/Info/Web_Workshop
  • mchlSync

    mchlSync

    Star

    14968 Points

    2799 Posts

    Re: Re: The issue while calling the Javascript from managed code

    Dec 29, 2007 09:38 AM | LINK

    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