Skip to main content

Microsoft Silverlight

Answered Question Calling Javascript method from Silverlight, passing extra parametersRSS Feed

(0)

brauliod
brauliod

Participant

Participant

1169 points

472 Posts

Silverlight MVP

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.dbschemaeditor.com
    Free Silverlight Based Database Schema Editor
/// ---------------------------------

mchlsync
mchlsync

Star

Star

14606 points

2,730 Posts

Silverlight MVP

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

Regards,
Michael Sync
Silverlight MVP

Blog : http://michaelsync.net


brauliod
brauliod

Participant

Participant

1169 points

472 Posts

Silverlight MVP

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.dbschemaeditor.com
    Free Silverlight Based Database Schema Editor
/// ---------------------------------

mchlsync
mchlsync

Star

Star

14606 points

2,730 Posts

Silverlight MVP

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

Regards,
Michael Sync
Silverlight MVP

Blog : http://michaelsync.net


Brauliod
Brauliod

Participant

Participant

1169 points

472 Posts

Silverlight MVP

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.dbschemaeditor.com
    Free Silverlight Based Database Schema Editor
/// ---------------------------------

robert.pieprzny
robert.p...

Member

Member

116 points

37 Posts

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. 

mchlsync
mchlsync

Star

Star

14606 points

2,730 Posts

Silverlight MVP
Answered Question

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

Regards,
Michael Sync
Silverlight MVP

Blog : http://michaelsync.net


  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities