avalonia-mobile
Use when building or fixing the Android or iOS (and Mac Catalyst) app side of an Avalonia app — project heads and startup per version (AvaloniaAndroidApplication, AvaloniaMainActivity, AvaloniaAppDelegate, IActivityApplicationLifetime vs ISingleViewApplicationLifetime), safe areas/edge-to-edge (IInsetsManager), soft keyboard (IInputPane), back button, orientation, app activation/background, haptics (PlatformFeedback), touch behavior, and mobile storage/launcher. For packaging/signing see avalonia-deployment.
linuxdevel · 已有静态检查记录
安装与使用
运行命令前先阅读来源与依赖说明。复制命令不会自动安装或执行。
npx skills add linuxdevel/Avalonia-skills --skill avalonia-mobile技能说明
name: avalonia-mobile description: Use when building or fixing the Android or iOS (and Mac Catalyst) app side of an Avalonia app — project heads and startup per version (AvaloniaAndroidApplication, AvaloniaMainActivity, AvaloniaAppDelegate, IActivityApplicationLifetime vs ISingleViewApplicationLifetime), safe areas/edge-to-edge (IInsetsManager), soft keyboard (IInputPane), back button, orientation, app activation/background, haptics (PlatformFeedback), touch behavior, and mobile storage/launcher. For packaging/signing see avalonia-deployment.
Avalonia Mobile (Android & iOS)
App-level concerns only. Packaging, signing, APK/IPA publishing: see avalonia-deployment. Version labels: see avalonia-versions.
Requirements
| 11.x | 12.0 / 12.1 | |
|---|---|---|
| TFM | net*-android / net*-ios (pre-net10) | net10.0-android / net10.0-ios (net10.0 required) |
| Mac Catalyst | no | (12.0+) net10.0-maccatalyst via Avalonia.iOS package |
| Packages | Avalonia.Android / Avalonia.iOS | same, version must match core Avalonia |
Standard solution layout (from avalonia-dotnet-templates xplat template): shared App project + *.Android head + *.iOS head (+ Desktop/Browser). Template defaults: Android SupportedOSPlatformVersion 23, iOS 13.0, AndroidX SplashScreen package referenced.
Android startup
(12.0+) Application class + activity
// Application.cs — initialization moved here from the activity
[Application]
public class AndroidApp : AvaloniaAndroidApplication<App>
{
public AndroidApp(IntPtr javaReference, JniHandleOwnership transfer)
: base(javaReference, transfer) { }
protected override AppBuilder CustomizeAppBuilder(AppBuilder builder)
=> base.CustomizeAppBuilder(builder).WithInterFont();
}
// MainActivity.cs — non-generic
[Activity(
Label = "MyApp",
Theme = "@style/MyTheme.NoActionBar",
Icon = "@drawable/icon",
MainLauncher = true,
ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.ScreenSize | ConfigChanges.UiMode)]
public class MainActivity : AvaloniaMainActivity { }
CreateAppBuilder() / CustomizeAppBuilder() were removed from AvaloniaMainActivity — put builder customization in the AvaloniaAndroidApplication<TApp> subclass or App. (The IntPtr, JniHandleOwnership constructor is the standard .NET-Android requirement for Application subclasses; the template may omit/inline it — copy from the generated head.)
Type chain: AppCompatActivity -> AvaloniaActivity -> AvaloniaMainActivity.
(12.0+) App.axaml.cs — set the main view
public override void OnFrameworkInitializationCompleted()
{
switch (ApplicationLifetime)
{
case IClassicDesktopStyleApplicationLifetime desktop:
desktop.MainWindow = new MainWindow();
break;
case IActivityApplicationLifetime activity: // Android (12.0+)
activity.MainViewFactory = () => new MainView(); // Func<Control>
break;
case ISingleViewApplicationLifetime single: // iOS, Browser, embedded Linux
single.MainView = new MainView();
break;
}
base.OnFrameworkInitializationCompleted();
}
Android no longer uses ISingleViewApplicationLifetime; keep that branch for iOS/Browser. Use a factory (a new view per activity instance), not a shared instance.
(11.x) Generic activity + single view
[Activity(Label = "MyApp", MainLauncher = true, Theme = "@style/MyTheme.NoActionBar",
ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.ScreenSize | ConfigChanges.UiMode)]
public class MainActivity : AvaloniaMainActivity<App>
{
protected override AppBuilder CustomizeAppBuilder(AppBuilder builder)
=> base.CustomizeAppBuilder(builder).WithInterFont();
}
// App: case ISingleViewApplicationLifetime single: single.MainView = new MainView();
Multiple activities (12.0+)
Android may create several instances of the main activity in one process (notification tap, return from another app), and 12.0 lets later activities use your project's Application class. IActivityApplicationLifetime.MainViewFactory (Func<Control>) is called once per new activity instance, so each gets a fresh view with its own state. Never set ISingleViewApplicationLifetime.MainView on Android 12 — the Android lifetime implements both interfaces and wraps MainView in a factory returning the same instance, so a second activity shows a blank screen. The lifetime holds only the factory; keep your own reference if you need the current view. Extra (non-main) activities subclass AvaloniaActivity (AppCompatActivity -> AvaloniaActivity) and set its Content property. AvaloniaActivity also has ActivityResult, RequestPermissionsResult (both Action<...>), event BackRequested, and OnBackPressed() / OnBackInvoked().
Rendering mode
// in AvaloniaAndroidApplication<TApp>.CustomizeAppBuilder
.With(new AndroidPlatformOptions
{
RenderingMode = new[] { AndroidRenderingMode.Egl, AndroidRenderingMode.Software }
})
AndroidRenderingMode: Egl (default GPU), Vulkan, Software. First mode that initializes wins; empty list or all failing throws InvalidOperationException — keep Software as fallback.
iOS / Mac Catalyst startup
[Register("AppDelegate")]
public partial class AppDelegate : AvaloniaAppDelegate<App>
{
protected override AppBuilder CustomizeAppBuilder(AppBuilder builder)
=> base.CustomizeAppBuilder(builder)
.WithInterFont()
.With(new iOSPlatformOptions
{
RenderingMode = new[] { iOSRenderingMode.Metal, iOSRenderingMode.OpenGl },
EnableHighRefreshRate = false // opt out of high refresh rate
});
}
- iOS uses
ISingleViewApplicationLifetime(MainView) in all versions. - (12.0+) Scene-based lifecycle: a scene delegate i