我试图让我的 Silverlight 4.0 应用程序在用户单击某些内容以将它们带到其 Web URL 时启动关联的程序文件的文件扩展名,但无论我使用 HtmlPage.Window.Navigate 还是 HyperlinkButton,我都有不同的体验。
我的 Web URL 是我的资源的 RESTful URL,例如“http://.../objects/object-1/package”。 URL 实际上是一个 ASP.NET MVC 2 Controller ,它返回带有自定义扩展名的 zip 内容。也就是说,HTTP 响应 header 如下所示:
HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Wed, 13 Apr 2011 17:22:19 GMT
X-AspNet-Version: 4.0.30319
Content-Disposition: attachment; filename=object-1.pkg
Transfer-Encoding: chunked
Cache-Control: private
Content-Type: application/zip
Connection: Close
当我使用超链接按钮时,Internet Explorer 会提示我打开、保存或取消。当我选择打开时,它会打开我与 *.pkg 关联的应用程序:
<HyperlinkButton TargetName="_blank" NavigateUri="http://localhost:8023/objects/object-1/package">Launch!</HyperlinkButton>
但是,如果我改为使用最终使用 HtmlPage.Window.Navigate 的命令,IE 只会打开一个窗口,然后快速关闭:
string url = string.Format("{0}/objects/object-{1}/package", _webBaseUrl, objectId.Value);
Uri uri = new Uri(url);
HtmlPage.Window.Navigate(uri, "_blank");
我已经使用 Fiddler2 验证了在这两种情况下,HTTP 请求和 HTTP 响应看起来相同。这似乎是 Internet Explorer 或 Silverlight 的细微差别,我不确定我能否克服,但我希望 Stackoverflow 社区能够确认或否认这个问题。
最佳答案
有一篇短文here这对这个问题有所启发 - 我发现 HtmlPage.Window.Navigate
在 IE 之外根本不适合我。
但回到最初的问题,使用 dotPeek 我可以看到以下差异:
跟踪 HyperlinkButton OnClick,它将调用委托(delegate)给 agcore:(XcpImports.cs)
[DllImport("agcore", EntryPoint = "NavigateToSafeURI", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
private static uint NavigateToSafeURINative(IntPtr context, [MarshalAs(UnmanagedType.LPWStr)] string location, [MarshalAs(UnmanagedType.LPWStr)] string target, bool checkUserInitiatedAction);
和 HtmlPage.Window.Navigate(uri) 进行此调用:
[DllImport("agcore")]
public static int DOM_Invoke(IntPtr pBrowserService, IntPtr pObject, [MarshalAs(UnmanagedType.LPWStr)] string pszMethodName, int nArgCount, [MarshalAs(UnmanagedType.LPArray)] NativeMethods.ScriptParam[] ppParams, ref NativeMethods.ScriptParam pResult, ref NativeMethods.ExceptionInfo pExcepInfo);
关于silverlight - Silverlight 的 HtmlPage.Window.Navigate 和 HyperlinkButton 的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5654814/