InvoiceParser/MainWindow.xaml.cs
2026-09-16 13:09:19 -07:00

48 lines
1.6 KiB
C#

using System.IO;
using System.Windows;
using Microsoft.Web.WebView2.Core;
namespace InvoiceParser;
public partial class MainWindow : Window
{
private readonly string _url;
public MainWindow(string url)
{
_url = url;
InitializeComponent();
Loaded += OnLoaded;
}
protected override void OnClosed(EventArgs e)
{
// Releases the WebView2 COM objects and terminates its msedgewebview2
// child processes. Left undisposed they keep this process alive after
// the window is gone.
try { Browser.Dispose(); } catch { }
base.OnClosed(e);
}
private async void OnLoaded(object sender, RoutedEventArgs e)
{
try
{
// Keep WebView2 profile data next to the exe so the app is fully portable.
var userData = Path.Combine(AppContext.BaseDirectory, "webview2_data");
var env = await CoreWebView2Environment.CreateAsync(null, userData);
await Browser.EnsureCoreWebView2Async(env);
Browser.CoreWebView2.Settings.AreDefaultContextMenusEnabled = true;
Browser.CoreWebView2.Settings.IsStatusBarEnabled = false;
Browser.CoreWebView2.Navigate(_url);
}
catch (Exception ex)
{
MessageBox.Show(
"WebView2 could not start: " + ex.Message +
"\n\nInstall the WebView2 Runtime (https://go.microsoft.com/fwlink/p/?LinkId=2124703) and try again.",
"Invoice Parser", MessageBoxButton.OK, MessageBoxImage.Error);
Close();
}
}
}