Skip to content

Instantly share code, notes, and snippets.

@VincentH-Net
Last active April 4, 2022 18:47
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VincentH-Net/c7568727517c4ef4f622815a580316d9 to your computer and use it in GitHub Desktop.
Save VincentH-Net/c7568727517c4ef4f622815a580316d9 to your computer and use it in GitHub Desktop.
C# language proposal examples for UI markup #CSharpForMarkup
// C# vNext markup friendly
enum Row { Icon, Prompt, Header, Entry }
void Build() => Content = new Grid
{
RowDefinitions = Rows.Define(
(Icon , Auto),
(Prompt, Auto),
(Header, 50 ),
(Entry , Auto)
),
{
Image { }
.Row (Icon) .CenterH ()
.Bind (vm.Icon),
Label { LineBreakMode = WordWrap }
.Row (Prompt) .TextCenterH ()
.Bind (vm.RegistrationPrompt),
Label { Text = "CODE" }
.Row (Header) .Bottom (),
Entry { Placeholder = "123456", Keyboard = Numeric }
.Row (Entry) .Margins (left: 10)
.Bind (vm.RegistrationCode)
}
};
// C# 8
void Build() => Content = new Grid
{
RowDefinitions = Rows.Define(
(Row.Icon , Auto),
(Row.Prompt, Auto),
(Row.Header, 50 ),
(Row.Entry , Auto)
),
Children = {
new Image { }
.Row (Row.Icon) .CenterH ()
.Bind (nameof(vm.Icon)),
new Label { LineBreakMode = LineBreakMode.WordWrap }
.Row (Row.Prompt) .TextCenterH ()
.Bind (nameof(vm.RegistrationPrompt)),
new Label { Text = "CODE" }
.Row (Row.Header) .Bottom (),
new Entry { Placeholder = "123456", Keyboard = Keyboard.Numeric }
.Row (Row.Entry) .Margins (left: 10)
.Bind (nameof(vm.RegistrationCode))
}
};
@VincentH-Net
Copy link
Author

Nameof can sort-of be eliminated in C# 10 with the [CallerArgumentExpression] attribute.

@VincentH-Net
Copy link
Author

VincentH-Net commented Apr 4, 2022

Could we reduce repetition in nested object initializers?
(e.g. in a declarative UI markup expression, but useful when new-ing up any tree of objects)

  1. Allow to omit new in initializers below the tree root.
    So have new on the root, but not required to repeat new for each nested child or content object.

  2. Allow to indicate a default initialization property for a type (collection or single "content property"),
    so that property name can be omitted in an initialization expression. E.g.

    new Grid { Children = { new Label { }, new Button { } } } could be like:
    new Grid { { new Label { }, new Button { } } } or even:
    new Grid { new Label { }, new Button { } } and when combined with 1):
    new Grid { Label { }, Button { } }

  3. Allow to omit the parameter type when passing in a parameter value (SwiftUI uses .).
    To be used when passing in an enum value or a class static member to a parameter.
    (also useful outside expression trees) e.g.

    Alignment = Alignment.Left could be:
    Alignment = .Left

    where Alignment.Left is either an enum value or a static class member.
    When the . is entered, Intellisense starts just as if the full type name was entered before the .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment