Skip to content

[Blazor] Simplify passing enum values #7077

Open
@NUlliiON

Description

@NUlliiON

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

Clone of dotnet/aspnetcore#12154
This is a clone of an existing issue. I saw the comment dotnet/aspnetcore#12154 (comment). Is it possible to implement this functionality in the future? This will greatly reduce the amount of code.
My current solution:

public enum StackLayoutHorizontalAlign
{
    Center
}
public enum StackLayoutVerticalAlign
{
    Bottom
}
public class StackLayout : ComponentBase
{
    [Parameter]
    public StackLayoutHorizontalAlign HorizontalAlign { get; set; }

    [Parameter]
    public StackLayoutVerticalAlign VerticalAlign { get; set; }

    ...
}

Usage:

<StackLayout HorizontalAlign="StackLayoutHorizontalAlign.Center" VerticalAlign="StackLayoutVerticalAlign.Bottom">
    ...
</StackLayout>

You could suggest something like:

public class StackLayout : ComponentBase
{
    [Parameter]
    public string HorizontalAlign { get; set; }

    [Parameter]
    public string VerticalAlign { get; set; }

    ...
}

public static class HAlign
{
    public const string Center = "center";

    ...
}

public static class VAlign
{
    public const string Bottom = "bottom";

    ...
}
  1. In this case there is no support for the highlighting code (inconvenient to refactor).
<StackLayout HorizontalAlign="center" VerticalAlign="bottom">
    ...
</StackLayout>
  1. In this case, additional classes complicate the code.
<StackLayout HorizontalAlign="@HAlign.Center" VerticalAlign="VAlign.Bottom">
    ...
</StackLayout>

I don't think this is a solution to the problem.

Describe the solution you'd like

My solution:

public enum StackLayoutHorizontalAlign
{
    Center
}
public enum StackLayoutVerticalAlign
{
    Bottom
}
public class StackLayout : ComponentBase
{
    [Parameter]
    public StackLayoutHorizontalAlign HorizontalAlign { get; set; }

    [Parameter]
    public StackLayoutVerticalAlign VerticalAlign { get; set; }

    ...
}

Usage:

<StackLayout HorizontalAlign="Center" VerticalAlign="Bottom">
    ...
</StackLayout>

I also think that this improvement will work well with "String-based enums" if they are implemented in C# (dotnet/csharplang#2849)

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions