Javascript operations in Silverlight are mainly usedSystem. Windows. Browser. HtmlPage. Window.
The following Javascript code is available on the HTML page:
<Script type = "text/javascript"> function GetBP (id) {// Obtain data through ajax based on the id, omitted. Var bp = {"BH": "0102007009", "Name": "Ⅱ-7 high slope", "X": 118.2039630, "Y": 30.0837440 }; return bp ;}</script>
In Silverlight. windows. browser. htmlPage. window. the Eval (string code) method can execute Javascript code and obtain its return value (object), and convert the return value to System. windows. browser. scriptObject, and then get the corresponding value through GetProperty:
Object obj = System. windows. browser. htmlPage. window. eval (string. format ("GetBP ('{0}');", id); string ret = string. format ("No.: {0} n name: {1} n longitude: {2} n latitude: {3}", (System. windows. browser. scriptObject) obj ). getProperty ("BH"), (System. windows. browser. scriptObject) obj ). getProperty ("Name"), (System. windows. browser. scriptObject) obj ). getProperty ("X"), (System. windows. browser. scriptObject) obj ). getProperty ("Y"); System. windows. browser. htmlPage. window. alert (ret );
The result is as follows:
If the Javascript code returns a simple object (string, number, etc.), you can directly use ToString () as follows:
Object obj = System. windows. browser. htmlPage. window. eval (string. format ("GetBP ('{0 }'). name; ", id); string Name = obj. toString (); System. windows. browser. htmlPage. window. alert ("Name:" + Name );
The silverlightObject placeholder is used in JavaScript syntax. You can call GetHost for most Silverlight objects to obtain references to the silverlightObject object from the JavaScript API. In addition, you can call Document. GetElementByID and specify the ID of the object element that instantiates the Silverlight plug-in to obtain the silverlightObject instance from the DOM.
The Silverlight. js syntax is based on Silverlight. js functions. As of the date of release of this document, these functions are the latest. The Silverlight. js library may have been updated by Microsoft. In this case, the syntax may change, including the parameter name and parameter order.
Silverlight calls javaScript
It mainly uses the HtmlPage. Window object.
Xaml. cs
Private void button#click (object sender, RoutedEventArgs e)
{
// Use Invoke
HtmlPage. Window. Invoke ("calljs", "Invoke ");
}
Private void button2_Click (object sender, RoutedEventArgs e)
{
// Create a script object
ScriptObject calljs =
(ScriptObject) HtmlPage. Window. GetProperty ("calljs ");
// Use InvokeSelf
Calljs. InvokeSelf ("InvokeSelf ");
}
Private void LayoutRoot_Loaded (object sender, RoutedEventArgs e)
{
// JavaScript script
String jsText = @ "function calljs (msg ){
Alert (msg );
}";
// Create a script snippet
HtmlElement element = HtmlPage. Document. CreateElement ("Script ");
Element. SetAttribute ("type", "text/javascript ");
Element. SetProperty ("text", jsText );
// Add the script to the Html page
HtmlPage. Document. Body. AppendChild (element );
}
Declare the call method for creating javaScript objects
Private void LayoutRoot_Loaded (object sender, RoutedEventArgs e)
{
// JavaScript script
String jsText = @"
JsObject = function (msg)
{
This. Msg = msg;
}
JsObject. prototype. Show = function ()
{
Alert (this. Msg );
}";
// Create a script object
HtmlElement element = HtmlPage. Document. CreateElement ("Script ");
Element. SetAttribute ("type", "text/javascript ");
Element. SetProperty ("text", jsText );
// Add JavaScript to the Html page
HtmlPage. Document. Body. AppendChild (element );
}
Private void button#click (object sender, RoutedEventArgs e)
{
// Use CreateInstance to obtain JavaScript objects
ScriptObject script = HtmlPage. Window. CreateInstance ("jsObject", textBox1.Text );
Script. Invoke ("Show ");
}
Use the Eval method of HtmlWindow
Directly write javascript text through
HtmlPage. Window. Eval (textBox1.Text); to run this command, such as textBox1.Text = "alert ('Welcome! ')"
JavaScript calls Silverlight
In xaml. cs, initialize and define accessible objects and methods.
Public writable cript5 ()
{
InitializeComponent ();
// Register the JavaScript access object
HtmlPage. RegisterScriptableObject ("Builder", this );
}
// Define CreateRect as a script member
[ScriptableMember]
Public void CreateRect (int width, int height)
{
// Create a rectangle object
Rectangle rect = new Rectangle ();
Rect. Width = width;
Rect. Height = height;
Rect. Fill = new SolidColorBrush (Colors. Blue );
LayoutRoot. Children. Clear ();
LayoutRoot. Children. Add (rect );
}
Then you can call it in the js part.
<Script type = "text/javascript">
Function createRectangle (){
// Retrieve the Silverlight object based on the object id
Var xamlobj = document. all ('xamlobobject ');
// Call the CreateRect method in Silverlight
Xamlobj. content. Builder. CreateRect (
Document. all ('txtwidth'). value
, Document. all ('txtheight '). value );
}
</Script>