Skip to content

Commit b7e188a

Browse files
committed
🤖 Android
1 parent 130a126 commit b7e188a

37 files changed

+1296
-588
lines changed

Avalonia.WebView2.slnx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<File Path="README.md" />
1111
<File Path="src/.editorconfig" />
1212
<File Path="src/Directory.Build.props" />
13+
<File Path="src/Directory.Build.targets" />
1314
<File Path="src/Directory.Packages.props" />
1415
<File Path="src/GeneratePackage.props" />
1516
<File Path="src/Version.props" />

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@ Microsoft Edge WebView2 控件允许在本机应用中嵌入 web 技术(HTML、C
1717

1818
## Screenshots
1919
![Win11 Sample Screenshot](https://raw.githubusercontent.com/BeyondDimension/Avalonia.WebView2/main/res/screenshots/Avalonia.WebView2.Sample.Win11.webp)
20-
![Win7 Sample Screenshot](https://raw.githubusercontent.com/BeyondDimension/Avalonia.WebView2/main/res/screenshots/Avalonia.WebView2.Sample.Win7.webp)
20+
![Win7 Sample Screenshot](https://raw.githubusercontent.com/BeyondDimension/Avalonia.WebView2/main/res/screenshots/Avalonia.WebView2.Sample.Win7.webp)
21+
![Android Sample Screenshot](https://raw.githubusercontent.com/BeyondDimension/Avalonia.WebView2/main/res/screenshots/Avalonia.WebView2.Sample.Mobile.Android.webp)
22+
![Android Storage Sample Screenshot](https://raw.githubusercontent.com/BeyondDimension/Avalonia.WebView2/main/res/screenshots/Avalonia.WebView2.Sample.Mobile.Android-Storage.webp)
77.2 KB
Loading
175 KB
Loading

src/Avalonia.WebView2.Sample.Mobile.Android/Avalonia.WebView2.Sample.Mobile.Android.csproj

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
@@ -9,6 +9,15 @@
99
<AndroidPackageFormat>apk</AndroidPackageFormat>
1010
<AndroidEnableProfiledAot>false</AndroidEnableProfiledAot>
1111
<RootNamespace>Avalonia.WebView2.Sample</RootNamespace>
12+
<IsAotCompatible>true</IsAotCompatible>
13+
</PropertyGroup>
14+
15+
<!--<PropertyGroup Condition="'$(Configuration)'=='Debug'">
16+
<EmbedAssembliesIntoApk>True</EmbedAssembliesIntoApk>
17+
</PropertyGroup>-->
18+
19+
<PropertyGroup Condition="'$(Configuration)'=='Release'">
20+
<AndroidCreatePackagePerAbi>true</AndroidCreatePackagePerAbi>
1221
</PropertyGroup>
1322

1423
<ItemGroup>

src/Avalonia.WebView2.Sample.Mobile.Android/MainActivity.cs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,13 @@ namespace Avalonia.WebView2.Sample;
2020
RoundIcon = "@mipmap/ic_launcher_round",
2121
MainLauncher = true,
2222
ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.ScreenSize | ConfigChanges.UiMode)]
23-
public sealed class MainActivity : AvaloniaMainActivity<App>
23+
sealed class MainActivity : AvaloniaMainActivity<App>
2424
{
25+
/// <summary>
26+
/// 获取状态栏的高度
27+
/// </summary>
28+
/// <param name="ctx"></param>
29+
/// <returns></returns>
2530
static int GetStatusBarHeight(Context ctx)
2631
{
2732
var resId = ctx.Resources!.GetIdentifier("status_bar_height", "dimen", "android");
@@ -33,17 +38,33 @@ static int GetStatusBarHeight(Context ctx)
3338
return default;
3439
}
3540

41+
/// <summary>
42+
/// 获取 DPI 缩放比例
43+
/// </summary>
44+
/// <param name="ctx"></param>
45+
/// <returns></returns>
46+
static double GetScaling(Context ctx)
47+
{
48+
// https://github.com/AvaloniaUI/Avalonia/blob/a8fb53e92ae31eced207c56d4d2662467afa7d9e/src/Android/Avalonia.Android/Platform/AndroidScreens.cs#L41-L44
49+
if (ctx.Resources?.Configuration is { } config)
50+
{
51+
return config.DensityDpi / (double)global::Android.Util.DisplayMetricsDensity.Default;
52+
}
53+
return 1D;
54+
}
55+
3656
protected override void OnCreate(Bundle? savedInstanceState)
3757
{
38-
MainViewModel.StatusBarMarginTop = GetStatusBarHeight(this);
58+
var statusBarHeight = GetStatusBarHeight(this);
59+
var scaling = GetScaling(this);
60+
MainViewModel.SetStatusBarMargin(top: statusBarHeight, scaling: scaling);
3961

4062
base.OnCreate(savedInstanceState);
4163
}
4264

4365
protected override AppBuilder CustomizeAppBuilder(AppBuilder builder)
4466
{
45-
return base.CustomizeAppBuilder(builder)
46-
.WithInterFont()
67+
return App.BuildAvaloniaApp(base.CustomizeAppBuilder(builder))
4768
.UseReactiveUI();
4869
}
4970
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Android.Runtime;
2+
using Android.Webkit;
3+
using WV2 = global::Avalonia.Controls.WebView2;
4+
5+
namespace Avalonia.WebView2.Sample;
6+
7+
[Application(
8+
Debuggable =
9+
#if DEBUG
10+
true,
11+
#else
12+
false,
13+
#endif
14+
UsesCleartextTraffic = WV2.UsesCleartextTraffic)]
15+
sealed class MainApplication : global::Android.App.Application
16+
{
17+
internal MainApplication(nint javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
18+
{
19+
}
20+
21+
public override void OnCreate()
22+
{
23+
base.OnCreate();
24+
25+
// 开启远程调试 WebView https://developer.chrome.google.cn/docs/devtools/remote-debugging/webviews
26+
WebView.SetWebContentsDebuggingEnabled(true);
27+
}
28+
}

src/Avalonia.WebView2.Sample.Mobile.Android/Resources/drawable/ic_launcher_background.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
android:viewportWidth="108"
66
android:viewportHeight="108">
77
<path
8-
android:fillColor="#3DDC84"
8+
android:fillColor="#512bd4"
99
android:pathData="M0,0h108v108h-108z" />
1010
<path
1111
android:fillColor="#00000000"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<resources>
2-
<string name="app_name">WebView2.Sample</string>
2+
<string name="app_name">AvaWV2(Sample)</string>
33
</resources>

src/Avalonia.WebView2.Sample.Mobile.iOS/AppDelegate.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ public partial class AppDelegate : AvaloniaAppDelegate<App>
1818
{
1919
protected override AppBuilder CustomizeAppBuilder(AppBuilder builder)
2020
{
21-
return base.CustomizeAppBuilder(builder)
22-
.WithInterFont()
21+
return App.BuildAvaloniaApp(base.CustomizeAppBuilder(builder))
2322
.UseReactiveUI();
2423
}
2524
}

0 commit comments

Comments
 (0)