Add null check in iOS ShadowEffect OnDetached (#5952)
authorMatt Soucoup <masoucou@microsoft.com>
Fri, 26 Apr 2019 16:15:04 +0000 (18:15 +0200)
committerSamantha Houts <samhouts@users.noreply.github.com>
Fri, 26 Apr 2019 16:15:04 +0000 (09:15 -0700)
Added a null check in iOS ShadowEffect OnDetached
fixes #5951

Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue5951.cs [new file with mode: 0644]
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems
Xamarin.Forms.Platform.iOS/ShadowEffect.cs

diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue5951.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue5951.cs
new file mode 100644 (file)
index 0000000..da4324f
--- /dev/null
@@ -0,0 +1,92 @@
+using System;
+using Xamarin.Forms.CustomAttributes;
+using Xamarin.Forms.Internals;
+using Xamarin.Forms.PlatformConfiguration;
+using Xamarin.Forms.PlatformConfiguration.iOSSpecific;
+
+#if UITEST
+using Xamarin.Forms.Core.UITests;
+using Xamarin.UITest;
+using NUnit.Framework;
+#endif
+
+namespace Xamarin.Forms.Controls.Issues
+{
+       [Preserve(AllMembers = true)]
+       [Issue(IssueTracker.Github, 5951, "App Crashes On Shadow Effect's OnDetached On Button That's Never Visible", PlatformAffected.iOS)]
+       public class Issue5951 : TestContentPage
+       {
+               protected override void Init()
+               {
+                       var instructionsLabel = new Label { Text = "Press the push page button. If everything works, you'll see the word success below.", FontSize = 16 };
+
+                       var resultsLabel = new Label { Text = string.Empty, FontSize = 16 };
+
+                       var pushButton = new Button
+                       {
+                               Text = "Push page",
+                               Command = new Command(async () =>
+                               {
+                                       var shadowPage = new PageWithShadowButton();
+
+                                       await Navigation.PushAsync(shadowPage);
+
+                                       try
+                                       {
+                                               await shadowPage.Navigation.PopAsync();
+
+                                               resultsLabel.Text = "Success";
+                                       }
+                                       catch (NullReferenceException)
+                                       {
+                                               resultsLabel.Text = "Error";
+                                       }
+                               })
+                       };
+
+                       Content = new StackLayout
+                       {
+                               Children = {
+                                       instructionsLabel,
+                                       resultsLabel,
+                                       pushButton
+                               }
+                       };
+
+
+               }
+
+#if UITEST
+               [Test]
+               public void Issue5951Test()
+               {
+                       RunningApp.Tap(q => q.Marked("Push page"));
+                       RunningApp.WaitForElement(q => q.Marked("Push page"));
+
+                       RunningApp.WaitForElement(q => q.Marked("Success"));
+               }
+#endif
+       }
+
+       [Preserve(AllMembers = true)]
+       public class PageWithShadowButton : ContentPage
+       {
+               public PageWithShadowButton()
+               {
+                       var shadowButton = new Button { Text = "Never Visible", IsVisible = false };
+
+                       shadowButton.On<iOS>()
+                               .SetIsShadowEnabled(true)
+                               .SetShadowColor(Color.Black)
+                               .SetShadowOffset(new Size(10, 10))
+                               .SetShadowOpacity(0.2);
+
+                       Content = new StackLayout
+                       {
+                               Children = { shadowButton }
+                       };
+               }
+       }
+
+
+}
index 340c17b..2c6d298 100644 (file)
     <Compile Include="$(MSBuildThisFileDirectory)Issue4356.cs">
       <DependentUpon>Issue4356.xaml</DependentUpon>
     </Compile>
+    <Compile Include="$(MSBuildThisFileDirectory)Issue5951.cs" />
   </ItemGroup>
   <ItemGroup>
     <EmbeddedResource Include="$(MSBuildThisFileDirectory)Bugzilla22229.xaml">
   <ItemGroup>
     <EmbeddedResource Include="$(MSBuildThisFileDirectory)A11yTabIndex.xaml">
       <SubType>Designer</SubType>
-      <Generator>MSBuild:Compile</Generator>
+      <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
     </EmbeddedResource>
   </ItemGroup>
 </Project>
\ No newline at end of file
index 73d7dde..18e2ced 100644 (file)
@@ -15,8 +15,12 @@ namespace Xamarin.Forms.Platform.iOS
                protected override void OnDetached()
                {
                        var layer = ShadowView.Layer;
-                       layer.ShadowColor = Color.Transparent.ToCGColor();
-                       layer.ShadowOpacity = 0;
+
+                       if (layer != null)
+                       {
+                               layer.ShadowColor = Color.Transparent.ToCGColor();
+                               layer.ShadowOpacity = 0;
+                       }
                }
 
                protected override void OnElementPropertyChanged(PropertyChangedEventArgs args)
@@ -31,7 +35,7 @@ namespace Xamarin.Forms.Platform.iOS
                        }
                }
 
-               private void UpdateShadow ()
+               private void UpdateShadow()
                {
                        var layer = ShadowView.Layer;