Silverlight Tip of the Day #52 – How to Popup a Browser Window
Let’s say a user clicks on a button and you want to popup a separate browser and point them to a specific web page. How do you do this in Silverlight?
Silverlight now supports a method called HtmlPage.PopupWindow(). For security reasons this call can only be made in response to any user input such as a button click.
To use this method you will need to add a reference to System.Windows.Browser;
The call to HtmlPage.PopupWindow() takes three parameters:
- Uri – The location to browse to (I.e http://www.silverlight.net).
- String – The name you want to call the target window.
- HtmlPopupWindowOptions – A variety of options such as window positioning, sizing. Also, whether the toolbar and menubar are visible and more.
The following code demonstrates how to do this in response to a button click from the user:
private void Button_Click(object sender, RoutedEventArgs e)
{
HtmlPopupWindowOptions options = new HtmlPopupWindowOptions();
options.Left = 0;
options.Top = 0;
options.Width = 800;
options.Height = 600;
if(true == HtmlPage.IsPopupWindowAllowed)
HtmlPage.PopupWindow(new Uri("http://www.silverlight.net"), "new", options);
}
*Note: This method is not supported in Safari because it does not implement the right NPAPI contract.
Thank you,
--Mike Snow
Subscribe in a reader