Fix for Android Visibility/Opacity crash (#785)
authorE.Z. Hart <hartez@users.noreply.github.com>
Fri, 3 Mar 2017 11:28:44 +0000 (04:28 -0700)
committerRui Marinho <me@ruimarinho.net>
Fri, 3 Mar 2017 11:28:44 +0000 (11:28 +0000)
* Repro

* Fix for UI test

* Cache the Alpha value and queue up restoration after the visibility is changed

* Fix issue with negative height/width in UpdateLayout

* Clean up usings

Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla51238.cs [new file with mode: 0644]
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems
Xamarin.Forms.Platform.Android/VisualElementTracker.cs

diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla51238.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla51238.cs
new file mode 100644 (file)
index 0000000..4bce76a
--- /dev/null
@@ -0,0 +1,65 @@
+using Xamarin.Forms.CustomAttributes;
+using Xamarin.Forms.Internals;
+
+#if UITEST
+using NUnit.Framework;
+#endif
+
+namespace Xamarin.Forms.Controls.Issues
+{
+       [Preserve(AllMembers = true)]
+       [Issue(IssueTracker.Bugzilla, 51238,
+               "Transparent Grid causes Java.Lang.IllegalStateException: Unable to create layer for Platform_DefaultRenderer",
+               PlatformAffected.Android)]
+       public class Bugzilla51238 : TestContentPage
+       {
+#if UITEST
+               [Test]
+               public void Issue1Test()
+               {
+                       RunningApp.WaitForElement("Tap Me!");
+                       RunningApp.Tap("Tap Me!"); // Crashes the app if the issue isn't fixed
+                       RunningApp.WaitForElement("Tap Me!");
+               }
+#endif
+
+               protected override void Init()
+               {
+                       var grid = new Grid();
+                       grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Star });
+                       grid.RowDefinitions.Add(new RowDefinition { Height = 50 });
+
+                       var transparentLayer = new Grid();
+                       transparentLayer.IsVisible = false;
+                       transparentLayer.BackgroundColor = Color.Lime;
+                       transparentLayer.Opacity = 0.5;
+
+                       var label = new Label
+                       {
+                               Text = "Foo",
+                               HorizontalOptions = LayoutOptions.Center,
+                               VerticalOptions = LayoutOptions.Center
+                       };
+
+                       Grid.SetRow(label, 0);
+                       Grid.SetRow(transparentLayer, 0);
+
+                       var button = new Button
+                       {
+                               Text = "Tap Me!",
+                               HorizontalOptions = LayoutOptions.Center,
+                               VerticalOptions = LayoutOptions.Center
+                       };
+
+                       Grid.SetRow(button, 1);
+
+                       button.Clicked += (sender, args) => { transparentLayer.IsVisible = !transparentLayer.IsVisible; };
+
+                       grid.Children.Add(label);
+                       grid.Children.Add(transparentLayer);
+                       grid.Children.Add(button);
+
+                       Content = grid;
+               }
+       }
+}
\ No newline at end of file
index 26f5bcb..1093f1b 100644 (file)
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla47923.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla48236.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla47971.cs" />
+    <Compile Include="$(MSBuildThisFileDirectory)Bugzilla51238.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla51642.xaml.cs">
       <DependentUpon>Bugzilla51642.xaml</DependentUpon>
       <SubType>Code</SubType>
index 7ade459..34c362d 100644 (file)
@@ -79,8 +79,8 @@ namespace Xamarin.Forms.Platform.Android
 
                        var x = (int)_context.ToPixels(view.X);
                        var y = (int)_context.ToPixels(view.Y);
-                       var width = (int)_context.ToPixels(view.Width);
-                       var height = (int)_context.ToPixels(view.Height);
+                       var width = Math.Max(0, (int)_context.ToPixels(view.Width));
+                       var height = Math.Max(0, (int)_context.ToPixels(view.Height));
 
                        var formsViewGroup = aview as FormsViewGroup;
                        if (formsViewGroup == null)