maui
maui copied to clipboard
[Bug] Grid.Children missing Add convenience methods
Description
Error:
error CS1501: No overload for method 'Add' takes 3 arguments
Code:
var grid = new Microsoft.Maui.Controls.Layout2.GridLayout
{
RowSpacing = 0,
ColumnSpacing = 0,
RowDefinitions =
{
new RowDefinition(),
new RowDefinition(),
new RowDefinition()
},
ColumnDefinitions =
{
new ColumnDefinition(),
new ColumnDefinition(),
new ColumnDefinition()
}
};
grid.Children.Add(new BoxView
{
Color = Color.DarkSlateBlue
}, 2, 1);
I see now that you cannot do RowDefinitions/ColumnDefinitions that way anymore? And you cannot add views to Children.
grid.Add(new BoxView
{
Color = Color.LightSkyBlue
});
That ^ also doesn't allow 3 args.
Basic Information
- Version with issue: Preview 3
Workaround
Use the Add methods off Grid instead of Grid.Children.
maybe I'm wrong, but droid samples does not work for grid layout
maybe I'm wrong, but droid samples does not work for grid layout
If you're running into a problem with GridLayout, please open an issue.
I hacked the Add onto Grid directly for this PR: https://github.com/dotnet/maui/pull/1894
But we probs want to add that IGridList<IView>
This is really annoying for those that do views-in-code, and the current docs "lie" about the API.
verified with VS 17.2.0 Preview 2.0 [32228.609.main],
Grid is from Microsoft.Maui.Controls.Grid,
there are 3 args to add a child to given row and column:
grid.Add(new BoxView
{
Color = Colors.Blue
}, 1, 0);
Content = grid;

You can workaround this for now by calling SetRow/Column explicitly:
grid.SetRow((IView)view1, 0);
grid.SetColumn((IView)view1, 0);
grid.SetRow((IView)view2, 0);
grid.SetColumn((IView)view2, 1);
grid.Children.Add(view1);
grid.Children.Add(view2);
The cast is necessary because of this:

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.
Still happening with Visual Studio 17.4.5 and using .NET MAUI version 7.0.59. Attaching latest reproduction sample using the exact same code in our API documentation page. It's confusing when the build error says it's internal only and the API docs property says it's internal only but the sample in the API doc uses this syntax. GridAddTest.zip
BUILD ERROR:
Build started...
1>------ Build started: Project: GridAddTest, Configuration: Debug Any CPU ------
1>C:\Users\Sweeky Satpathy\source\repos\GridAddTest\GridAddTest\MainPage.xaml.cs(32,24,32,30): warning CS0612: 'Device' is obsolete
1>C:\Users\Sweeky Satpathy\source\repos\GridAddTest\GridAddTest\MainPage.xaml.cs(32,44,32,53): warning CS0612: 'NamedSize' is obsolete
1>C:\Users\Sweeky Satpathy\source\repos\GridAddTest\GridAddTest\MainPage.xaml.cs(32,24,32,75): warning CS0612: 'Device.GetNamedSize(NamedSize, Type)' is obsolete
1>C:\Users\Sweeky Satpathy\source\repos\GridAddTest\GridAddTest\MainPage.xaml.cs(29,23,29,26): error CS1501: No overload for method 'Add' takes 5 arguments
1>C:\Users\Sweeky Satpathy\source\repos\GridAddTest\GridAddTest\MainPage.xaml.cs(36,23,36,26): error CS1501: No overload for method 'Add' takes 3 arguments
1>C:\Users\Sweeky Satpathy\source\repos\GridAddTest\GridAddTest\MainPage.xaml.cs(43,23,43,26): error CS1501: No overload for method 'Add' takes 3 arguments
1>C:\Users\Sweeky Satpathy\source\repos\GridAddTest\GridAddTest\MainPage.xaml.cs(49,23,49,26): error CS1501: No overload for method 'Add' takes 3 arguments
1>C:\Users\Sweeky Satpathy\source\repos\GridAddTest\GridAddTest\MainPage.xaml.cs(54,23,54,26): error CS1501: No overload for method 'Add' takes 3 arguments
1>C:\Users\Sweeky Satpathy\source\repos\GridAddTest\GridAddTest\MainPage.xaml.cs(63,23,63,26): error CS1501: No overload for method 'Add' takes 5 arguments
1>C:\Users\Sweeky Satpathy\source\repos\GridAddTest\GridAddTest\MainPage.xaml.cs(72,23,72,26): error CS1501: No overload for method 'Add' takes 5 arguments
1>C:\Users\Sweeky Satpathy\source\repos\GridAddTest\GridAddTest\MainPage.xaml.cs(81,23,81,26): error CS1501: No overload for method 'Add' takes 3 arguments
1>Done building project "GridAddTest.csproj" -- FAILED.
1>C:\Users\Sweeky Satpathy\source\repos\GridAddTest\GridAddTest\MainPage.xaml.cs(32,24,32,30): warning CS0612: 'Device' is obsolete
1>C:\Users\Sweeky Satpathy\source\repos\GridAddTest\GridAddTest\MainPage.xaml.cs(32,44,32,53): warning CS0612: 'NamedSize' is obsolete
1>C:\Users\Sweeky Satpathy\source\repos\GridAddTest\GridAddTest\MainPage.xaml.cs(32,24,32,75): warning CS0612: 'Device.GetNamedSize(NamedSize, Type)' is obsolete
1>C:\Users\Sweeky Satpathy\source\repos\GridAddTest\GridAddTest\MainPage.xaml.cs(29,23,29,26): error CS1501: No overload for method 'Add' takes 5 arguments
1>C:\Users\Sweeky Satpathy\source\repos\GridAddTest\GridAddTest\MainPage.xaml.cs(36,23,36,26): error CS1501: No overload for method 'Add' takes 3 arguments
1>C:\Users\Sweeky Satpathy\source\repos\GridAddTest\GridAddTest\MainPage.xaml.cs(43,23,43,26): error CS1501: No overload for method 'Add' takes 3 arguments
1>C:\Users\Sweeky Satpathy\source\repos\GridAddTest\GridAddTest\MainPage.xaml.cs(49,23,49,26): error CS1501: No overload for method 'Add' takes 3 arguments
1>C:\Users\Sweeky Satpathy\source\repos\GridAddTest\GridAddTest\MainPage.xaml.cs(54,23,54,26): error CS1501: No overload for method 'Add' takes 3 arguments
1>C:\Users\Sweeky Satpathy\source\repos\GridAddTest\GridAddTest\MainPage.xaml.cs(63,23,63,26): error CS1501: No overload for method 'Add' takes 5 arguments
1>C:\Users\Sweeky Satpathy\source\repos\GridAddTest\GridAddTest\MainPage.xaml.cs(72,23,72,26): error CS1501: No overload for method 'Add' takes 5 arguments
1>C:\Users\Sweeky Satpathy\source\repos\GridAddTest\GridAddTest\MainPage.xaml.cs(81,23,81,26): error CS1501: No overload for method 'Add' takes 3 arguments
1>Done building project "GridAddTest.csproj" -- FAILED.
1>C:\Users\Sweeky Satpathy\source\repos\GridAddTest\GridAddTest\MainPage.xaml.cs(32,24,32,30): warning CS0612: 'Device' is obsolete
1>C:\Users\Sweeky Satpathy\source\repos\GridAddTest\GridAddTest\MainPage.xaml.cs(32,44,32,53): warning CS0612: 'NamedSize' is obsolete
1>C:\Users\Sweeky Satpathy\source\repos\GridAddTest\GridAddTest\MainPage.xaml.cs(32,24,32,75): warning CS0612: 'Device.GetNamedSize(NamedSize, Type)' is obsolete
1>C:\Users\Sweeky Satpathy\source\repos\GridAddTest\GridAddTest\MainPage.xaml.cs(29,23,29,26): error CS1501: No overload for method 'Add' takes 5 arguments
1>C:\Users\Sweeky Satpathy\source\repos\GridAddTest\GridAddTest\MainPage.xaml.cs(36,23,36,26): error CS1501: No overload for method 'Add' takes 3 arguments
1>C:\Users\Sweeky Satpathy\source\repos\GridAddTest\GridAddTest\MainPage.xaml.cs(43,23,43,26): error CS1501: No overload for method 'Add' takes 3 arguments
1>C:\Users\Sweeky Satpathy\source\repos\GridAddTest\GridAddTest\MainPage.xaml.cs(49,23,49,26): error CS1501: No overload for method 'Add' takes 3 arguments
1>C:\Users\Sweeky Satpathy\source\repos\GridAddTest\GridAddTest\MainPage.xaml.cs(54,23,54,26): error CS1501: No overload for method 'Add' takes 3 arguments
1>C:\Users\Sweeky Satpathy\source\repos\GridAddTest\GridAddTest\MainPage.xaml.cs(63,23,63,26): error CS1501: No overload for method 'Add' takes 5 arguments
1>C:\Users\Sweeky Satpathy\source\repos\GridAddTest\GridAddTest\MainPage.xaml.cs(72,23,72,26): error CS1501: No overload for method 'Add' takes 5 arguments
1>C:\Users\Sweeky Satpathy\source\repos\GridAddTest\GridAddTest\MainPage.xaml.cs(81,23,81,26): error CS1501: No overload for method 'Add' takes 3 arguments
1>Done building project "GridAddTest.csproj" -- FAILED.
1>C:\Users\Sweeky Satpathy\source\repos\GridAddTest\GridAddTest\MainPage.xaml.cs(32,24,32,30): warning CS0612: 'Device' is obsolete
1>C:\Users\Sweeky Satpathy\source\repos\GridAddTest\GridAddTest\MainPage.xaml.cs(32,44,32,53): warning CS0612: 'NamedSize' is obsolete
1>C:\Users\Sweeky Satpathy\source\repos\GridAddTest\GridAddTest\MainPage.xaml.cs(32,24,32,75): warning CS0612: 'Device.GetNamedSize(NamedSize, Type)' is obsolete
1>C:\Users\Sweeky Satpathy\source\repos\GridAddTest\GridAddTest\MainPage.xaml.cs(29,23,29,26): error CS1501: No overload for method 'Add' takes 5 arguments
1>C:\Users\Sweeky Satpathy\source\repos\GridAddTest\GridAddTest\MainPage.xaml.cs(36,23,36,26): error CS1501: No overload for method 'Add' takes 3 arguments
1>C:\Users\Sweeky Satpathy\source\repos\GridAddTest\GridAddTest\MainPage.xaml.cs(43,23,43,26): error CS1501: No overload for method 'Add' takes 3 arguments
1>C:\Users\Sweeky Satpathy\source\repos\GridAddTest\GridAddTest\MainPage.xaml.cs(49,23,49,26): error CS1501: No overload for method 'Add' takes 3 arguments
1>C:\Users\Sweeky Satpathy\source\repos\GridAddTest\GridAddTest\MainPage.xaml.cs(54,23,54,26): error CS1501: No overload for method 'Add' takes 3 arguments
1>C:\Users\Sweeky Satpathy\source\repos\GridAddTest\GridAddTest\MainPage.xaml.cs(63,23,63,26): error CS1501: No overload for method 'Add' takes 5 arguments
1>C:\Users\Sweeky Satpathy\source\repos\GridAddTest\GridAddTest\MainPage.xaml.cs(72,23,72,26): error CS1501: No overload for method 'Add' takes 5 arguments
1>C:\Users\Sweeky Satpathy\source\repos\GridAddTest\GridAddTest\MainPage.xaml.cs(81,23,81,26): error CS1501: No overload for method 'Add' takes 3 arguments
1>Done building project "GridAddTest.csproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
========== Elapsed 00:04.399 ==========
The API docs are wrong at this point. There were taken from Forms and not updated for .NET MAUI yet.
It seems that we have a replacement for the 3 argument overload. It's not done on the Grid.Children anymore, but you can do it on Grid directly. So: Grid.Add(new Label(), 1, 1);.
The 5 arguments overload is provided in a PR here and we're discussing if this is something we want to take in as it seems a bit of a confusing API.
I think this was alluded to earlier, but the Children property is documented as “For internal use by the Microsoft.Maui.Controls platform.” at https://learn.microsoft.com/en-us/dotnet/api/microsoft.maui.controls.layout.children?view=net-maui-7.0 - the original Xamarin.Forms docs say the same sort of thing. Given that Layout defines Add (and Grid adds the 3 argument version of Add), I would want to prefer those methods since their docs don’t mention “internal use only.”
Or is the Children property now equally preferred in MAUI, and it’s just that the docs are out of date?
I just converted a large batch of direct Children property references in my just-ported app to Add, Remove, and Clear method calls (on the Layout, as opposed to the Children property), with no obvious behavior differences. It seems like a good idea, but I can’t tell if it’s really justified. I could imagine that the methods meet a constraint that the internal-only property doesn't. Or maybe it's all the same thing?
If the Children property really is internal use only, it would be nice to mark it with an Obsolete attribute or something to that effect. If it's standard practice to use it (as various docs imply) then it would be good to change the documentation.