问题很简单但很烦人。我有一个按钮,点击事件只是打开一个链接
HtmlPage.Window.Navigate(uri, "_blank");
但它一直被浏览器阻止。我搜索了很多。似乎每个人都在使用这种方法,但没有人提到新的选项卡/窗口被阻止。那我该怎么办?
更新
问题解决了。似乎要导航到外部网页,应该使用HyperlinkButton。这不会被浏览器阻止。
“要启用用户导航到其他网页,您可以使用HyperlinkButton控件并将NavigateUri属性设置为外部资源,并设置TargetName属性以打开新的浏览器窗口。” --- MSDN, Silverlight - External navigation
<HyperlinkButton NavigateUri="http://www.microsoft.com" Content="Go to Microsoft" TargetName="_blank" />
PS。 HtmlPage.PopupWindow也被浏览器阻止。在我看来,没有用户手动禁用块,HtmlPage.Window.Navigate和HtmlPage.PopupWindow是无用的。
答案 0 :(得分:1)
您是否考虑过Silverlight 3和4中的System.Windows.Browser.HtmlPage.PopupWindow(uri, "_blank", null)
?
答案 1 :(得分:1)
您可以像这样使用System.Windows.Browser。 HtmlPage.Window.Eval :
HtmlPage.Window.Eval("mywindowopener('http://www.google.com'")
调用javascript函数“mywindowopener”并传递一个URL。然后在你的Javascript:
function mywindowopener(uri) {
window.loginDialog = window.open(uri, "popupwindow",
"height=320,width=480,location=no,menubar=no,toolbar=no");
}
“HtmlPage.Window.Eval”将绕过弹出窗口阻止程序,而“HtmlPage.Window.Invoke(mywindowopener,url)”或“HtmlPage.PopupWindow”则不会。
答案 2 :(得分:0)
Silverlight代码:
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();
}
包含Siverlight控件的Html页面:
<!--
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.
-->