【问题标题】:include static JS and CSS WebBrowser control包括静态 JS 和 CSS WebBrowser 控件
【发布时间】:2015-02-24 01:15:36
【问题描述】:

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

该项目为桌面应用程序构建一个 dll。使用 Visual Studio 2013 和 C#。

HTML 在 WebBrowser 中加载,但出现与包含 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:\MyDir\MyProject\bin\Debug\Resources/myscript.js

它在 URL 前面加上“关于”。我可能还需要更改斜线。 我尝试了很多东西。 "about" 总是放在前面。

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

【问题讨论】:

标签: c# wpf webbrowser-control


【解决方案1】:

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

use file:///C:\some\path.cssres://somedll.dll/path.csshost a HTTP server using WCF 在随机高端口上并使用 http://localhost:37458/path.css

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多