Add WebView2Demo project

This commit is contained in:
Rosmontis_Cloud 2026-07-01 16:32:40 +08:00
parent f604d53191
commit e589d228c0
8 changed files with 156 additions and 1 deletions

@ -1 +0,0 @@
Subproject commit 1d85cb160f81d5a41125d348f97c01b2db0a09b4

9
WebView2Demo/App.xaml Normal file
View File

@ -0,0 +1,9 @@
<Application x:Class="WebView2Demo.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WebView2Demo"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>

13
WebView2Demo/App.xaml.cs Normal file
View File

@ -0,0 +1,13 @@
using System.Configuration;
using System.Data;
using System.Windows;
namespace WebView2Demo;
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}

View File

@ -0,0 +1,10 @@
using System.Windows;
[assembly:ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]

View File

@ -0,0 +1,22 @@
<Window x:Class="WebView2Demo.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:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
mc:Ignorable="d"
Title="无言势字幕 - WebView2 Demo" Height="500" Width="450"
Background="#0a0a0a">
<Grid>
<!-- WebView2 控件 -->
<wv2:WebView2 x:Name="WebView"
Source="https://www.baidu.com"/>
<!-- 底部按钮栏 -->
<StackPanel VerticalAlignment="Bottom" Background="#141414" Height="50">
<Button Content="刷新页面" Click="RefreshPage"
Background="#1e1e1e" Foreground="White"
Padding="15,5" Margin="10"/>
</StackPanel>
</Grid>
</Window>

View File

@ -0,0 +1,44 @@
using System;
using System.Windows;
using Microsoft.Web.WebView2.Wpf;
namespace WebView2Demo
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
InitializeWebView();
}
private async void InitializeWebView()
{
try
{
// 初始化 WebView2
await WebView.EnsureCoreWebView2Async();
Console.WriteLine("WebView2 初始化成功!");
// 可以设置自定义 UserAgent
// WebView.CoreWebView2.Settings.UserAgent = "MyApp/1.0";
// 监听页面加载完成事件
WebView.CoreWebView2.NavigationCompleted += (s, e) =>
{
Console.WriteLine("页面加载完成, Success: " + e.IsSuccess);
};
}
catch (Exception ex)
{
MessageBox.Show($"WebView2 初始化失败: {ex.Message}\n请确保已安装 WebView2 Runtime",
"错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void RefreshPage(object sender, RoutedEventArgs e)
{
WebView.Reload();
}
}
}

43
WebView2Demo/README.md Normal file
View File

@ -0,0 +1,43 @@
# WebView2Demo
无言势字幕 - WebView2 学习项目
## 技术栈
- C# / .NET 8
- WPF (Windows Presentation Foundation)
- Microsoft WebView2 (Chrome 内核)
## 项目结构
```
WebView2Demo/
├── MainWindow.xaml # WPF 窗口
├── MainWindow.xaml.cs # 窗口逻辑
├── App.xaml # 应用入口
└── WebView2Demo.csproj # 项目配置
```
## 运行
```bash
dotnet restore
dotnet run
```
## 功能
- WebView2 控件加载网页
- C# 与 JavaScript 通信
- WPF 窗口嵌入浏览器
## 学习笔记
查看 [C# 学习笔记目录](../notes/)
- Lesson 01-10: C# 基础
- Lesson 11: WebView2 入门
## 作者
Rosmontis_Cloud

View File

@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.4022.49" />
</ItemGroup>
</Project>