From b55eb0f5ce58b90bcce38c98b98071ec8a5b77ed Mon Sep 17 00:00:00 2001 From: Simon Rozsival Date: Fri, 29 May 2026 14:13:58 +0200 Subject: [PATCH] Fix LayoutInflater.From for remapped inflater types Replace the generated LayoutInflater.from(Context) binding with a handwritten implementation that delegates to FromContext. FromContext now wraps the live layout_inflater service handle when remapping prevents a direct managed cast, preserving Intune-style LayoutInflater remaps without returning null. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../Android.Views/LayoutInflater.cs | 21 ++++++++++++++++--- src/Mono.Android/metadata | 1 + .../Android.Views/LayoutInflaterTest.cs | 3 +++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/Mono.Android/Android.Views/LayoutInflater.cs b/src/Mono.Android/Android.Views/LayoutInflater.cs index bbf39484f97..2210a95c808 100644 --- a/src/Mono.Android/Android.Views/LayoutInflater.cs +++ b/src/Mono.Android/Android.Views/LayoutInflater.cs @@ -1,16 +1,31 @@ using System; using Android.Content; +using Android.Runtime; namespace Android.Views { public partial class LayoutInflater { + [Register ("from", "(Landroid/content/Context;)Landroid/view/LayoutInflater;", "")] + public static LayoutInflater? From (Context? context) + { + ArgumentNullException.ThrowIfNull (context); + + return FromContext (context); + } + public static LayoutInflater? FromContext (Context context) { - return context.GetSystemService (Context.LayoutInflaterService!) as LayoutInflater; + var service = context.GetSystemService (Context.LayoutInflaterService!); + + if (service is LayoutInflater inflater) + return inflater; + + if (service?.Handle != IntPtr.Zero) + return Java.Lang.Object.GetObject (service.Handle, JniHandleOwnership.DoNotTransfer); + + return null; } } } - - diff --git a/src/Mono.Android/metadata b/src/Mono.Android/metadata index 9aba17d02a9..d70782a0cec 100644 --- a/src/Mono.Android/metadata +++ b/src/Mono.Android/metadata @@ -18,6 +18,7 @@ + public diff --git a/tests/Mono.Android-Tests/Mono.Android-Tests/Android.Views/LayoutInflaterTest.cs b/tests/Mono.Android-Tests/Mono.Android-Tests/Android.Views/LayoutInflaterTest.cs index 20b1f72f8be..8f5a37aa328 100644 --- a/tests/Mono.Android-Tests/Mono.Android-Tests/Android.Views/LayoutInflaterTest.cs +++ b/tests/Mono.Android-Tests/Mono.Android-Tests/Android.Views/LayoutInflaterTest.cs @@ -19,5 +19,8 @@ public void From () // Remapped to "net/dot/android/test/MyLayoutInflater" var from = LayoutInflater.From (Application.Context); Assert.IsNotNull (from); + + var fromContext = LayoutInflater.FromContext (Application.Context); + Assert.IsNotNull (fromContext); } }