Replace IsDesignModeEnabled check with original Context Type Check (#7143)
authorShane Neuville <shneuvil@microsoft.com>
Thu, 15 Aug 2019 22:23:12 +0000 (16:23 -0600)
committerGitHub <noreply@github.com>
Thu, 15 Aug 2019 22:23:12 +0000 (16:23 -0600)
* remove use of DesignMode just store check in static bool

* - fix set

* - fix last check

* - spelling fix

Xamarin.Forms.Platform.Android/ContextExtensions.cs

index 576c910..cc6b025 100644 (file)
@@ -115,7 +115,23 @@ namespace Xamarin.Forms.Platform.Android
                        return null;
                }
 
-               internal static bool IsDesignerContext(this Context context) => DesignMode.IsDesignModeEnabled;
+               static bool? _isDesignerContext;
+               internal static bool IsDesignerContext(this Context context)
+               {
+                       if (_isDesignerContext.HasValue)
+                               return _isDesignerContext.Value;
+
+                       if (context == null)
+                               _isDesignerContext = false;
+                       else if ($"{context}".Contains("com.android.layoutlib.bridge.android.BridgeContext"))
+                               _isDesignerContext = true;
+                       else if (context is ContextWrapper contextWrapper)
+                               return contextWrapper.BaseContext.IsDesignerContext();
+                       else
+                               _isDesignerContext = false;
+
+                       return _isDesignerContext.Value;
+               }
 
                public static AFragmentManager GetFragmentManager(this Context context)
                {