Skip to content

Commit 0690235

Browse files
committed
Test ApplicationManifest & Fix Sample
1 parent ca432f5 commit 0690235

File tree

10 files changed

+177
-24
lines changed

10 files changed

+177
-24
lines changed

src/Avalonia.WebView2.Sample/App.xaml.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Avalonia.Markup.Xaml;
2+
using AvaloniaWebView2 = Avalonia.Controls.WebView2;
23

34
namespace Avalonia.WebView2.Sample;
45

@@ -7,6 +8,7 @@ public class App : Application
78
public override void Initialize()
89
{
910
AvaloniaXamlLoader.Load(this);
11+
InitWebView2();
1012
}
1113

1214
public override void OnFrameworkInitializationCompleted()
@@ -18,4 +20,37 @@ public override void OnFrameworkInitializationCompleted()
1820

1921
base.OnFrameworkInitializationCompleted();
2022
}
23+
24+
public static bool AvailableWebView2 { get; private set; }
25+
26+
public static string? WebView2VersionString { get; private set; }
27+
28+
static void InitWebView2()
29+
{
30+
try
31+
{
32+
WebView2VersionString = CoreWebView2Environment.GetAvailableBrowserVersionString();
33+
if (!string.IsNullOrEmpty(WebView2VersionString)) AvailableWebView2 = true;
34+
}
35+
catch (WebView2RuntimeNotFoundException)
36+
{
37+
38+
}
39+
40+
if (AvailableWebView2)
41+
{
42+
AvaloniaWebView2.DefaultCreationProperties = new()
43+
{
44+
Language = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName,
45+
UserDataFolder = GetUserDataFolder(),
46+
};
47+
48+
static string GetUserDataFolder()
49+
{
50+
var path = Path.Combine(AppContext.BaseDirectory, "AppData", "WebView2", "UserData");
51+
if (!Directory.Exists(path)) Directory.CreateDirectory(path);
52+
return path;
53+
}
54+
}
55+
}
2156
}

src/Avalonia.WebView2.Sample/Avalonia.WebView2.Sample.csproj

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<OutputType>WinExe</OutputType>
55
<TargetFramework>net6.0-windows</TargetFramework>
6+
<ApplicationManifest>Properties\app.manifest</ApplicationManifest>
67
</PropertyGroup>
78

89
<ItemGroup>
9-
<AvaloniaXaml Include="**\*.xaml" />
10+
<AvaloniaXaml Include="**\*.xaml" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<None Remove="WebView2Compat.xaml" />
1015
</ItemGroup>
1116

1217
<ItemGroup>
@@ -25,8 +30,11 @@
2530
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" />
2631
</ItemGroup>
2732

28-
<!--<ItemGroup>
33+
<ItemGroup>
2934
<ProjectReference Include="..\Avalonia.WebView2\Avalonia.WebView2.csproj" />
35+
</ItemGroup>
36+
37+
<!--<ItemGroup>
3038
<Reference Include="Microsoft.Web.WebView2.Core">
3139
<HintPath>..\..\ref\Microsoft.Web.WebView2\lib\netcoreapp3.0\Microsoft.Web.WebView2.Core.dll</HintPath>
3240
</Reference>

src/Avalonia.WebView2.Sample/MainWindow.xaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
xmlns="https://github.com/avaloniaui"
44
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
55
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6+
xmlns:local="using:Avalonia.WebView2.Sample"
67
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
78
xmlns:wv2="clr-namespace:Avalonia.Controls;assembly=Avalonia.WebView2"
89
Title="Avalonia.WebView2.Sample"
@@ -23,10 +24,10 @@
2324
FontWeight="Bold" />
2425
</Grid>
2526
<Grid Grid.Row="1" Background="BlueViolet">
26-
<wv2:WebView2
27+
<local:WebView2Compat x:Name="WebView2Compat" />
28+
<!--<wv2:WebView2
2729
x:Name="WebView2"
28-
CreationProperties="{Binding CreationProperties}"
29-
Source="https://www.bing.com" />
30+
Source="https://www.bing.com" />-->
3031
</Grid>
3132
<Grid Grid.Row="2">
3233
<Button

src/Avalonia.WebView2.Sample/MainWindow.xaml.cs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,9 @@ public partial class MainWindow : Window
77
{
88
readonly Button Button;
99
readonly AvaloniaWebView2 WebView2;
10+
readonly WebView2Compat WebView2Compat;
1011
readonly new Label Title;
1112

12-
static string GetUserDataFolder()
13-
{
14-
var path = Path.Combine(AppContext.BaseDirectory, "AppData", "WebView2", "UserData");
15-
if (!Directory.Exists(path)) Directory.CreateDirectory(path);
16-
return path;
17-
}
18-
19-
public CoreWebView2CreationProperties CreationProperties { get; } = new()
20-
{
21-
Language = "en",
22-
UserDataFolder = GetUserDataFolder(),
23-
};
24-
2513
public CoreWebView2Environment Environment { get; }
2614

2715
public MainWindow()
@@ -33,10 +21,11 @@ public MainWindow()
3321
#endif
3422
Title = this.FindControl<Label>("Title");
3523
WebView2 = this.FindControl<AvaloniaWebView2>("WebView2");
24+
WebView2Compat = this.FindControl<WebView2Compat>("WebView2Compat");
3625
Button = this.FindControl<Button>("Button");
3726

3827
Button.Click += Button_Click;
39-
Environment = CreationProperties.CreateEnvironmentAsync().GetAwaiter().GetResult();
28+
Environment = AvaloniaWebView2.DefaultCreationProperties!.CreateEnvironmentAsync().GetAwaiter().GetResult();
4029
Environment.ProcessInfosChanged += Environment_ProcessInfosChanged;
4130
SetTitle(Environment.BrowserVersionString);
4231
}
@@ -89,7 +78,7 @@ void Environment_ProcessInfosChanged(object? sender, object e)
8978
void Button_Click(object? sender, RoutedEventArgs e)
9079
{
9180
#if DEBUG
92-
WebView2.Test();
81+
(WebView2 ?? WebView2Compat?.WebView2)?.Test();
9382
#endif
9483
}
9584

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
3+
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
4+
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
5+
<security>
6+
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
7+
<!-- UAC 清单选项
8+
如果想要更改 Windows 用户帐户控制级别,请使用
9+
以下节点之一替换 requestedExecutionLevel 节点。
10+
11+
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
12+
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
13+
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />
14+
15+
指定 requestedExecutionLevel 元素将禁用文件和注册表虚拟化。
16+
如果你的应用程序需要此虚拟化来实现向后兼容性,则移除此
17+
元素。
18+
-->
19+
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
20+
</requestedPrivileges>
21+
</security>
22+
</trustInfo>
23+
24+
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
25+
<application>
26+
<!-- 设计此应用程序与其一起工作且已针对此应用程序进行测试的
27+
Windows 版本的列表。取消评论适当的元素,
28+
Windows 将自动选择最兼容的环境。 -->
29+
30+
<!-- Windows Vista -->
31+
<!--<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" />-->
32+
33+
<!-- Windows 7 -->
34+
<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />
35+
36+
<!-- Windows 8 -->
37+
<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />
38+
39+
<!-- Windows 8.1 -->
40+
<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />
41+
42+
<!-- Windows 10 -->
43+
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
44+
45+
</application>
46+
</compatibility>
47+
48+
<!-- 指示该应用程序可感知 DPI 且 Windows 在 DPI 较高时将不会对其进行
49+
自动缩放。Windows Presentation Foundation (WPF)应用程序自动感知 DPI,无需
50+
选择加入。选择加入此设置的 Windows 窗体应用程序(面向 .NET Framework 4.6)还应
51+
在其 app.config 中将 "EnableWindowsFormsHighDpiAutoResizing" 设置设置为 "true"。
52+
53+
将应用程序设为感知长路径。请参阅 https://docs.microsoft.com/windows/win32/fileio/maximum-file-path-limitation -->
54+
<!--
55+
<application xmlns="urn:schemas-microsoft-com:asm.v3">
56+
<windowsSettings>
57+
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
58+
<longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true</longPathAware>
59+
</windowsSettings>
60+
</application>
61+
-->
62+
63+
<!-- 启用 Windows 公共控件和对话框的主题(Windows XP 和更高版本) -->
64+
<!--
65+
<dependency>
66+
<dependentAssembly>
67+
<assemblyIdentity
68+
type="win32"
69+
name="Microsoft.Windows.Common-Controls"
70+
version="6.0.0.0"
71+
processorArchitecture="*"
72+
publicKeyToken="6595b64144ccf1df"
73+
language="*"
74+
/>
75+
</dependentAssembly>
76+
</dependency>
77+
-->
78+
79+
</assembly>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<UserControl
2+
x:Class="Avalonia.WebView2.Sample.WebView2Compat"
3+
xmlns="https://github.com/avaloniaui"
4+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
7+
xmlns:wv2="clr-namespace:Avalonia.Controls;assembly=Avalonia.WebView2"
8+
d:DesignHeight="450"
9+
d:DesignWidth="800"
10+
mc:Ignorable="d">
11+
<wv2:WebView2 x:Name="WebView2" Source="https://www.bing.com" />
12+
</UserControl>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Avalonia;
2+
using Avalonia.Controls;
3+
using Avalonia.Markup.Xaml;
4+
using AvaloniaWebView2 = Avalonia.Controls.WebView2;
5+
6+
namespace Avalonia.WebView2.Sample;
7+
8+
public partial class WebView2Compat : UserControl
9+
{
10+
public readonly AvaloniaWebView2 WebView2;
11+
12+
public WebView2Compat()
13+
{
14+
InitializeComponent();
15+
WebView2 = this.FindControl<AvaloniaWebView2>("WebView2");
16+
}
17+
18+
private void InitializeComponent()
19+
{
20+
AvaloniaXamlLoader.Load(this);
21+
}
22+
}

src/Avalonia.WebView2/Avalonia.WebView2.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
This package is necessary for Avalonia applications. To learn more about package versions checkout https://aka.ms/wv2-packageversion. To learn best practices checkout: https://aka.ms/wv2-bestpractices.
1111
</Description>
1212
<PackageTags>Web WebView Native native package Edge avalonia avaloniaui dotnet framework core Webview2</PackageTags>
13-
<Version>1.0.1264.42-preview.220710.1</Version>
13+
<Version>1.0.1264.42-preview.220710.2</Version>
1414
</PropertyGroup>
1515

1616
<PropertyGroup Condition=" '$(Configuration)'=='Release' ">

src/Avalonia.WebView2/Properties/ImplicitUsings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
global using Avalonia;
22
global using Avalonia.Controls;
3+
global using Avalonia.Controls.Platform;
34
global using Avalonia.Controls.ApplicationLifetimes;
45
global using Avalonia.VisualTree;
56
global using Avalonia.Platform;

src/Avalonia.WebView2/WebView2.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -878,10 +878,14 @@ void CoreWebView2Controller_ZoomFactorChanged(object? sender, object e)
878878
protected override IPlatformHandle CreateNativeControlCore(IPlatformHandle parent)
879879
{
880880
#if !DISABLE_WEBVIEW2_CORE
881+
#if !WINDOWS
881882
if (OperatingSystem.IsWindows())
883+
#endif
882884
{
883885
var hwnd = Window!.PlatformImpl.Handle.Handle;
884-
IntPtr windowExW = NativeMethods.CreateWindowExW(NativeMethods.WS_EX.TRANSPARENT, null, string.Empty, NativeMethods.WS.CLIPCHILDREN | NativeMethods.WS.VISIBLE | NativeMethods.WS.CHILD, 0, 0, 0, 0, hwnd, IntPtr.Zero, Marshal.GetHINSTANCE(typeof(NativeMethods).Module), IntPtr.Zero);
886+
IntPtr windowExW = NativeMethods.CreateWindowExW(NativeMethods.WS_EX.TRANSPARENT, "static", string.Empty, NativeMethods.WS.CLIPCHILDREN | NativeMethods.WS.VISIBLE | NativeMethods.WS.CHILD, 0, 0, 0, 0, hwnd, IntPtr.Zero, Marshal.GetHINSTANCE(typeof(NativeMethods).Module), IntPtr.Zero);
887+
var error = Marshal.GetLastWin32Error();
888+
if (error != 0) throw new Win32Exception(error);
885889
if (_coreWebView2Controller != null)
886890
ReparentController(windowExW);
887891
//if (!_hwndTaskSource.Task.IsCompleted)
@@ -901,14 +905,16 @@ protected override void DestroyNativeControlCore(IPlatformHandle control)
901905
{
902906
#if !DISABLE_WEBVIEW2_CORE
903907
PlatformHandle = null;
908+
#if !WINDOWS
904909
if (OperatingSystem.IsWindows())
910+
#endif
905911
{
906912
if (_coreWebView2Controller != null)
907913
ReparentController(IntPtr.Zero);
908914
NativeMethods.DestroyWindow(control.Handle);
909915
}
910916
#endif
911-
base.DestroyNativeControlCore(control);
917+
((INativeControlHostDestroyableControlHandle?)control)?.Destroy();
912918
}
913919

914920
#if !DISABLE_WEBVIEW2_CORE

0 commit comments

Comments
 (0)