Page view counter
Calling Javascript method from Silverlight, passing extra parameters
Last post 12-29-2007 5:36 AM by mchlsync. 6 replies.
Sort Posts:
12-25-2007 11:56 AM
Calling Javascript method from Silverlight, passing extra parameters

Hi,

   I have seen several samples about how to call a javascript function from silverlight, that's nice, but... all samples does not pass any extra param to the javascript method (using the other way around, call from javascript to ajax you can add for instance extra string params). Anyone has a sample about how to do this, or add extra params to the EventArgs?

  Thanks

     Braulio

// ---------------------------------
    Braulio Diez

    http://www.tipsdotnet.com
// ---------------------------------

Brauliod

Loading...
Joined on 06-04-2007
Malaga (Spain)
Posts 230
12-27-2007 12:45 PM
Re: Calling Javascript method from Silverlight, passing extra parameters

brauliod:
add extra params to the EventArgs
 

What do you meant by "extra parameters" ? You mean, you want to pass the unlimited numbers of parameters to Javascript function?

(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-27-2007 4:54 PM
Re: Calling Javascript method from Silverlight, passing extra parameters

Just add my custom fixed parameters, e.g. several strings (I guess I can pass just some basic types, string, number...)

// ---------------------------------
    Braulio Diez

    http://www.tipsdotnet.com
// ---------------------------------

Brauliod

Loading...
Joined on 06-04-2007
Malaga (Spain)
Posts 230
12-27-2007 9:53 PM
Re: Calling Javascript method from Silverlight, passing extra parameters

I think it doesn't support or still has some bugs for that.

I reported it in this link.  you can track it.


(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 7:21 AM
Re: Re: Calling Javascript method from Silverlight, passing extra parameters

Thanks for the info, in the mean time I will use HTML hidden fields as a workaround.

 

// ---------------------------------
    Braulio Diez

    http://www.tipsdotnet.com
// ---------------------------------

Brauliod

Loading...
Joined on 06-04-2007
Malaga (Spain)
Posts 230
12-29-2007 5:16 AM
Re: Re: Calling Javascript method from Silverlight, passing extra parameters

http://silverlight.net/forums/t/7729.aspx

 

You need construct your own EventArgs in  CallbackToBrowser(this, new EventArgs());

 

How do it you find in msdn it's simple. 

robert.pieprzny

Loading...
Joined on 12-18-2007
Poland
Posts 37
12-29-2007 5:36 AM
Marked as Answer
Re: Re: Calling Javascript method from Silverlight, passing extra parameters

 I forget to put [Scriptable] in my EventArgs class. You can try the code below. I hope it works.

The new EventArgs class with parameters

[Scriptable]
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){                                             
        alert(args.Data);
    }
  
}

 

(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
Microsoft Communities