Search code examples
wpfwinformsconsole-application

Navigate to a URL (for opening a web page) from a WPF application


I am writing a consumer application for TwitPic with OAuth Echo.

I need to navigate the user to a web page when they try to get a pin code from Twitter.

How can I navigate them from a WPF form when the consumer clicks a button?


Solution

  • You could host a WebBrowser control in it's own window and open that window from your button click:

    Window Hosting the BrowserControl:

    <Window x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
    <DockPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <WebBrowser x:Name="Browser" ></WebBrowser>
    </DockPanel>
    

    In the click event of your button, use the WebBrowser control's Navigate method:

    Window1 w = new Window1();
            w.Browser.Navigate(new Uri("http://stackoverflow.com"));
            w.Show();