天天看點

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>      

最後,雖然可以實作功能,但是使用起來,平滑感不友好

繼續閱讀