【问题标题】:Load a local web with WebView in a WPF application在 WPF 应用程序中使用 WebView 加载本地 Web
【发布时间】:2020-04-30 13:42:25
【问题描述】:

在 WPF 应用程序中,我如何加载一个简单的 Web,但它调用多个 JavaScript、CSS 等... 托管在硬盘上? 使用 WebBrowser 时,Web 应用程序无法运行,它会给出许多 JavaScript 错误,而使用 WebView 时,我无法加载 file:///c:/folder/web.html 类型的 url

【问题讨论】:

    标签: wpf webview


    【解决方案1】:

    您可以将文件复制到输出目录并使用WebViewNavigateToLocal 方法:

    const string LocalFilePath = "web.html";
    System.IO.File.Copy("c:/folder/web.html", LocalFilePath);
    webView.NavigateToLocal(LocalFilePath);
    

    或者您可以将文件的内容加载到string 并使用NavigateToString 方法:

    string s = System.IO.File.ReadAllText("c:/folder/web.html");
    webView.NavigateToString(s);
    

    【讨论】:

    • 使用NavigateToLocal 它对我有用,但它警告我此方法已过时并且我使用NavigateToLocalStream 并且无法使其正常工作
    • @PanzerIhnen:然后取消警告。或使用NavigateToString
    猜你喜欢
    • 2023-03-10
    • 1970-01-01
    • 2013-01-30
    • 2017-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-21
    • 2013-09-29
    相关资源
    最近更新 更多