WIN10 series: C # App Control Basics 18

Source: Internet
Author: User

WebView controls

Using the WebView control, you can add a simple Web browser window to the application, display the content of the Web page of the specified address, and use the methods, properties, and events provided by the WebView control to implement such actions as page navigation, HTML text parsing, and so on.

In a XAML file, the use of the WebView control is as follows:

<webview .../>

Here are some common properties of the WebView control:

    • The Name property, Gets or sets the WebView control.
    • The Source property, gets or sets the URI address that is opened in the WebView control.

After describing the common properties, take a look at the common methods of WebView controls:

    • The Navigatetostring method that displays the contents of the specified HTML string.
    • The Navigate method that displays the Web page content for the specified URI address.

Here's a simple example of how to use the WebView control to parse a piece of HTML text and display it .

(1) HTML text parsing

Create a blank application project with the Windows store named "Showhtmldemo" and add a WebView control using the following code in the grid element of the MainPage.xaml file.

<webview name= "showhtml" horizontalalignment= "left" height= "" "Width=" $ "margin=" 348,134,0,0 " verticalalignment= "Top"/>

The code adds a The WebView control and sets its Name property to the value of the Showhtml,height property and the Width property to the value of the 100,horizontalalignment property to the value of the Left,verticalalignment property as top.

opens the MainPage.xaml.cs file in Onnavigatedto method , define an HTML string, and then use this string as a parameter to invoke the Navigatetostring method of the WebView control instance showhtml, as shown in the following code :

protected override void Onnavigatedto (NavigationEventArgs e)

{

string htmlstring = " Control text Resolution Use WebView Control Resolution HTML text </div></body>

Showhtml.navigatetostring (htmlstring);

}

The above code defines a string variable, htmlstring, for storing a piece of HTML text, and then invoking the Navigatetostring method of showhtml with the variable htmlstring as a parameter. Parses and displays HTML text in the WebView control of the interface.

Run the program, which will display the parsed Web page content in the WebView control of the interface, as shown in effect 4-29.

In addition to providing HTML text that needs to be parsed and displayed for the WebView control, you can also directly provide a network URI address for the WebView control, gain access to the HTML text information that needs to be displayed by accessing the network URI address, and then demonstrate the process with a simple example.

(2) Web browsing

Create a blank application project named "Showwebdemo" in the Windows store and add a WebView control to the MainPage.xaml file, as shown in the following code:

<webview name= "Showweb" horizontalalignment= "left" height= "+" width= "" "Margin=" 348,134,0,0 " verticalalignment= "Top"/>

the code above sets the The value of the WebView control's Name property value for the Showweb,height property is the value of the 120,width property for the 320,horizontalalignment property, and the value of the Left,verticalalignment property is top.

in the MainPage.xaml.cs file, In the Onnavigatedto method , define a URI address and use this URI address as a parameter, call the Navigate method provided by the WebView control to display the Address page , as shown in the following code:

protected override void Onnavigatedto (NavigationEventArgs e)

{

Uri TargetUri = new Uri ("http://www.microsoft.com");

Showweb.navigate (TargetUri);

}

The above code defines an object of the URI class TargetUri and instantiates the object as http://www.microsoft.com, and then takes the TargetUri object as a parameter to the Showweb.navigate method, displaying the address contents in the WebView control.

Run the program, the main page of the Microsoft Official website will be displayed in the WebView control on the interface, as shown in 4-30

Figure 4-29 Displaying HTML strings using the WebView control figure 4-30 Displaying Web page content using the WebView control

WIN10 series: C # App Control Basics 18

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.