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] IsStringNullOrEmpty Converters

See original GitHub issue

IsStringNullOrEmptyConverter

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

Summary

The IsStringNullOrEmptyConverter 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.IsNullOrEmpty.

The IsStringNotNullOrEmptyConverter 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.IsNullOrEmpty.

Detailed Design

IsStringNullOrEmptyConverter.shared.cs

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

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

IsStringNotNullOrEmptyConverter.shared.cs

public class IsStringNullOrEmptyConverter : ValueConverterExtension, ICommunityToolkitValueConverter
{
  public object Convert(object? value, Type? targetType, object? parameter, CultureInfo? culture) => value is string stringValue ? !string.IsNullOrEmpty(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: IsStringNullOrEmptyConverter x:Key="IsStringNullOrEmptyConverter" />
        </ResourceDictionary>
    </ContentPage.Resources>

    <StackLayout>

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

    </StackLayout>
</ContentPage>

C# Usage

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

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
juansturlacommented, Oct 16, 2021

Well it looks like you already did that before being assigned hahaha

0reactions
msftbot[bot]commented, Feb 11, 2022

Reopening Proposal.

Only Proposals moved to the Closed Project Column and Completed Project Column can be closed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

is-string-null-or-empty-converter.md
The IsStringNullOrEmptyConverter is a one way converter that returns a bool indicating whether the binding value is null or string.Empty.
Read more >
Why do I get null instead of empty string when receiving ...
You can use the DisplayFormat attribute on the property of your model class: [DisplayFormat(ConvertEmptyStringToNull = false)].
Read more >
IsStringNullOrWhiteSpaceConve...
The IsStringNullOrWhiteSpaceConverter is a one way converter that returns a bool indicating whether the binding value is null or string.
Read more >
Bluetooth RFBUS from TOSHIBA_2.1.1127.0_Soft32.exe
Submit malware for free analysis with Falcon Sandbox and Hybrid Analysis technology. Hybrid Analysis develops and licenses analysis tools to fight malware.
Read more >
Automated Malware Analysis Report for http://jino.ru
System is w10x64 ... @copyright Copyright (c) 2014 Yehuda Katz, Tom Dale, Stefan Penner and contributors (Conversion to ES6 API by Jake Archibald)....
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