From b6f10eff046e5fce7b074350b4831317116b0ca8 Mon Sep 17 00:00:00 2001 From: Matt Soucoup Date: Fri, 26 Apr 2019 18:15:04 +0200 Subject: [PATCH] Add null check in iOS ShadowEffect OnDetached (#5952) Added a null check in iOS ShadowEffect OnDetached fixes #5951 --- .../Issue5951.cs | 92 +++++++++++++++++++ ...rin.Forms.Controls.Issues.Shared.projitems | 3 +- Xamarin.Forms.Platform.iOS/ShadowEffect.cs | 10 +- 3 files changed, 101 insertions(+), 4 deletions(-) create mode 100644 Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue5951.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 index 000000000..da4324f86 --- /dev/null +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue5951.cs @@ -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() + .SetIsShadowEnabled(true) + .SetShadowColor(Color.Black) + .SetShadowOffset(new Size(10, 10)) + .SetShadowOpacity(0.2); + + Content = new StackLayout + { + Children = { shadowButton } + }; + } + } + + +} diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems index 340c17bd0..2c6d2980e 100644 --- a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems @@ -915,6 +915,7 @@ Issue4356.xaml + @@ -1162,7 +1163,7 @@ Designer - MSBuild:Compile + MSBuild:UpdateDesignTimeXaml \ No newline at end of file diff --git a/Xamarin.Forms.Platform.iOS/ShadowEffect.cs b/Xamarin.Forms.Platform.iOS/ShadowEffect.cs index 73d7dde7e..18e2ced82 100644 --- a/Xamarin.Forms.Platform.iOS/ShadowEffect.cs +++ b/Xamarin.Forms.Platform.iOS/ShadowEffect.cs @@ -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;