vs中webbrowser的选择。 有自带的WebBrowser,WebKit .NET,CefSharp,通过比较最后选定CefSharp,

对各个控件的使用进行说明 。

1.WebBrowser

基本IE浏览器内核,受限比较多。

1.wpf webbrowser中调用网页,报js脚本错误

绑定silent属性为true就可以了。

//初始化
MyWebBrowser.Navigating += MyWebBrowser_Navigating;
private void MyWebBrowser_Navigating(object sender, NavigatingCancelEventArgs e)
{
    FieldInfo fi = typeof(WebBrowser).GetField("_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic);
    if (fi != null)
    {
        object browser = fi.GetValue(MyWebBrowser);
        if (browser != null)
            browser.GetType().InvokeMember("Silent", BindingFlags.SetProperty, null, browser, new object[] { true });
    }
}
2.加载对应的网址
 private void ShowUrl(string urlAddress="")
 {
     if (!string.IsNullOrEmpty(urlAddress))
     {
         //if (urlAddress.IndexOf("http://") <= 0 ||)
         //    urlAddress = "http://" + urlAddress;
         Uri url = new Uri(urlAddress);

         MyWebBrowser.Navigate(url);
     }

 }
检测你的浏览器版本太低

1578494278234

2.WebKit .NET

WebKit.net是对WebKit的.Net封装,使用它.net程序可以非常方便的集成和使用webkit作为加载网页的容器。

下载地址:

https://sourceforge.net/projects/webkitdotnet/

WebKit 是一个开源的浏览器引擎,但是调查发现,WebKit.net已经在15年以后没有更新了。并且只支持32位机。

现在CefSharp用的比较多。

3.CefSharp

开源地址:https://github.com/cefsharp/cefsharp

安装完以后要看下目录下的readme.txt .有一些注意事项关注 。

注意:cefsharp不支持anycpu所以要配置x86,x64两组。 最新版 通过设置可以解决。

​ cefsharp安装完以后,必须重启vs才可以正常使用。

​ framework 版本必须是4.5.2以上

通过Nuget安装,右击项目-》管理Nuget程序包-》在打开的界面中搜索CefSharp,左边选联机

1578495759375

点击安装会自动下载安装

下载完以后会在当前目录下的packages下

1578497671268

在wfp中引用cef sharp

xmlns:cefsharp="clr-namespace:CefSarp.Wpf;assembly=CefSharp.Wpf"

使用

<cefsharp:ChromiumWebBrowser x:Name="MyWebBrowser" Address="http://www.baidu.com"></cefsharp:ChromiumWebBrowser>

编译后报错

错误 7 XML 命名空间“clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf”中不存在标记“ChromiumWebBrowser”。

错误 3 未知的生成错误“因为没有预加载,所以无法解析程序集“CefSharp, Version=75.1.143.0, Culture=neutral, PublicKeyToken=40c4b6fc221f4138”的依赖项。在使用 ReflectionOnly API 时,必须通过 ReflectionOnlyAssemblyResolve 事件预加载或按需要加载依赖程序集。 行 147 位置 18.” E:projectMyMasterjdhelperMainWindow.xaml 147 18 013.京东金融抽金币

错误 1 命名空间“clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf”中不存在“ChromiumWebBrowser”名称。 E:projectMyMasterjdhelperMainWindow.xaml 147 17 013.京东

以上错误,查看CefSharp.Wpf 发现有依赖引用。

1578839627464

可能 是因为先开始.net framework version版本一是4.5.2,后来改为4.5.2 导致Cefsharp.common没有下载 。

重新在nuget包中下载cefsharp.common 就可以了。

下载完以后编译继续报错。

Please check to make sure that you have specified a valid combination

The OutputPath property is not set for project '013.jdrj.csproj'.  Please check to make sure that you have specified a valid combination of Configuration and Platform for this project.  Configuration='Debug'  Platform='x64'.  This error may also appear if some other project is trying to follow a project-to-project reference to this project, this project has been unloaded or is not included in the solution, and the referencing project does not build using the same or an equivalent Configuration or Platform.    013.京东金融抽金币

打开项目文件,发现

 <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">

只有很简单的一行代码。连输出的outputpath都没有配置

把相应的文件从Debug|x32上拷贝下来,然后再编译就可以了。

<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
    <DebugSymbols>true</DebugSymbols>
    <OutputPath>bin\x86\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <DebugType>full</DebugType>
    <PlatformTarget>x86</PlatformTarget>
    <ErrorReport>prompt</ErrorReport>
    <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
    <Prefer32Bit>true</Prefer32Bit>
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
  </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
    <DebugSymbols>true</DebugSymbols>
    <OutputPath>bin\x64\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <DebugType>full</DebugType>
    <PlatformTarget>x64</PlatformTarget>
    <ErrorReport>prompt</ErrorReport>
    <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
    <Prefer32Bit>true</Prefer32Bit>
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
  </PropertyGroup>

本文由 hcb 创作,采用 知识共享署名 3.0,可自由转载、引用,但需署名作者且注明文章出处。

2 条评论

  1. 西冷茶
    西冷茶

    牛批,问题解决了,谢谢

    1. hcb
      hcb

      不错

添加新评论