Wednesday 22 February 2012

Silverlight HtmlPage Class

HtmlPage.BrowserInformation gives general information about the browser, such as name, version, and operating system.

HtmlPage.Window gets the browser's window object.

HtmlPage.Document gets the browser's document object.

HtmlPage.PopupWindow opens a pop-up window.

Calling  javascript functions  from silverlight :-
private void btnAlertWindow_Click(object sender, RoutedEventArgs e)
{
    //First parameter is function name then parameters to send.
    HtmlPage.Window.Invoke("alertWindow", "hai");
}

In silverlight host page:-

function alertWindow(mes) {
//Alert the user.
      alert('alert from html page...' + mes);
}

Calling  silverlight  functions  from javascript:-
If we want to call Silverlight functions from javascript we have to create a class  with attributes like ScriptableType and ScriptableMember.

[ScriptableType]
public class User
 {
    [ScriptableMember]
    public string GetFullName(string firstName, string lastName)
    {
            return firstName + " " + lastName;
    }
}

Then register the user object with key name which is usefull to get the object in javascript.

HtmlPage.RegisterScriptableObject("objUser", new User());

In silverlight host page:-

function getFullName()
{
//Here we are getting the host div tag.
var silverlightControlHost = document.getElementById('divHost');

//Here we are getting the silverlight object tag and user object by     key     name and calling the function.
var result =   silverlightControlHost.all.namedItem('objHost').Content.objUser.GetFullN ame("David", "Jackson");

//Alert the user.
alert('Full name:' + result);
}


Let me know, if you have any feedback. Mail me for source code. Enjoy reading my articles…
sekhartechblog@gmail.com

No comments:

Post a Comment