Silverlight open uri link being blocked by browser

坚强是说给别人听的谎言 提交于 2019-12-22 07:57:07

问题


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, Silverlight - 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.


回答1:


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




回答2:


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.




回答3:


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.
-->


来源:https://stackoverflow.com/questions/5964026/silverlight-open-uri-link-being-blocked-by-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!