Web Browser Control in WPF

No.of Views1686
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  16 Feb 2010 00:02:56
Tag : WCF , How to
Web Browser Control in WPF
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 Objective:

In this article, I am going to show you; how to work with Web Browser control in WPF? We will be opening a web site (even a Silverlight Enabled web site in WPF application) in the new Web Browser control.

Expected Output

brswrctrlwpfimg01



Web Browser Control

This control is added in .Net 3.5 SP1. This is used to browse the websites in a WPF application. This control could be used to execute scripts also. This control is capable of loading Silverlight enabled site also. This feature makes it a great control.

Image Loading...



Working Explanation:

I do have two buttons on the form. One button will load the requested site from the user text box. And another is used to load the default web site for this window application. I have taken Google as default website. There is one text box also. In this text box user could enter the site she is willing to load in the browser.


Step 1: Create a WPF Application.

Open the Visual Studio 2008 and create a new WPF Application.

Image Loading....



Step 2: Design the window

Open the WPF application in blend and design the window.

Image Loading....



1. I have divided the default grid in three rows and two columns
2. In first row , I put oneTextBlock and one TextBox
3. In TextBox user will input site address she wants to load in the web browser.
4. In second row, I put one border layout. And merged the columns.
5. Then I put one WebBrowser control in the second row of Grid inside border Layout.
6. In third row, I put one StackPanel with horizontal orientation.
7. Then I put two buttons in the third row. One button is to load the default website and another to load the requested website.

Windows1.Xaml (For your reference)


{codecitation class="brush: xml; gutter: true;" width="700px"}


<Window x:Class="webbrowsercontrol.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="400" Width="400">
<Grid>
<Grid.Background>
<LinearGradientBrush MappingMode="RelativeToBoundingBox" EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF000000" Offset="0"/>
<GradientStop Color="#FF52698D" Offset="1"/>
</LinearGradientBrush>
</Grid.Background>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.226*"/>
<ColumnDefinition Width="0.774*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="0.091*"/>
<RowDefinition Height="0.755*"/>
<RowDefinition Height="0.154*"/>
</Grid.RowDefinitions>
<TextBlock Margin="1,1,1,1" Text="Navigate To" TextWrapping="Wrap" Foreground="#FFF4EAEA"/>
<TextBox x:Name="txtLoad" Margin="0,1,0,1" Grid.Column="1" Text="" TextWrapping="Wrap"
Background="#FFBDA4A4" HorizontalAlignment="Stretch" Cursor="No" Foreground="#FFE91D1D"
Width="Auto" Height="30" VerticalAlignment="Top"/>
<Border Margin="1,1,1,1" Grid.ColumnSpan="2" Grid.Row="1" BorderBrush="#FF000000" BorderThickness="1,1,1,1">
<WebBrowser x:Name="myBrowser" Margin="0,0,0,0" Cursor="Arrow"/>
</Border>
<Border Margin="1,0,1,0" Grid.ColumnSpan="2" Grid.Row="2" BorderBrush="#FF000000" BorderThickness="1,1,1,1">
<StackPanel Margin="0,0,0,0" Orientation="Horizontal">
<Button x:Name="btnExternal" Margin="0,0,0,0" Width="175" Content="Load
Requested Site" FontWeight="Bold" FontSize="14" Click="btnExternal_Click">
<Button.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF000000" Offset="0"/>
<GradientStop Color="#FFFFFFFF" Offset="1"/>
<GradientStop Color="#FFC44848" Offset="0.289"/>
</LinearGradientBrush>
</Button.Background>
</Button>
<Button x:Name="btnInternal" Margin="9,0,0,0" Width="175" Content="Load Default Site"
Foreground="#FF0F0D0D" FontWeight="Bold" FontSize="14" Click="btnInternal_Click">
<Button.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF000000" Offset="0"/>
<GradientStop Color="#FFFFFFFF" Offset="1"/>
<GradientStop Color="#FFA93030" Offset="0.211"/>
</LinearGradientBrush>
</Button.Background>
</Button>
</StackPanel>
</Border>

</Grid>
</Window>


{/codecitation}

Step 3: Navigating the Sites in WebBrowser control

I have given Name of WebBrowser control is myBrowser. If you see XAML code closely, you would find that.

Navigate method on WebBrowser control is used to load the site in the control.
So, if you want to load Google in WebBrowser control, you need to call Navigate method like below,

{codecitation class="brush: plain; gutter: true;" width="600px"}

myBrowser.Navigate(new Uri("http://www.google.com"));

{/codecitation}

Navigate Method

Image Loading....



Navigate method is overloaded with two arguments. One takes only URI address to be load and other takes URI source as well as Frame name along with other parameters.

Load Default Site Button

I have made codegain.com as default web site to be loaded. So when user will click on Load Default site button, this site will get loaded in the Browser control.

{codecitation class="brush: csharp; gutter: true;" width="700px"}

private void btnInternal_Click(object sender, System.Windows.RoutedEventArgs e)
{
myBrowser.Navigate(new Uri("http://www.codegain.com"));

}

{/codecitation}


Load Requested Site Button

When user will give any site address in textbox that site will get loaded on click event of this button.

{codecitation class="brush: csharp; gutter: true;" width="700px"}

private void btnExternal_Click(object sender, System.Windows.RoutedEventArgs e)
{
Uri ui = new Uri(txtLoad.Text.Trim(), UriKind.RelativeOrAbsolute);
myBrowser.Navigate(ui);
}
{/codecitation}


Windows.Xaml.cs

{codecitation class="brush: csharp; gutter: true;" width="700px"}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace webbrowsercontrol
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
myBrowser.Navigate(new Uri("http://www.google.com"));
}

private void btnExternal_Click(object sender, System.Windows.RoutedEventArgs e)
{
Uri ui = new Uri(txtLoad.Text.Trim(), UriKind.RelativeOrAbsolute);
myBrowser.Navigate(ui);
}

private void btnInternal_Click(object sender, System.Windows.RoutedEventArgs e)
{
myBrowser.Navigate(new Uri("http://www.codegain.com"));

}
}
}

{/codecitation}

Step 4: Run the application

Press F5 to run the application. If you see while loading Google.com is loaded in Browser Control. Because if you see closely Windows1() constructor , I am loading the browser control with google over there.

Image Loading....

Click on Load Default site button, it will load codegain.

Type some site address in textbox at right top corner and click Load requested site.

Image loading....

Able to Load Silverlight enabled site

Most interesting Part is type http://silverlight.net/ in the textbox and click on Load Requested site. It would load SilverLight site. It means WebBrowser control is capable of loading a Silverlight enabled site also.

Image Loading....

Conclusion:

We saw, how to work with BrowserControl in WPF. Please download the attached code for better reference. Happy Coding.

Thank you


About the Author


Dhananjay Kumar
Description :I done my engineering from Anand Engineering college Agra in 2007. I am MCTS WCF, MCTS MOSS Development, I am MCTS Web Development . I am native of Jamshedpur. Currently Please feel free to contact me regarding any clarification of my article at Dhananjay.25july@gmail.com

Occupation : Software Engineer
Company : UST Global.
Location : India
Follow me at twitter : http://twitter.com/dhananjay25


 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top