Silverlight open uri link being blocked by browser
The problem is simple but annoying. I have a button and the click event just opens a link by
HtmlPage.Window.Navigate(uri, "_blank");
But it keeps being blocked by the browser. I searched a lot. It seems everyone is using this method but no one mentioned the new tab/windows is being blocked. So what should I do?
UPDATE
Problem solved. It seems that to navigate to outside web pages, HyperlinkButton should be used. This is not blocked by the browser.
"To enable user navigation to other Web pages, you can use the HyperlinkButton control and set the NavigateUri property to the external resource and set the TargetName property to open a new browser window." --- MSDN, Sil开发者_JAVA百科verlight - External navigation
<HyperlinkButton NavigateUri="http://www.microsoft.com" Content="Go to Microsoft" TargetName="_blank" />
PS. HtmlPage.PopupWindow is also blocked by the browser. It seems to me that HtmlPage.Window.Navigate and HtmlPage.PopupWindow are useless without the user manually disable the block.
Have you considered System.Windows.Browser.HtmlPage.PopupWindow(uri, "_blank", null)
in Silverlight 3 and 4?
Instead of the last null, you can also set a bunch of options via HtmlPopupWindowOptions
You can use System.Windows.Browser.HtmlPage.Window.Eval like this:
HtmlPage.Window.Eval("mywindowopener('http://www.google.com'")
to call a javascript function "mywindowopener" and pass a URL. Then in your Javascript:
function mywindowopener(uri) {
window.loginDialog = window.open(uri, "popupwindow",
"height=320,width=480,location=no,menubar=no,toolbar=no");
}
"HtmlPage.Window.Eval" will bypass the popup blocker whereas "HtmlPage.Window.Invoke(mywindowopener,url)" or "HtmlPage.PopupWindow" will not.
Silverlight Code:
public static void OpenWindow(string url, WindowTarget target = WindowTarget._blank)
{
// This will be blocked by the pop-up blocker in some browsers
// HtmlPage.Window.Navigate(new Uri(url), target.ToString());
// Workaround: use a HyperlinkButton, but do make sure for IE9, you need to have
// <meta http-equiv="x-ua-compatible" content="IE=8" />
// and for all browsers, in the Silverlight control:
// <param name="enableNavigation" value="true" />
// Also, it seems the workaround only works in a user-triggered event handler
//
// References:
// 1. http://stackoverflow.com/questions/186553/sending-a-mouse-click-to-a-button-in-silverlight-2
// 2. http://stackoverflow.com/questions/14678235/silverlight-hyperlinkbutton-not-working-at-all
HyperlinkButton hb = new HyperlinkButton()
{
NavigateUri = new Uri(url),
TargetName = target.ToString()
};
(new HyperlinkButtonAutomationPeer(hb) as IInvokeProvider).Invoke();
}
Html Page containing the Siverlight control:
<!--
http://stackoverflow.com/tags/x-ua-compatible/info
X-UA-Compatible is a IE-specific header that can be used to tell modern IE versions to
use a specific IE engine to render the page. For example, you can make IE8 use IE7 mode
or tell IE to use the newest available rendering engine.
-->
<meta http-equiv="x-ua-compatible" content="IE=8" />
<!-- If we don't have the the above meta tag, Silverlight HyperlinkButton won't work in IE9
Some Security issue (Unathorized access exception)
TODO:
1. Check if IE10 has the same issue or not;
2. Test this in IE7 or IE6.
-->
精彩评论