1.建立Winform專案
#這裡.NET Framework 4.7.2版本
2.選擇 "Manage NuGet Packages"
3.搜尋 "CefSharp.WinForms",安裝這個套件
4.Program.cs 加入程式碼
2.選擇 "Manage NuGet Packages"
3.搜尋 "CefSharp.WinForms",安裝這個套件
4.Program.cs 加入程式碼
using CefSharp.WinForms;
using CefSharp;
using System;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
internal static class Program
{
[STAThread]
static void Main()
{
// 初始化 CefSharp
CefSettings settings = new CefSettings();
Cef.Initialize(settings, performDependencyCheck: true, browserProcessHandler: null);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
5.From1.cs 加入程式碼
using CefSharp.WinForms;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
InitializeBrowser();
}
private void InitializeBrowser()
{
var browser = new ChromiumWebBrowser("http://www.google.com");
Controls.Add(browser);
browser.Dock = DockStyle.Fill;
}
}
}
6.完成
WPF Chrome瀏覽器套件實作
1.建立WPF專案
using CefSharp.Wpf;
using CefSharp;
using System.Windows;
namespace WpfApp4
{
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
// 初始化 CefSharp
CefSettings settings = new CefSettings();
Cef.Initialize(settings, performDependencyCheck: true, browserProcessHandler: null);
}
}
}
5.MainWindow.xaml 加入程式碼
<Window x:Class="WpfApp4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp4"
xmlns:cefSharp="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<cefSharp:ChromiumWebBrowser Address="http://www.google.com" />
</Grid>
</Window>
6.完成
沒有留言:
張貼留言