Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing crash when setting empty string for SettingsCard's Header and Description #312

Merged
merged 2 commits into from
Jan 9, 2024

Conversation

Dub1shu
Copy link
Contributor

@Dub1shu Dub1shu commented Jan 5, 2024

Fixes #310

There was a crash when binding an empty string to Header or Description while the content of SettingsCard was set. This pull request fixes that issue.

PR Type

Bugfix

What is the current behavior?

When the content of SettingsCard is set, binding an empty string to Header or Description causes a crash.

What is the new behavior?

When the content of SettingsCard is set, binding an empty string to Header or Description will no longer result in a crash.

PR Checklist

Please check if your PR fulfills the following requirements:

  • Created a feature/dev branch in your fork (vs. submitting directly from a commit on main)
  • Based off latest main branch of toolkit
  • Tested code with current supported SDKs
  • New component
    • Documentation has been added
    • Sample in sample app has been added
    • Analyzers are passing for documentation and samples
    • Icon has been created (if new sample) following the Thumbnail Style Guide and templates
  • Tests for the changes have been added (if applicable)
  • Header has been added to all new source files
  • Contains NO breaking changes

Other information

I would like to explain the fixes in this pull request using XAML simplified for the SettingsCard:

  • When initializing the component with the Content property (here, Header) of the ContentPresenter for the Header set to null, a crash occurs due to the ContentPresenter for the Content.
  • If the Visibility of the ContentPresenter for the Header is Collapsed during this initialization, it avoids the crash.
  • In SettingsCard.cs, if Header is null, it doesn't crash because the OnHeaderChanged function sets the Visibility to Collapsed.
  • The problem arises when an empty string is set as the Content in the ContentPresenter; it is treated equivalently to null, but the OnHeaderChanged function does not handle empty string.
  • In this pull request, fixes are implemented to check for empty strings within the OnHeaderChanged and OnDescriptionChanged functions.
<Style TargetType="local:Temp">
    <Style.Setters>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:Temp">
                    <StackPanel>
                        <ContentPresenter x:Name="PART_HeaderPresenter"
                                          Content="{TemplateBinding Header}"/>
                        <ContentPresenter Content="{TemplateBinding Content}"/>
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style.Setters>
</Style>

@Dub1shu
Copy link
Contributor Author

Dub1shu commented Jan 5, 2024

@dotnet-policy-service agree

Copy link
Collaborator

@niels9001 niels9001 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Dub1shu Thanks, changes look good!

@Jay-o-Way
Copy link

Jay-o-Way commented Jan 8, 2024

Why create a custom function? Can't use CS IsNullOrEmpty()?
https://learn.microsoft.com/en-us/dotnet/api/system.string.isnullorempty?view=net-8.0

@niels9001
Copy link
Collaborator

Why create a custom function? Can't use CS IsNullOrEmpty()?

@Dub1shu I am assuming because of Header and Description are of type object, not string. And IsNullOrEmpty() is a string method.

@Dub1shu
Copy link
Contributor Author

Dub1shu commented Jan 8, 2024

@Jay-o-Way
@niels9001
Thank you for your review.
As @niels9001 said, the Header and Description properties are type object, so I created the custom function.

@Jay-o-Way
Copy link

Alrighty. Thought it would be string but I see 🙂

Copy link
Member

@Arlodotexe Arlodotexe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added tests and confirmed that these changes fix the issue. Looks good, thanks!

@Arlodotexe Arlodotexe merged commit eb98b48 into CommunityToolkit:main Jan 9, 2024
9 checks passed
@Dub1shu
Copy link
Contributor Author

Dub1shu commented Jan 11, 2024

Thank you for reviewing and adding the test 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Binding the Description property on a SettingsCard to string.Empty causes a crash
4 participants