From c33d41925a53203f59671ba7ceb9bbeb8ccdbded Mon Sep 17 00:00:00 2001 From: Dub1shu Date: Fri, 5 Jan 2024 20:34:52 +0900 Subject: [PATCH 1/2] Fix crash on setting string.Empty in SettingsCard --- .../src/SettingsCard/SettingsCard.cs | 31 ++++++++++++++----- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/components/SettingsControls/src/SettingsCard/SettingsCard.cs b/components/SettingsControls/src/SettingsCard/SettingsCard.cs index ba645331..cd691bb4 100644 --- a/components/SettingsControls/src/SettingsCard/SettingsCard.cs +++ b/components/SettingsControls/src/SettingsCard/SettingsCard.cs @@ -247,20 +247,20 @@ private void OnDescriptionChanged() { if (GetTemplateChild(DescriptionPresenter) is FrameworkElement descriptionPresenter) { - descriptionPresenter.Visibility = Description != null - ? Visibility.Visible - : Visibility.Collapsed; + descriptionPresenter.Visibility = IsNullOrEmptyString(Description) + ? Visibility.Collapsed + : Visibility.Visible; } - + } private void OnHeaderChanged() { if (GetTemplateChild(HeaderPresenter) is FrameworkElement headerPresenter) { - headerPresenter.Visibility = Header != null - ? Visibility.Visible - : Visibility.Collapsed; + headerPresenter.Visibility = IsNullOrEmptyString(Header) + ? Visibility.Collapsed + : Visibility.Visible; } } @@ -274,7 +274,7 @@ private void CheckVerticalSpacingState(VisualState s) { // On state change, checking if the Content should be wrapped (e.g. when the card is made smaller or the ContentAlignment is set to Vertical). If the Content and the Header or Description are not null, we add spacing between the Content and the Header/Description. - if (s != null && (s.Name == RightWrappedState || s.Name == RightWrappedNoIconState || s.Name == VerticalState) && (Content != null) && (Header != null || Description != null)) + if (s != null && (s.Name == RightWrappedState || s.Name == RightWrappedNoIconState || s.Name == VerticalState) && (Content != null) && (!IsNullOrEmptyString(Header) || !IsNullOrEmptyString(Description))) { VisualStateManager.GoToState(this, ContentSpacingState, true); } @@ -295,4 +295,19 @@ private void CheckVerticalSpacingState(VisualState s) return FocusManager.GetFocusedElement() as FrameworkElement; } } + + private static bool IsNullOrEmptyString(object obj) + { + if (obj == null) + { + return true; + } + + if (obj is string objString && objString == string.Empty) + { + return true; + } + + return false; + } } From f9f624e9dd83ec695df1aff9bb182224dccd1643 Mon Sep 17 00:00:00 2001 From: Arlo Godfrey Date: Mon, 8 Jan 2024 20:02:21 -0600 Subject: [PATCH 2/2] Added tests for #310 --- .../SettingsControls/tests/Test_SettingsCard.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/components/SettingsControls/tests/Test_SettingsCard.cs b/components/SettingsControls/tests/Test_SettingsCard.cs index f4928c53..3ba535b6 100644 --- a/components/SettingsControls/tests/Test_SettingsCard.cs +++ b/components/SettingsControls/tests/Test_SettingsCard.cs @@ -11,6 +11,20 @@ namespace SettingsControlsExperiment.Tests; [TestClass] public partial class SettingsCardTestClass : VisualUITestBase { + [UIThreadTestMethod] + public void EmptyNameTest(SettingsCard card) + { + // See https://github.com/CommunityToolkit/Windows/issues/310#issue-2066181868 + card.Name = string.Empty; + } + + [UIThreadTestMethod] + public void EmptyDescriptionTest(SettingsCard card) + { + // See https://github.com/CommunityToolkit/Windows/issues/310#issue-2066181868 + card.Description = string.Empty; + } + // If you don't need access to UI objects directly or async code, use this pattern. [TestMethod] public void SimpleSynchronousExampleTest()