银光(Silverlight)打开的URI链接被浏览器阻止

4
问题很简单但很烦人。我有一个按钮,点击事件只是通过链接打开一个网页。
HtmlPage.Window.Navigate(uri, "_blank");

但它一直被浏览器阻止。我搜索了很多,似乎每个人都在使用这种方法,但没有人提到新标签页/窗口被阻止的问题。那么我该怎么办呢?

更新

问题已解决。似乎要导航到外部网页,应使用HyperlinkButton。这不会被浏览器阻止。

"为了让用户导航到其他 web 页面,可以使用 HyperlinkButton 控件,并将 NavigateUri 属性设置为外部资源,将 TargetName 属性设置为打开新的浏览器窗口。" --- MSDN,Silverlight - 外部导航

<HyperlinkButton NavigateUri="http://www.microsoft.com" Content="Go to Microsoft" TargetName="_blank" />

PS. HtmlPage.PopupWindow在浏览器中也被屏蔽了。在我看来,如果用户不手动禁用阻止功能,则HtmlPage.Window.Navigate和HtmlPage.PopupWindow都是无用的。


如果在HTML页面中添加了一个超链接,打开一个新浏览器窗口的页面,会被阻止吗? - Emond
1
你是否启用了Html桥接:<param name="enableHtmlAccess" value="true" /> http://msdn.microsoft.com/en-us/library/cc645023(v=vs.95).aspx - Emond
@Erno,我尝试了这个方法,但仍然失败了。顺便说一下,SL托管在与应该在SL中打开的链接相同的域上。 - h--n
你确定添加了正确的参数并在浏览器中刷新了页面吗?如果没有启用HTML访问权限,HtmlPage根本不会起作用。 - Emond
@Erno,是的,我又试了一遍。实际上,SL能够在页面上调用JavaScript而无需启用enableHtmlAccess。看来enableHtmlAccess是为了跨域保护。 - h--n
显示剩余3条评论
3个回答

1
您有考虑过在Silverlight 3和4中使用System.Windows.Browser.HtmlPage.PopupWindow(uri, "_blank", null)吗?
除了最后一个null外,您还可以通过HtmlPopupWindowOptions设置一系列选项。

谢谢。我尝试了没有任何HtmlPopupWindowOptions的方法,但它也被阻止了。无论如何,通过使用HyperlinkButton解决了问题,请查看更新。 - h--n
HtmlPage.PopupWindow 可以正常工作,而 HtmlPage.Window.Navigate 只是调用 window.open JavaScript,HtmlPage.PopupWindow 禁用了弹出窗口拦截器,请参见源代码:NativeHost.Current.BrowserService.TogglePopupBlocker(true); - Alex Burtsev

1

您可以像这样使用System.Windows.Browser.HtmlPage.Window.Eval

    HtmlPage.Window.Eval("mywindowopener('http://www.google.com'")

调用名为“mywindowopener”的JavaScript函数并传递一个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

"则不会。"

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. https://dev59.com/EkXRa4cB1Zd3GeqPr3Vu
        //   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();
    }

包含 Silverlight 控件的 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.
-->

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接