簡體   English   中英

Silverlight Open URI鏈接被瀏覽器阻止

[英]Silverlight open uri link being blocked by browser

問題很簡單,但很煩人。 我有一個按鈕,而click事件只是通過打開一個鏈接

HtmlPage.Window.Navigate(uri, "_blank");

但是它一直被瀏覽器阻止。 我搜了很多。 似乎每個人都在使用這種方法,但是沒有人提到新標簽頁/窗口被阻止。 所以我該怎么做?

更新

問題解決了。 似乎要導航到外部網頁,應使用HyperlinkBut​​ton。 這不會被瀏覽器阻止。

“要允許用戶導航到其他網頁,可以使用HyperlinkBut​​ton控件,並將NavigateUri屬性設置為外部資源,並設置TargetName屬性以打開新的瀏覽器窗口。” --- MSDN,Silverlight-外部導航

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

PS。 HtmlPage.PopupWindow也被瀏覽器阻止。 在我看來,如果沒有用戶手動禁用該塊,則HtmlPage.Window.Navigate和HtmlPage.PopupWindow是無用的。

您可以使用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”則不會。

您是否在Silverlight 3和4中考慮過System.Windows.Browser.HtmlPage.PopupWindow(uri, "_blank", null)

除了最后一個null外,您還可以通過HtmlPopupWindowOptions設置一堆選項

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM