When I try to create a Grid, it throw System.Runtime.InteropServices.COMException
var definition = new RowDefinition {Height = GridLength.Auto};
var grid = new Grid { RowDefinitions = { definition, definition } };
StackTrace below:
System.Runtime.InteropServices.COMException (0x800F1000): No installed components were detected.
at WinRT.DelegateExtensions.DynamicInvokeAbi(Delegate del, Object[] invoke_params)
at ABI.System.Collections.Generic.IVectorMethods`1.Append(IObjectReference obj, T value)
at Test..ctor()
...
I try to run below code at Immediate Window:
var g1 = new Grid{RowDefinitions = { new RowDefinition {Height = GridLength.Auto} } };No Exception.RowDefinition d = new() {Height = GridLength.Auto};No Exception.var g3 = new Grid{RowDefinitions = {d}};COMException.
And I change my code into
RowDefinition Definition() => new RowDefinition {Height = GridLength.Auto};
var grid = new Grid { RowDefinitions = { Definition(), Definition() } };
it can run without exception.
-
The first one puts the same RowDefinition into the grid twice. The second one creates two RowDefinitions objects. You should have seen the message in the debugger "Element is already the child of another element."Raymond Chen– Raymond Chen2023年03月04日 05:05:05 +00:00Commented Mar 4, 2023 at 5:05
-
@AndrewKeepCoding The problem is already solved. They just want to know why the solution works.Raymond Chen– Raymond Chen2023年03月08日 01:04:32 +00:00Commented Mar 8, 2023 at 1:04
-
@RaymondChen Yeah. It seems like it. Thanks!Andrew KeepCoding– Andrew KeepCoding2023年03月08日 01:09:22 +00:00Commented Mar 8, 2023 at 1:09
lang-cs