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

Nullable #2

Closed
VladislavAntonyuk opened this issue Jun 24, 2022 · 7 comments
Closed

Nullable #2

VladislavAntonyuk opened this issue Jun 24, 2022 · 7 comments

Comments

@VladislavAntonyuk
Copy link
Contributor

VladislavAntonyuk commented Jun 24, 2022

make code compatible with C# 8 nullable: https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/tutorials/nullable-reference-types

@rrmanzano
Copy link
Owner

Hi @VladislavAntonyuk

First of all, thank you for your feedback, I am always open to comments/feedback. I like the idea to support "Nullable Reference Types", I will include this feature ASAP.

Here is my proposal (Taking the string type as an example): If we try to assign "typeof(string?)" as "declaringType" parameter to the method "BindableProperty.Create", we will receive a compilation error, then based on the documentation the nullable reference types are a compile-time feature, that means we can set that parameter like "declaringType = typeof(string)" instead of "declaringType = typeof(string?)".

    using Maui.BindableProperty.Generator.Core;

    public partial class CustomEntry : ContentView
    {
        [AutoBindable]
        private string? _text;
    }
    public partial class CustomEntry
    {
        public static readonly Microsoft.Maui.Controls.BindableProperty TextProperty =
                                    Microsoft.Maui.Controls.BindableProperty.Create(
                                                            nameof(Text),
                                                            typeof(string), // Here the change
                                                            typeof(CustomEntry),
                                                            default(string?));

        public string? Text
        {
            get => (string?)GetValue(TextProperty);
            set => SetValue(TextProperty, value);
        }
    }

@rrmanzano
Copy link
Owner

Hi @VladislavAntonyuk , I just sent a new release (Version 0.8.0) that includes the new functionality to support "Nullable Reference Types" Here is the commit. Again, I am always open to comments/feedback, Thanks !!

@VladislavAntonyuk
Copy link
Contributor Author

VladislavAntonyuk commented Jun 30, 2022

Hi @rrmanzano. Thank you for the fix. Unfortunately, I receive this error: CS8669 The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.

image

image

You can add <WarningsAsErrors>Nullable</WarningsAsErrors> to Demo project to achieve the same errors

@VladislavAntonyuk
Copy link
Contributor Author

I will create a PR

@tmose1106
Copy link

Hiya, I'm not seeing this feature functioning as intend in 0.9.2

I.e., I am running .NET 7 and I have the following class:

    public partial class OpenShiftViewModel : BindableObject
    {
        [AutoBindable()]
        private readonly string? _facility;
        [AutoBindable]
        private readonly string? _department;
    }

And the output looks like such:

    public partial class OpenShiftViewModel
    {
        [global::System.CodeDom.Compiler.GeneratedCode("Maui.BindableProperty.Generator.Core.BindableProperty.AutoBindablePropertyGenerator", "0.9.2.0")]
        public static readonly Microsoft.Maui.Controls.BindableProperty FacilityProperty =
                                        Microsoft.Maui.Controls.BindableProperty.Create(
                                                                nameof(Facility),
                                                                typeof(string),
                                                                typeof(OpenShiftViewModel),
                                                                defaultValue: default(string));

        [global::System.CodeDom.Compiler.GeneratedCode("Maui.BindableProperty.Generator.Core.BindableProperty.AutoBindablePropertyGenerator", "0.9.2.0")]
        [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
        public string Facility
        {
            get => (string)GetValue(FacilityProperty);
            set => SetValue(FacilityProperty, value);
        }

        [global::System.CodeDom.Compiler.GeneratedCode("Maui.BindableProperty.Generator.Core.BindableProperty.AutoBindablePropertyGenerator", "0.9.2.0")]
        public static readonly Microsoft.Maui.Controls.BindableProperty DepartmentProperty =
                                        Microsoft.Maui.Controls.BindableProperty.Create(
                                                                nameof(Department),
                                                                typeof(string),
                                                                typeof(OpenShiftViewModel),
                                                                defaultValue: default(string));

        [global::System.CodeDom.Compiler.GeneratedCode("Maui.BindableProperty.Generator.Core.BindableProperty.AutoBindablePropertyGenerator", "0.9.2.0")]
        [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
        public string Department
        {
            get => (string)GetValue(DepartmentProperty);
            set => SetValue(DepartmentProperty, value);
        }
}

Am I missing some here?

@rrmanzano
Copy link
Owner

Hi, @tmose1106 looks like there is an issue in the latest version (0.9.2.0), let me take a look, and If that is an issue I will send a fix ASAP.

@rrmanzano
Copy link
Owner

Hi, @tmose1106 thank you for your feedback, the fix is ready in version 0.9.3, you can find it here:
https://www.nuget.org/packages/M.BindableProperty.Generator/0.9.3

image

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

No branches or pull requests

3 participants