question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Proposal] IsStringNullOrWhiteSpace Converter

See original GitHub issue

IsStringNullOrWhiteSpace Converters

  • Proposed
  • Prototype
  • Implementation
    • iOS Support
    • Android Support
    • macOS Support
    • Windows Support
  • Unit Tests
  • Sample
  • Documentation

Summary

The IsStringNullOrWhiteSpaceConverter is a converter that allows users to convert an incoming string binding to a bool value. This value represents if the incoming binding value is null or empty using string.IsNullOrWhiteSpace.

The IsStringNotNullOrWhiteSpaceConverter is a converter that allows users to convert an incoming string binding to a bool value. This value represents if the incoming binding value is null or empty using string.IsNullOrWhiteSpace .

Detailed Design

IsStringNullOrWhiteSpaceConverter.shared.cs

public class IsStringNullOrWhiteSpaceConverter : ValueConverterExtension, ICommunityToolkitValueConverter
{
  public object Convert(object? value, Type? targetType, object? parameter, CultureInfo? culture) => value is string stringValue ? string.IsNullOrWhiteSpace(stringValue) : throw new InvalidArgumentException("Binding must be of type string");

  public object? ConvertBack(object? value, Type? targetType, object? parameter, CultureInfo? culture) => throw new NotImplementedException();
}

IsStringNotNullOrWhiteSpaceConverter.shared.cs

public class IsStringNotNullOrWhiteSpaceConverter : ValueConverterExtension, ICommunityToolkitValueConverter
{
  public object Convert(object? value, Type? targetType, object? parameter, CultureInfo? culture) => value is string stringValue ? !string.IsNullOrWhiteSpace(stringValue) : throw new InvalidArgumentException("Binding must be of type string");

  public object? ConvertBack(object? value, Type? targetType, object? parameter, CultureInfo? culture) => throw new NotImplementedException();
}

Usage Syntax

XAML Usage

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
             x:Class="MyLittleApp.MainPage">

    <ContentPage.Resources>
        <ResourceDictionary>
            <xct: IsStringNullOrWhiteSpaceConverter x:Key="IsStringNullOrWhiteSpaceConverter" />
        </ResourceDictionary>
    </ContentPage.Resources>

    <StackLayout>

        <Label IsVisible="{Binding MyStringValue, Converter={StaticResource IsStringNullOrWhiteSpaceConverter}}" />

    </StackLayout>
</ContentPage>

C# Usage

class MyPage : ContentPage
{
  public MyPage()
  {
    Content = new StackLayout
    {
      Children = 
      {
          new Label().Bind(Label.IsVisibleProperty, nameof(ViewModel.MyString), converter: new IsStringNullOrWhiteSpaceConverter()),
      };
    }
  }
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
brminnickcommented, Feb 14, 2022

I approve to move forward with this feature ✅

1reaction
pictoscommented, Feb 14, 2022

I approve this feature

Read more comments on GitHub >

github_iconTop Results From Across the Web

IsStringNullOrWhiteSpaceConve...
The IsStringNullOrWhiteSpaceConverter is a one way converter that returns a bool indicating whether the binding value is null or string.
Read more >
[Bug] IsNullOrEmptyConverter checks with string. ...
I'm using the IsNullOrEmptyConverter in my project to show a UI element when an input string is null or empty, but when I...
Read more >
How can I determine if a String is non-null and not only ...
myString?.trim() returns a string (or null or blank) instead of the question's which returns a boolean. I guess it depends how you're using...
Read more >
Checking if a string is null
There is a IsNullOrWhiteSpace method when it's telling you if there is any character in the variable which is not space, tab, enter...
Read more >
Convert Empty String Columns to NULL for Many Colu...
I have many fields that I need to convert to NULLs if they are empty strings. Is there an easier and more time-efficient...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found