{"id":111746,"date":"2025-10-30T07:00:00","date_gmt":"2025-10-30T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=111746"},"modified":"2025-10-30T09:47:56","modified_gmt":"2025-10-30T16:47:56","slug":"20251030-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20251030-00\/?p=111746","title":{"rendered":"Trying to build a XAML tree in code throws a &#8220;No installed components were detected&#8221; exception"},"content":{"rendered":"<p>You might, for some reason, be building some XAML in code rather than markup. Starting with this XAML:<\/p>\n<pre>&lt;!-- XAML markup --&gt;\r\n&lt;Grid&gt;\r\n    &lt;Grid.ColumnDefinitions&gt;\r\n        &lt;ColumnDefinition Width=\"Auto\" \/&gt;\r\n        &lt;ColumnDefinition Width=\"Auto\" \/&gt;\r\n    &lt;\/Grid.ColumnDefinitions&gt;\r\n&lt;\/Grid&gt;\r\n<\/pre>\n<p>Here&#8217;s a flawed attempt to create the grid in code.<\/p>\n<pre>\/\/ C#\r\nvar autoWidth = new ColumnDefinition() { Width = GridLength.Auto };\r\nvar grid = new Grid() { ColumnDefinitions = { autoWidth, autoWidth } };\r\n<\/pre>\n<p>We create a column definition whose Width is Auto, and then create a grid whose ColumnDefinitions is two columns, namely two of our Auto columns.<\/p>\n<p>This throws a confusing exception.<\/p>\n<pre style=\"white-space: pre-wrap;\">System.Runtime.InteropServices.COMException (0x800F1000): No installed components were detected.\r\n<\/pre>\n<p>What do you mean, no installed components were detected? Why does something need to be installed at all? Do I have to install a special component to be able to create XAML elements from code?<\/p>\n<p>Okay, the first problem is an error code collision. The error message text is coming from error number <code>0x800F1000<\/code> which is <code>SPAPI_<wbr \/>E_<wbr \/>ERROR_<wbr \/>NOT_<wbr \/>INSTALLED<\/code> and belongs to the SETUPAPI facility. Unfortunately, XAML decided to put some of its error codes in the same facility, resulting in an error code collision. XAML defined error code number <code>0x800F1000<\/code> to mean &#8220;invalid operation&#8221; but gave it the same number as <code>SPAPI_<wbr \/>E_<wbr \/>ERROR_<wbr \/>NOT_<wbr \/>INSTALLED<\/code>. Therefore, when various components try to decide the error code, they come up with the setup error instead of the XAML error.<\/p>\n<p>Another XAML error code collision is &#8220;X is not a valid value for property Y&#8221; which has the numeric value <code>0x800F1001<\/code> and which collides with <code>SPAPI_<wbr \/>E_<wbr \/>BAD_<wbr \/>SECTION_<wbr \/>NAME_<wbr \/>LINE<\/code>.<\/p>\n<p>So the real problem has nothing to do with installed components. That&#8217;s just an unfortunate decode of the error code.<\/p>\n<p>Fortunately, XAML provides a details string, which is easily overlooked because it&#8217;s two lines away and looks like part of another paragraph.<\/p>\n<pre style=\"white-space: pre-wrap;\">System.Runtime.InteropServices.COMException (0x800F1000): No installed components were detected.\r\n\r\n<span style=\"border: solid 1px currentcolor;\">Element is already the child of another element.<\/span>\r\nSource: Cannot evaluate the exception source.\r\n<\/pre>\n<p>The problem is that we are reusing the same <code>ColumnDefinition<\/code> object to describe two different columns. The markup creates two different <code>ColumnDefinition<\/code> objects, but our code version creates one object and inserts it twice.<\/p>\n<p>To make our code equivalent to the markup, we have to create two <code>ColumnDefinition<\/code> objects, because that&#8217;s what the markup does.<\/p>\n<pre>\/\/ C#\r\nvar autoWidth1 = new ColumnDefinition() { Width = GridLength.Auto };\r\nvar autoWidth2 = new ColumnDefinition() { Width = GridLength.Auto };\r\nvar grid = new Grid() { ColumnDefinitions = { autoWidth1, autoWidth2 } };\r\n<\/pre>\n<p>And since we&#8217;re not reusing the column definition, we don&#8217;t need to give it a name.<\/p>\n<pre>\/\/ C#\r\nvar grid = new Grid() { ColumnDefinitions = {\r\n        new ColumnDefinition() { Width = GridLength.Auto },\r\n        new ColumnDefinition() { Width = GridLength.Auto }\r\n    } };\r\n<\/pre>\n<p>It&#8217;s convenient that this code does resemble the original XAML.<\/p>\n<p>We thought we were doing the right thing by reusing an existing object with identical properties, but the XAML tree is a tree, and you can&#8217;t insert the same node into a tree in two different locations.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>An unfortunate error code collision, but the explanatory text leads the way.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-111746","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>An unfortunate error code collision, but the explanatory text leads the way.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/111746","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=111746"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/111746\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=111746"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=111746"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=111746"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}