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

[Proposal] IsNull / IsNotNull Converters #306

Closed
10 tasks done
IeuanWalker opened this issue Feb 22, 2022 · 17 comments · Fixed by #366
Closed
10 tasks done

[Proposal] IsNull / IsNotNull Converters #306

IeuanWalker opened this issue Feb 22, 2022 · 17 comments · Fixed by #366
Assignees
Labels
approved This Proposal has been approved and is ready to be added to the Toolkit champion A member of the .NET MAUI Toolkit core team has chosen to champion this feature documentation approved proposal A fully fleshed out proposal describing a new feature in syntactic and semantic detail

Comments

@IeuanWalker
Copy link
Contributor

IeuanWalker commented Feb 22, 2022

IsNull IsNotNull Converters

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

Link to Discussion

Discussion

Summary

The IsNullConverter is a converter that allows users to convert an incoming binding to a bool value. This value represents if the incoming binding value is null.

The IsNotNullConverter is a converter that allows users to convert an incoming binding to a bool value. This value represents if the incoming binding value is not null.

Motivation

As mentioned in the discussion this was the behaviour of another converter, so these converters would just reimplement the old behaviour. It's been quite common in the projects I've worked on to be able to check any object type if it's null or not.

Detailed Design

IsNotNullConverter.shared.cs

/// <summary>
/// Converts the incoming value to a <see cref="bool"/> indicating whether or not the value is not null.
/// </summary>
public class IsNotNullConverter : BaseConverter<object?, bool>
{
	/// <summary>
	/// Converts the incoming object to a <see cref="bool"/> indicating whether or not the value is not null.
	/// </summary>
	/// <param name="value">The value to convert.</param>
	/// <returns>A <see cref="bool"/> indicating if the incoming value is not null</returns>
	public bool ConvertFrom(object? value) =>  value is not null;
}

IsNullConverter.shared.cs

/// <summary>
/// Converts the incoming value to a <see cref="bool"/> indicating whether or not the value is null.
/// </summary>
public class IsNullConverter : BaseConverter<object?, bool>
{
	/// <summary>
	/// Converts the incoming object to a <see cref="bool"/> indicating whether or not the value is not null.
	/// </summary>
	/// <param name="value">The value to convert.</param>
	/// <returns>A <see cref="bool"/> indicating if the incoming value is not null</returns>
	public bool ConvertFrom(object? value) =>  value is null;
}

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: IsNullConverter x:Key="IsNullConverter" />
        </ResourceDictionary>
    </ContentPage.Resources>

    <StackLayout IsVisible="{Binding MyObject, Converter={StaticResource IsNullConverter}}">

    </StackLayout>
</ContentPage>

C# Usage

class MyPage : ContentPage
{
  public MyPage()
  {
    Content = new StackLayout().Bind(StackLayout.IsVisibleProperty, nameof(ViewModel.MyObject), converter: new IsNullConverter())
  }
}

Drawbacks

None

Alternatives

None

@IeuanWalker IeuanWalker added new proposal A fully fleshed out proposal describing a new feature in syntactic and semantic detail labels Feb 22, 2022
@IeuanWalker
Copy link
Contributor Author

If approved, I'm happy to put a PR together for it

@brminnick
Copy link
Collaborator

Thanks @IeuanWalker! This all looks good!

The only thing I'd tweak is to use is null and is not null just to match our Contribution Guidelines.

I'll raise this for a vote/approval at our next standup, 03 March.

@brminnick brminnick self-assigned this Feb 22, 2022
@ghost ghost added champion A member of the .NET MAUI Toolkit core team has chosen to champion this feature and removed new labels Feb 22, 2022
@brminnick
Copy link
Collaborator

I approve this proposal ✅

@VladislavAntonyuk
Copy link
Collaborator

can we use BaseConverter<object?, bool>? example: https://github.com/CommunityToolkit/Maui/pull/327/files
I approve

@brminnick
Copy link
Collaborator

Good catch! Yes, this is a perfect use case for BaseConverter<object?, bool>.

I'll edit the description now 👍

@bijington
Copy link
Contributor

I vote to approve

@brminnick
Copy link
Collaborator

Congrats @IeuanWalker! This Proposal has officially been approved and can be implemented!

Would you like to also do the implementation and open a Pull Request?

@IeuanWalker
Copy link
Contributor Author

@brminnick awesome, ye will do

@brminnick brminnick added approved This Proposal has been approved and is ready to be added to the Toolkit in-progress labels Mar 16, 2022
@IeuanWalker

This comment was marked as resolved.

@VladislavAntonyuk

This comment was marked as resolved.

@IeuanWalker

This comment was marked as resolved.

@ghost ghost added the help wanted This proposal has been approved and is ready to be implemented label Mar 16, 2022
@pictos

This comment was marked as resolved.

@VladislavAntonyuk

This comment was marked as resolved.

@IeuanWalker

This comment was marked as resolved.

@brminnick

This comment was marked as resolved.

@brminnick brminnick removed the help wanted This proposal has been approved and is ready to be implemented label Mar 22, 2022
@brminnick brminnick linked a pull request Apr 17, 2022 that will close this issue
6 tasks
@ghost
Copy link

ghost commented Apr 19, 2022

Reopening Proposal.

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

@ghost ghost reopened this Apr 19, 2022
@ghost ghost reopened this Apr 20, 2022
@ghost
Copy link

ghost commented Apr 20, 2022

Reopening Proposal.

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

@ghost ghost closed this as completed Apr 20, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Nov 24, 2024
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
approved This Proposal has been approved and is ready to be added to the Toolkit champion A member of the .NET MAUI Toolkit core team has chosen to champion this feature documentation approved proposal A fully fleshed out proposal describing a new feature in syntactic and semantic detail
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants