VincentH-Net / CSharpForMarkup

Concise, declarative C# UI markup for .NET browser / native UI frameworks
MIT License
748 stars 43 forks source link

Grid Definitions - Values for Stars? #10

Closed freever closed 4 years ago

freever commented 4 years ago

Hi

I am really starting to love this library, and I can't wait for it to be part of Xamarin.Forms. Death to XAML :)

I can't figure out a way to provide a value to GridLength.Star when defining rows and columns. As far as I can tell I can either provide a numberic value, which is used as GridLength.Absolute, or I can provide a GridLength of .Star or .Auto... but I cannot give a numeric value AND a GridLength.

For example, if I want to create a grid with 3 columns, one of which is twice the size of the other two, I would do something like:

ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(2, GridUnitType.Star) });

Is there a snazzy way of doing this, like an overload that allows:

ColumnDefinitions = EnumsForGridRowsAndColumns.Columns.Define(
    (Cols.One, GridLength.Star, 1),
    (Cols.Two, GridLength.Star, 1),
    (ColsThree, GridLength.Star, 2));

Thanks!

VincentH-Net commented 4 years ago

Thanks @freever ! CSharpForMarkup is now integrated in Xamarin.Forms 4.6 (currently in preview release). It contains a lot of improvements (and quite a few helper renames so be aware of that) so I would encourage you to switch to that.

I already created a helper that does what you need: commit

public static GridLength Stars(double value) => new GridLength(value, GridUnitType.Star);

You can use it like this:

RowDefinitions = Rows.Define (
    (Row.ConvertedNumber, Star),
    (Row.ConvertButton  , Stars (2)))

This will be in the next Xamarin Forms CSharpForMarkup PR which will extend the merged one. For now you can add the above extension method to your project and add a using static of the class that contains it.

freever commented 4 years ago

Thanks I will use that for now