Noble-Surfer
Noble-Surfer

Reputation: 3172

C# application- how to display a website inside a xaml form?

I am trying to add a web browser to an existing C# application, but, having not used C# in about 6 years, I am quite unfamiliar with how it works.

The GUI for the application has been created using XAML, and I want to use this to add/ embed the web browser within the application, but I'm not sure how to do this.

I have the following script inside my WebBrowser.xaml class:

<Window x:Class="RiviamAgent.WebBrowser"
    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"
    xmlns:local="clr-namespace:RiviamAgent"
    mc:Ignorable="d"
    Title="WebBrowser" Height="500" Width="500">
<Window.CommandBindings>
    <CommandBinding Command="New" CanExecute="CommonCommandBinding_CanExecute" />
    <CommandBinding Command="Open" CanExecute="CommonCommandBinding_CanExecute" />
    <CommandBinding Command="Save" CanExecute="CommonCommandBinding_CanExecute" />
</Window.CommandBindings>
<DockPanel>
    <ToolBarTray DockPanel.Dock="Top">
        <ToolBar Header="File">
            <Button Command="New" Content="New" ToolBar.OverflowMode="Always" />
            <Button Command="Open" Content="Open" ToolBar.OverflowMode="Always" />
            <Button Command="Save" Content="Save" ToolBar.OverflowMode="Always" />
        </ToolBar>
        <ToolBar Header="Edit" Margin="5.4,0,-5.4,0">
            <Button Command="Cut" Content="Cut" ToolBar.OverflowMode="Always" />
            <Button Command="Copy" Content="Copy" ToolBar.OverflowMode="Always" />
            <Button Command="Paste" Content="Paste" ToolBar.OverflowMode="Always" />
        </ToolBar>
        <ToolBar Margin="9.2,0,-8.2,0">
            <Button Command="Back" ToolTip="Return to the previous page"/>
            <Image Source="C:\Users\elgan\workspace\browser\riviam_windows\Images\navigateBack.png" Width="20" Height="20" Margin="0,0,0,2.4" />
        </ToolBar>
        <ToolBar Margin="16.4,0,-16.2,0" >
            <Button Command="Forward" ToolTip="Proceed to the next page" />
            <Image Source="C:\Users\elgan\workspace\browser\riviam_windows\Images\navigateForward.png" Width="20" Height="20" />
        </ToolBar>
    </ToolBarTray>
    <TextBox AcceptsReturn="True" />
    <WebBrowser Name="browser" Navigating="www.google.com"/>
    <TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="12">
       Web page
    </TextBlock>

</DockPanel>

and this is currently showing a blank page, with a toolbar at the top containing 'File' & 'Edit' menus, as well as 'Back' and 'Forward' navigation buttons.

I am now trying to display the contents of a website in the rest of the page, but I'm not sure how to do this.

In the last couple of lines that I have written, I am trying to get Google to display in the main area of the GUI (i.e. below the menus and navigation buttons), but at the moment, nothing is being displayed there, other than the text, "Web page".

Can anyone tell me why this is? How can I get the form to display Google or any other website? Thanks in advance.

Upvotes: 2

Views: 3785

Answers (2)

Paul Zyapa
Paul Zyapa

Reputation: 19

First of all Navigating="www.google.com" - a correct website name would be: https://www.google.com

Try to start all url with http:// or https://

And to navigate trough websites, try to put this somewhere in MainWindow.xaml.cs after

 public MainWindow()
        {
            InitializeComponent();

WebBrowser_Name.Navigate(new Uri(@"https://google.com"));

Upvotes: 0

howamith
howamith

Reputation: 201

Try changing:

Navigating="www.google.com"

to...

Source="http://www.google.com"

Hope that helps!

Upvotes: 2

Related Questions