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();
        }
    }
}
