From d780d1167983749330ecb6ddc43a84d869626cc4 Mon Sep 17 00:00:00 2001 From: SunnyDesignor <50539661+SunnyDesignor@users.noreply.github.com> Date: Mon, 16 Feb 2026 17:18:07 +0800 Subject: [PATCH] Fix unexpected crashes during accessibility actions. (#20687) --- .../Avalonia.Android/AvaloniaAccessHelper.cs | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/Android/Avalonia.Android/AvaloniaAccessHelper.cs b/src/Android/Avalonia.Android/AvaloniaAccessHelper.cs index 0b5a7e03c4..c034c4ec5b 100644 --- a/src/Android/Avalonia.Android/AvaloniaAccessHelper.cs +++ b/src/Android/Avalonia.Android/AvaloniaAccessHelper.cs @@ -147,13 +147,33 @@ namespace Avalonia.Android protected override bool OnPerformActionForVirtualView(int virtualViewId, int action, Bundle? arguments) { return (GetNodeInfoProvidersFromVirtualViewId(virtualViewId) ?? []) - .Select(x => x.PerformNodeAction(action, arguments)) + .Select(x => TryPerformNodeAction(x, action, arguments)) .Aggregate(false, (a, b) => a | b); } - protected override void OnPopulateNodeForVirtualView(int virtualViewId, AccessibilityNodeInfoCompat nodeInfo) + private static bool TryPerformNodeAction(INodeInfoProvider nodeInfoProvider, int action, Bundle? arguments) { - if (!_peers.TryGetValue(virtualViewId, out AutomationPeer? peer)) + try + { + return nodeInfoProvider.PerformNodeAction(action, arguments); + } + catch (ElementNotEnabledException) + { + return false; + } + catch (InvalidOperationException) + { + return false; + } + catch (NotSupportedException) + { + return false; + } + } + + protected override void OnPopulateNodeForVirtualView(int virtualViewId, AccessibilityNodeInfoCompat? nodeInfo) + { + if (nodeInfo is null || !_peers.TryGetValue(virtualViewId, out AutomationPeer? peer)) { return; // BAIL!! No work to be done }