【问题标题】:Silverlight HyperlinkButton NOT Working at ALLSilverlight HyperlinkBut​​ton 根本不工作
【发布时间】:2013-02-03 22:43:52
【问题描述】:

我正在尝试根据this documentation实现Siverlight HyperlinkBut​​ton:

<HyperlinkButton Content="Click here to learn about Silverlight"
                 NavigateUri="http://www.silverlight.net" 
                 TargetName="_blank" 
                 Margin="10,60,0,0"/>

但是,当我构建并单击它时,它什么也没显示。

【问题讨论】:

标签: silverlight button hyperlink


【解决方案1】:

我的猜测是您没有为 Silverlight 对象启用外部导航。如果您没有单击链接时会引发安全异常,但您可能会在不知情的情况下捕获它。上述链接的备注部分对此进行了描述。

要启用导航,请编辑您的 Silveright 主机页面(例如 index.html)并在 Silverlight 对象下添加以下参数

<param name="enableNavigation" value="true" />

默认情况下,Silverlight 会在您每次运行/调试时生成 .html 页面,因此您将丢失参数。要解决此问题,请在同一目录中使用不同的名称复制 .html 文件,或手动指定要在项目设置中使用的 .html 文件。

http://msdn.microsoft.com/en-us/library/dd833071(v=vs.95).aspx

更新:

这适用于 Chrome、Firefox 和 IE8。它不适用于 IE9。您可以使用开发人员工具在 IE 中对此进行测试。链接适用于文档模式 IE8,但在使用文档模式 IE9 时会引发未经授权的访问异常。

更新 #2:

要让它在 IE9 中工作,您可以强制它使用 IE8 文档模式。为此,您需要删除 DOCTYPE 标记并添加元标记。例如

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <meta http-equiv="x-ua-compatible" content="IE=8">
    .
    .

代替

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    .
    .

【讨论】:

  • 在你的 App.xaml.cs 中你可以在 Application_UnhandledException 中设置一个断点,看看你点击链接时是否抛出了异常?
  • ExceptionObject: {System.UnauthorizedAccessException: Attempted to perform an unauthorized operation. 是不是跨域问题?
  • 我可以重现该错误。添加上面的参数为我修复了它。不过你需要小心。每次构建或调试应用程序时,它都会将修改后的 .html 文件替换为正常文件。在同一目录中使用不同名称复制 .html 文件,在新文件中添加参数并通过打开新的 .html 文件启动您的 Silverlight 应用程序并查看它的运行情况
  • 是的,我正在使用特定的测试页面。我在运行它时通过检查 html 源代码来验证它。
  • 嗯。我刚刚注意到,对我来说,链接在 Chrome 中有效,但在 IE 中失败。现在看看为什么
【解决方案2】:

您应该访问此链接以解决超链接按钮问题,否则......它正在努力工作......

<UserControl x:Class="HyperlinkButton2.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="300" Height="100">
    <Canvas x:Name="LayoutRoot" Background="White">
        <HyperlinkButton x:Name="hbtnTest" TargetName="_blank" Content="SilverlightShow" NavigateUri="http://www.silverlightshow.net" Canvas.Top="40" Canvas.Left="30"></HyperlinkButton>
    </Canvas>

Using the HyperlinkButton control in Silverlight

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-19
    • 1970-01-01
    • 2015-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多