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 the开发者_StackOverflow社区m from a WPF form when the consumer clicks a button?
To launch the user's default browser to a specific web page you can do the following:
System.Diagnostics.Process.Start("http://www.google.com");
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();
Process.Start(new ProcessStartInfo("https://www.example.com") { UseShellExecute = true });
Note that I'm setting UseShellExecute = true
The default is true
on .Net Framework, and false
on .Net Core
apps. You need to set UseShellExecute to true
if you want to open a url using the default browser.
精彩评论