CefSharpでAnyCPU対応に苦慮した話3
WPFでもCefSharp使えるのではと試行錯誤した挙句,いろいろとはまった
NuGetでCefSharp.Wpfをインストールして。参考ページのように以下を追加してみる。
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var browser = new CefSharp.Wpf.ChromiumWebBrowser();
browser.Address = "https://google.co.jp";
Content = browser;
}
}
まあ,このコードはうまくはいかない。ソリューションプラットフォームをAny CPUにしているからかも。
と言うことでまた,例の手順の登場。
*.csprojファイルの最初のProperyGroupの末尾(</PropertyGroup>の前あたり)に
<CefSharpAnyCpuSupport>true</CefSharpAnyCpuSupport>
を追加してVisual Studioを再起動した。
さらに,別の参考ページでWPFアプリケーションのMainを編集する方法を確認する。
App.xamlをソリューション エクスプローラーで選択しておいて,プロパティのビルドアクションをPageに変更する。
この状態でApp.xaml.csに編集を加える。
例えば次のように変更を加える。
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Reflection;
using System.IO;
using CefSharp;
using CefSharp.Wpf;
namespace WPFwebui
{
///
/// App.xaml の相互作用ロジック
///
public partial class App : Application
{
[STAThread]
public static void Main()
{
AppDomain.CurrentDomain.AssemblyResolve += Resolver;
InitializeChromium();
App app = new App();
app.InitializeComponent();
app.Run();
}
private static Assembly Resolver(object sender, ResolveEventArgs args)
{
if (args.Name.StartsWith("CefSharp"))
{
string assemblyName = args.Name.Split(new[] { ',' }, 2)[0] + ".dll";
string archSpecificPath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase,
Environment.Is64BitProcess ? "x64" : "x86",
assemblyName);
return File.Exists(archSpecificPath)
? Assembly.LoadFile(archSpecificPath)
: null;
}
return null;
}
private static void InitializeChromium()
{
CefSettings settings = new CefSettings();
settings.BrowserSubprocessPath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase,
Environment.Is64BitProcess ? "x64" : "x86",
"CefSharp.BrowserSubprocess.exe");
Cef.Initialize(settings, performDependencyCheck: false, browserProcessHandler: null);
}
}
}
一応動いた?

