包括静态 JS 和 CSS WebBrowser 控件



我正在尝试将我的项目本地的 js 和 css 资源文件包含在作为字符串加载到 WPF WebBrowser 控件中的 html 页面上。

该项目为桌面应用程序生成 dll。 使用 Visual Studio 2013 和 C#。

HTML 在 Web 浏览器中加载,但我收到与包含 JS 文件相关的错误。

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        string page = GetPage();
        if (page != null)
        {
            string resourcePath = System.IO.Path.GetFullPath("Resources/");
            //  Try setting an absolute path
            page = page.Replace("Resources/", resourcePath);
            this.webbrowser.NavigateToString(page);
        }
    }

嵌入在 html 字符串中的内容如下:

<head>
    <script language="JavaScript" src="Resources/myscript.js"></script> 
    <link rel="stylesheet" type="text/css" href="Resources/mystyle.css"> 
</head>

我收到一个错误弹出窗口,例如:

An error has occurred in the script on this page.
URL:  aboutC:MyDirMyProjectbinDebugResources/myscript.js

它将"关于"附加到 URL。 我可能还需要更改斜杠。我尝试了很多东西。 "关于"始终在前面加上。

还尝试使用隔离存储,但这不适用于我的项目:http://msdn.microsoft.com/library/windows/apps/ff431811(v=vs.105).aspx

调用 NavigateToString 时必须使用绝对 URI。 由于您不是,IE 假定about:作为导致其失败的相对路径。

要么使用 file:///C:somepath.cssres://somedll.dll/path.css 或在随机高端口上使用 WCF 托管 HTTP 服务器并使用 http://localhost:37458/path.css

最新更新