Browser.HtmlPage.Window.Navigate被阻止,但HyperlinkBut​​ton未被阻止-为什么? [英] Browser.HtmlPage.Window.Navigate is blocked but HyperlinkButton isn't - why?

查看:119
本文介绍了Browser.HtmlPage.Window.Navigate被阻止,但HyperlinkBut​​ton未被阻止-为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个特定的UI元素,单击该元素应导航到另一个URL. 我尝试处理鼠标按下事件并执行以下操作: System.Windows.Browser.HtmlPage.Window.Navigate(new Uri("http://google.com"),"_blank");

I have a certain UI element, which when clicked should navigate to another URL. I tried handling the mouse down event and execute something like this: System.Windows.Browser.HtmlPage.Window.Navigate(new Uri("http://google.com"), "_blank");

但是,除非禁用了弹出窗口阻止程序,否则它会被浏览器阻止(我尝试使用Chrome和Firefox). 我做了一些研究,似乎浏览器阻止了由于用户交互而不会发生的导航,并且就浏览器而言,这种导航是由插件(Silverlight)而不是用户启动的.

However, this gets blocked by the browser (I tried both Chrome and Firefox) unless I disable the popup blocker. I did some research and it seems that the browser blocks navigations that don't occur as a result of user interaction and as far as the browser is concerned this navigation is initiated by a plugin (Silverlight), not a user.

好的,很有道理.但是后来我尝试使用HyperlinkBut​​ton来代替,它没有被阻止.

OK, makes sense. But then I tried using a HyperlinkButton instead and it was NOT blocked.

我想知道为什么这两种方法从浏览器中得到不同的对待.有什么主意吗?

I wonder why these two approaches get different treatment from the browser. Any idea?

我实际上是通过用HyperLinkBut​​ton包装导航触发UI来解决此问题的,但我仍然很好奇...

I actually worked around the problem by wrapping the navigation triggering UI with a HyperLinkButton, but I'm still very curious...

推荐答案

我要引用一个华丽的公司报价,并说这是一个功能,而不是错误."

I'm going to pull a fancy corporate quote and say "It is a feature, not a bug."

Silverlight团队显然担心安全性.他们不希望像我这样疯狂的haxorz和我使用我们的应用程序做疯狂的事情-您知道,就像弹出一堆浏览器窗口一样,将人们路由到Zombo.com.想象一下合唱团! Zombo!

The Silverlight team is obviously worried about Security. They don't want crazy haxorz like you and I to do crazy things with our apps--you know, like popping up a bunch of browser windows all routing people to Zombo.com. Just imagine the choir! Zombo!

摘自其文档:他们希望我们仅使用HyperlinkBut​​ton进入其应用程序之外.实际上,他们去了额外的步骤,并且根据我们的设置,即使我们从超链接按钮导航到外部页面,它们甚至还会为我们抛出SecurityException.很好.

Summarized from their documentation: They want us to only use the HyperlinkButton to go outside of their application. In fact, they went the extra step, and depending on our settings, they will even throw a SecurityException for us if we navigate to an outside page--Even from a HyperlinkButton. How nice.

真正的问题:为什么使用超链接按钮而不是其他东西?

The real question: Why the Hyperlink Button and not something else?

只有一种事物"可以导航.这样可以节省Microsoft在测试Silverlight时的时间.这件事既可以导航到内部XAML页面,也可以导航到外部Web页面-可能也是一致的,并且只有一种导航方式.

Only one "thing" can navigate. this take saves time for Microsoft while testing Silverlight. This one thing can navigate to both internal XAML pages and external web pages--Might as well be consistent and have only one way to do navigation.

它是一个UIElement.它的代码背后喜欢在主视觉线程中运行.它可以向浏览器保证Visual Element想要放到某个地方. Microsoft还可以将其限制逻辑放入需要鼠标单击/键盘输入事件树的控件中.

It is a UIElement. It's code behind likes to run in the primary visual thread. It can promise the browser that a Visual Element wants to go somewhere. Microsoft can also put its limiting logic in a control that requires a mouse-click/keyboard-input event tree.

总而言之,有意义的是通过使控件完成工作来简单地开始.

All in all, it makes sense to start simple by making a control do the work.

好玩的东西!希望对您有帮助.

Fun stuff! Hope this helps you.

这篇关于Browser.HtmlPage.Window.Navigate被阻止,但HyperlinkBut​​ton未被阻止-为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆