0

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:

  1. var g1 = new Grid{RowDefinitions = { new RowDefinition {Height = GridLength.Auto} } }; No Exception.
  2. RowDefinition d = new() {Height = GridLength.Auto}; No Exception.
  3. 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.

asked Mar 4, 2023 at 2:48
3
  • 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." Commented Mar 4, 2023 at 5:05
  • @AndrewKeepCoding The problem is already solved. They just want to know why the solution works. Commented Mar 8, 2023 at 1:04
  • @RaymondChen Yeah. It seems like it. Thanks! Commented Mar 8, 2023 at 1:09

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.