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); } }