Skip to content

Commit 49716c9

Browse files
committed
fixed webview for windows
1 parent f7d33ca commit 49716c9

5 files changed

Lines changed: 61 additions & 25 deletions

File tree

src/UniGetUI.Avalonia/App.axaml.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Avalonia.Markup.Xaml.Styling;
66
using Avalonia.Platform;
77
using Avalonia.Styling;
8+
using Avalonia.Threading;
89
using UniGetUI.Avalonia.Infrastructure;
910
using UniGetUI.Avalonia.Views;
1011
using UniGetUI.Avalonia.Views.DialogPages;
@@ -35,6 +36,21 @@ public override void Initialize()
3536

3637
public override void OnFrameworkInitializationCompleted()
3738
{
39+
if (OperatingSystem.IsWindows())
40+
{
41+
// Safety net for NativeWebView (WebView2) initialization failures thrown
42+
// asynchronously on the dispatcher. Without this the app crashes; with it
43+
// the Help page shows a fallback "Open in browser" button.
44+
Dispatcher.UIThread.UnhandledException += (_, e) =>
45+
{
46+
if (e.Exception is InvalidOperationException { Message: var msg }
47+
&& msg.Contains("child window for native control host"))
48+
{
49+
e.Handled = true;
50+
}
51+
};
52+
}
53+
3854
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
3955
{
4056
if (OperatingSystem.IsMacOS())

src/UniGetUI.Avalonia/UniGetUI.Avalonia.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<AssemblyName>UniGetUI.Avalonia</AssemblyName>
2121
<ApplicationIcon>..\UniGetUI\icon.ico</ApplicationIcon>
2222
<AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault>
23+
<ApplicationManifest Condition="$([MSBuild]::IsOSPlatform('Windows'))">app.manifest</ApplicationManifest>
2324
</PropertyGroup>
2425

2526
<PropertyGroup Condition="'$(EnableAvaloniaDiagnostics)' == 'true'">

src/UniGetUI.Avalonia/Views/Pages/HelpPage.axaml.cs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,45 @@ public partial class HelpPage : UserControl, IEnterLeaveListener
99
{
1010
private readonly HelpPageViewModel _viewModel;
1111
private string _pendingNavigation = HelpPageViewModel.HelpBaseUrl;
12+
private bool _adapterReady;
1213

1314
public HelpPage()
1415
{
1516
_viewModel = new HelpPageViewModel();
1617
DataContext = _viewModel;
1718
InitializeComponent();
1819

19-
WebViewControl.NavigationStarted += OnNavigationStarted;
20-
WebViewControl.NavigationCompleted += OnNavigationCompleted;
21-
}
22-
23-
private void OnNavigationStarted(object? sender, WebViewNavigationStartingEventArgs e)
24-
{
25-
NavProgressBar.IsVisible = true;
26-
}
27-
28-
private void OnNavigationCompleted(object? sender, WebViewNavigationCompletedEventArgs e)
29-
{
30-
NavProgressBar.IsVisible = false;
31-
_viewModel.CurrentUrl = WebViewControl.Source?.ToString() ?? HelpPageViewModel.HelpBaseUrl;
20+
WebViewControl.NavigationStarted += (_, _) => NavProgressBar.IsVisible = true;
21+
WebViewControl.NavigationCompleted += (_, _) =>
22+
{
23+
NavProgressBar.IsVisible = false;
24+
_viewModel.CurrentUrl = WebViewControl.Source?.ToString() ?? HelpPageViewModel.HelpBaseUrl;
25+
BackButton.IsEnabled = WebViewControl.CanGoBack;
26+
ForwardButton.IsEnabled = WebViewControl.CanGoForward;
27+
};
3228

33-
BackButton.IsEnabled = WebViewControl.CanGoBack;
34-
ForwardButton.IsEnabled = WebViewControl.CanGoForward;
29+
// WebView2 on Windows initializes asynchronously after the control is attached
30+
// to the visual tree. Navigate() called before AdapterCreated is silently dropped.
31+
// This mirrors WinUI's EnsureCoreWebView2Async() pattern.
32+
WebViewControl.AdapterCreated += (_, _) =>
33+
{
34+
_adapterReady = true;
35+
WebViewControl.Navigate(new Uri(_pendingNavigation));
36+
};
3537
}
3638

3739
public void NavigateTo(string uriAttachment)
3840
{
3941
string url = _viewModel.GetInitialUrl(uriAttachment);
40-
if (WebViewControl.IsLoaded)
42+
_pendingNavigation = url;
43+
if (_adapterReady)
4144
WebViewControl.Navigate(new Uri(url));
42-
else
43-
_pendingNavigation = url;
4445
}
4546

4647
public void OnEnter()
4748
{
48-
WebViewControl.Navigate(new Uri(_pendingNavigation));
49+
if (_adapterReady)
50+
WebViewControl.Navigate(new Uri(_pendingNavigation));
4951
}
5052

5153
public void OnLeave() { }

src/UniGetUI.Avalonia/Views/Pages/ReleaseNotesPage.axaml.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public partial class ReleaseNotesPage : UserControl, IEnterLeaveListener
77
{
88
private readonly ReleaseNotesPageViewModel _viewModel;
99
private bool _loaded;
10+
private bool _adapterReady;
1011

1112
public ReleaseNotesPage()
1213
{
@@ -20,11 +21,21 @@ public ReleaseNotesPage()
2021
NavProgressBar.IsVisible = false;
2122
_viewModel.CurrentUrl = WebViewControl.Source?.ToString() ?? _viewModel.ReleaseNotesUrl;
2223
};
24+
25+
WebViewControl.AdapterCreated += (_, _) =>
26+
{
27+
_adapterReady = true;
28+
if (!_loaded)
29+
{
30+
WebViewControl.Navigate(new Uri(_viewModel.ReleaseNotesUrl));
31+
_loaded = true;
32+
}
33+
};
2334
}
2435

2536
public void OnEnter()
2637
{
27-
if (!_loaded)
38+
if (!_loaded && _adapterReady)
2839
{
2940
WebViewControl.Navigate(new Uri(_viewModel.ReleaseNotesUrl));
3041
_loaded = true;

src/UniGetUI.Avalonia/app.manifest

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,18 @@
77

88
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
99
<application>
10-
<!-- A list of the Windows versions that this application has been tested on
11-
and is designed to work with. Uncomment the appropriate elements
12-
and Windows will automatically select the most compatible environment. -->
13-
14-
<!-- Windows 10 -->
10+
<!-- Windows 10 and Windows 11 -->
1511
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
1612
</application>
1713
</compatibility>
14+
15+
<!-- PerMonitorV2 DPI awareness is required for native child windows (e.g. WebView2).
16+
Without this, Win32 NativeControlHost cannot create its HWND on Windows 10+. -->
17+
<application xmlns="urn:schemas-microsoft-com:asm.v3">
18+
<windowsSettings>
19+
<dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2, PerMonitor</dpiAwareness>
20+
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
21+
</windowsSettings>
22+
</application>
23+
1824
</assembly>

0 commit comments

Comments
 (0)