WPF应用程序内嵌网页

时间:2023-03-09 15:35:13
WPF应用程序内嵌网页

原文:WPF应用程序内嵌网页

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/shaynerain/article/details/78160984

WPF内嵌网页,可以将网页本地化,经查找相关资料后,决定采用CefSharp

1、首先新建WPF工程,打开工具进入NUGET,搜索CefSharp,然后安装CefSharp.Wpf

2、完成后,将项目改为x64或者x86,然后添加引用,这里有两种方法分开来说,大同小异

3、方法一:直接在xaml文件中引用,文件如下

<Window x:Class="WpfApplication1.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:cefSharp="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid> <cefSharp:ChromiumWebBrowser Name="mychrome" Grid.Row="0" Address="http://blog.csdn.net/shaynerain"/>
</Grid>
</Window>

4、方法二:在cs文件中添加引用,需要两个文件都做修改

using System.Windows;
using CefSharp.Wpf; namespace WpfApplication2
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
ChromiumWebBrowser webView = null;
public MainWindow()
{
InitializeComponent();
} private void Window_Loaded(object sender, RoutedEventArgs e)
{
string path = "http://blog.csdn.net/shaynerain"; webView = new ChromiumWebBrowser();
browserGrid.Children.Add(webView);
webView.Address = path;
}
}
}
<Window x:Class="WpfApplication2.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:WpfApplication2"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid Name="browserGrid"> </Grid>
</Window>



最后,虽然可以实现功能,但是使用起来,平滑感不友好