Interaction with the Silverlight Browser: The Silverlight program calls the JavaScript method in the external HTML (with source code)

Source: Internet
Author: User

Overview

Webbrowser

From Silverlight 3, you can create a Silverlight application running outside the browser host environment.Program.

The webbrowser control allows you to display HTML content in this host environment.

You canCodeAdd the webbrowser control. By default, the webbrowser control does not have a size.

Therefore, you must specify the height and width to display the control in the application. When webbrowser is displayed in an application running in the browser, a rectangle with the specified height and width is displayed to replace the control.

You can set the HTML content to be displayed in the webbrowser control in several ways.

    • CallNavigatetostringMethod and pass a string containing the XAML content you want to display.

    • SetSourceSet the attribute to a fully qualified URI or relative Uri.

    • CallNavigateMethod and pass a fully qualified or relative URI to the HTML content you want to display.

 

Webbrowser. invokescriptMethod

Execute the script function defined in the currently loaded HTML.

Name
Description

Invokescript (string ):Execute the specified script defined in the currently loaded HTML.

Invokescript (string, array <string> []):Use the specified parameters to execute the specified script function defined in the currently loaded HTML.

Webbrowser. loadcompletedEvent

When the top-level navigation is complete and the content is loaded to the webbrowser control, or an error occurs during the loading.

Effect:

XAML code:

   <  Grid  X: Name  = "Layoutroot"  >  
< Grid. Background >
< Lineargradientbrush Startpoint = "0, 0" Endpoint = "0, 1" >
< Gradientstop Color = "# Ff0055dd" Offset = "0" />
< Gradientstop Color = "# Ff00ddff" Offset = "1" />
</ Lineargradientbrush >
</ Grid. Background >

<! -- HTML content can be displayed in this space.
Source is a page in clientbin of the web. -->
< Webbrowser X: Name = "Web" Source = "/Script-page.htm" Margin = "15" />
</ Grid >

CS code:

   Public  Mainpage ()
{
Initializecomponent ();
// When the top-level navigation is complete and the content is loaded to the webbrowser control, or an error occurs during the loading.
Web. loadcompleted + =
New Loadcompletedeventhandler (web_loadcompleted );
}


Void Web_loadcompleted ( Object Sender, navigationeventargs E)
{
// Execute the script function defined in the currently loaded HTML.
// The JavaScript method sayhello () without Parameters ()
Web. invokescript ( " Sayhello " );
// There is a parameter in the JavaScript method greetme (name)
Web. invokescript ( " Greetme " , " 'Silverlight' " );
}

HTML code:

 < Html  Xmlns  = "Http://www.w3.org/1999/xhtml"  >  
< Head >
< Title > </ Title >
< Script Language = "JavaScript" >
// Add content for div1
// A parameter method
Function Greetme (name ){
Div1.innerhtml + = " <P> hello! " + Name + " </P> " ;
}
// Method without Parameters
Function Sayhello (){
Div1.innerhtml + = " <P> hello! </P> " ;
}
</ Script >
</ Head >
< Body >
Hello world! < BR />
< Div ID = "Div1" >
</ Div >
</ Body >
</ Html >

Success!

Source code download

author: memories of lost youth
Source: http://www.cnblogs.com/lukun/
copyright of this Article It is shared by the author and the blog site. You are welcome to repost it, but this statement must be kept without the author's consent, and in the Article page obvious position to give the original connection, if there is a problem, you can contact me through the http://www.cnblogs.com/lukun/ , thank you very much.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.