WebBrowser - Rendering HTML in Silverlight

In previous versions of Silverlight if you needed to display some HTML in your application you would either need to use a control to convert the HTML to XAML such as our RichTextBlock control or you would render an actual HTML element over your Silverlight application.

Now in Silverlight 4 we have the new WebBrowser control.  This simple but powerful control allows you to display HTML, either by providing a URL or direct HTML content.

Some HTML in a Silverlight application

In this simple example we define a single WebBrowser control and in C# we add some static HTML:

<UserControl x:Class="WebBrowser.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">
        <Border Width="300" Height="200" BorderBrush="#000000" BorderThickness="2" CornerRadius="5">
            <WebBrowser x:Name="webBrowser" />
        </Border>
    </Grid>
</UserControl>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace WebBrowser
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            webBrowser.NavigateToString("<h1>Rendering HTML in Silverlight</h1><p>This is some <strong>simple HTML</strong>!</p>");
        }
    }
}


The NavigateToString method does the work here.  To display HTML in the WebBrowser control you pass a string of HTML to this method.  Alternatively you can call the Navigate method, this method simply takes a URL and displays it in your Silverlight application.

How to cretae the app in windoes based in asp.net or silcerlight,vb,net

ashish wrote:

This is not workig for me. webBrowser.Navigate(new Uri("http://www.vectorlight.net"));


I am using Visual Studio 2010 and Silverlight 4. The OOB is not loading anything for this ? Can anyone please help ?

Paul wrote:

FYI - WebBrowser requires the Silverlight application be run in out of browser mode


 ... otherwise, in-browser mode showing HTML gets more complicated: have to float the HTML over the Silverlight in windowless mode or use the HTML bridge.

Post your Comments

How to cretae the app in windoes based in asp.net or silcerlight,vb,net

ashish wrote:

This is not workig for me. webBrowser.Navigate(new Uri("http://www.vectorlight.net"));


I am using Visual Studio 2010 and Silverlight 4. The OOB is not loading anything for this ? Can anyone please help ?

Paul wrote:

FYI - WebBrowser requires the Silverlight application be run in out of browser mode


 ... otherwise, in-browser mode showing HTML gets more complicated: have to float the HTML over the Silverlight in windowless mode or use the HTML bridge.

Post your Comments

 
 
Latest Games
Zombie Escape
Apr 19, 2016
Plays: 2,513

Zombie Escape ScreenshotDrive fast before the crazy mutant zombies get you!

6 Ratings/4.1 Average
Car Parking
Jan 16, 2016
Plays: 2,380

Car Parking ScreenshotGuide the car to its parking space in this fun Car Parking game.

1 Rating/5 Average
Trash It
Jan 11, 2016
Plays: 2,285

Trash It ScreenshotAim for the Trash Can and get the various items of Trash in the bin.

4 Ratings/5 Average
Sky Fly
Jan 11, 2016
Plays: 2,435

Sky Fly ScreenshotFly your plane in this colorful vertical scrolling shoot-em-up. Blast the bad guys and collect any bonus's they leave behind.

1 Rating/5 Average
Professor Snappy
Jan 11, 2016
Plays: 1,969

Professor Snappy ScreenshotPop as many bubbles as possible in this fun and colorful bubble popping game. The levels start off easy enough but gradually get harder!

1 Rating/5 Average
Monster Match Saga
Jan 10, 2016
Plays: 2,294

Monster Match Saga ScreenshotHere we have a bunch of monsters that need to be matched up. Look out for the bomb and spinning monsters that will cause special damage!

3 Ratings/4.6 Average
Fly Bird Fly
Jan 10, 2016
Plays: 2,121

Fly Bird Fly ScreenshotGuide your friendly Bird through the maze of pipes and other obstacles collecting the Stars in this cool arcade game inspired by the legendary Flappy Bird.

1 Rating/5 Average
Life In One
Jan 10, 2016
Plays: 2,296

Life In One ScreenshotYou are stranded on an Alien planet. Your goal is to build a space rocket and escape. Start by building units to create power and mine the metal patches. Build defenses to defend your base from the advancing Aliens and Zombies!

2 Ratings/3 Average
X Pool
Jan 02, 2016
Plays: 2,913

X Pool ScreenshotPlay Pool against the computer or battle against your friends in the online mode!

3 Ratings/3 Average
Fruit Slicer
Jan 02, 2016
Plays: 2,009

Fruit Slicer ScreenshotSlice the fruit that is thrown up onto the screen. Slice the fruit into multiple pieces for maximum points!

1 Rating/5 Average