{"id":5870,"date":"2024-03-03T17:48:31","date_gmt":"2024-03-03T16:48:31","guid":{"rendered":"https:\/\/ekiwi.de\/index.php\/5870\/c-wpf-initialisecomponent-does-not-exist-in-the-current-context\/"},"modified":"2024-03-03T17:53:27","modified_gmt":"2024-03-03T16:53:27","slug":"c-wpf-initializecomponent-does-not-exist-in-the-current-context","status":"publish","type":"post","link":"https:\/\/ekiwi.de\/en\/index.php\/5870\/c-wpf-initializecomponent-does-not-exist-in-the-current-context\/","title":{"rendered":"C# \/ WPF: InitializeComponent does not exist in the current context"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of content<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/ekiwi.de\/en\/index.php\/5870\/c-wpf-initializecomponent-does-not-exist-in-the-current-context\/#Error_with_InitialiseComponent\" >Error with InitialiseComponent()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/ekiwi.de\/en\/index.php\/5870\/c-wpf-initializecomponent-does-not-exist-in-the-current-context\/#Solution\" >Solution<\/a><\/li><\/ul><\/nav><\/div>\n<p>A problem that has cost me a good few hours in Visual Studio when programming a WPF application with C# is that the method <em>InitializeComponent()<\/em> in the structure of the <em>CodeBehind<\/em> of the <em>MainWindows<\/em> or in other dialogues has brought an error message.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Error_with_InitialiseComponent\"><\/span>Error with InitialiseComponent()<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>This is the following error message:<\/p>\n<blockquote><p>CS0103: The name &#8216;InitializeComponent&#8217; does not exist in the current context<\/p><\/blockquote>\n<figure id=\"attachment_5868\" aria-describedby=\"caption-attachment-5868\" style=\"width: 584px\" class=\"wp-caption aligncenter\"><a href=\"\/wp-content\/uploads\/2024\/03\/the-name-initializecomponent-does-not-exist-in-the-current-context.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-5868\" src=\"\/wp-content\/uploads\/2024\/03\/the-name-initializecomponent-does-not-exist-in-the-current-context.jpg\" alt=\"Screenshot of the error message that the name InitializeComponent does not exist in the current context.\" width=\"584\" height=\"207\" srcset=\"\/wp-content\/uploads\/2024\/03\/the-name-initializecomponent-does-not-exist-in-the-current-context.jpg 584w, \/wp-content\/uploads\/2024\/03\/the-name-initializecomponent-does-not-exist-in-the-current-context-300x106.jpg 300w\" sizes=\"auto, (max-width: 584px) 100vw, 584px\" \/><\/a><figcaption id=\"caption-attachment-5868\" class=\"wp-caption-text\">Error\/Warning CS0103 for InitializeComponent<\/figcaption><\/figure>\n<p>Despite this error message, the application was compilable, but showed strange behavior. Among other things, newly inserted <em>UserControls<\/em> in the <em>CodeBehind<\/em> were no longer recognized during programming.<\/p>\n<p>If you search the Internet, you will come across many forum posts stating that this error has already cost other programmers many hours and even caused them to despair.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Solution\"><\/span>Solution<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>You can also read that there can be several different causes for the problem. One reason could be that the namespace in the <em>CodeBehind<\/em> does not match the namespace included in the XAML file. They must therefore be the same:<\/p>\n<p><strong>Example:&lt;\/strong<\/strong><\/p>\n<p><em>CodeBehind:<\/em> <span style=\"color: #0000ff;\">namespace <\/span>MyNamespace{}<\/p>\n<p><em>XAML:<\/em> <span style=\"color: #ff0000;\">x:Class<\/span><span style=\"color: #0000ff;\">=&#8221;MyNamespace.MainWindow&#8221;<\/span><\/p>\n<p>I was able to rule that out in my case.<\/p>\n<p>Another way to try it out is to delete the <em>obj<\/em> folder in the solution and close and restart <a href=\"https:\/\/ekiwi.de\/en\/index.php\/4793\/visual-studio-referenced-component-could-not-be-found\/\">Visual Studio<\/a> once. Then do a <em>rebuild<\/em> of the solution. But even that didn&#8217;t help in my case.<\/p>\n<p>In my case, the problem was apparently in the build settings for the XAML file. The custom tool had &#8220;<em>MSBuild:Compile<\/em>&#8221; in it.<\/p>\n<figure id=\"attachment_5866\" aria-describedby=\"caption-attachment-5866\" style=\"width: 527px\" class=\"wp-caption aligncenter\"><a href=\"\/wp-content\/uploads\/2024\/03\/xaml-build-settings-before.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-5866\" src=\"\/wp-content\/uploads\/2024\/03\/xaml-build-settings-before.jpg\" alt=\"Screenshot of the page properties of the XAML file before fixing the error\" width=\"527\" height=\"334\" srcset=\"\/wp-content\/uploads\/2024\/03\/xaml-build-settings-before.jpg 527w, \/wp-content\/uploads\/2024\/03\/xaml-build-settings-before-300x190.jpg 300w\" sizes=\"auto, (max-width: 527px) 100vw, 527px\" \/><\/a><figcaption id=\"caption-attachment-5866\" class=\"wp-caption-text\">Page properties of the XAML file with error message<\/figcaption><\/figure>\n<p>I have set the property &#8220;<em>Copy to Output Directory<\/em>&#8221; to &#8220;<strong><em>Do not copy<\/em><\/strong>&#8221; and as <em>Custom Tool<\/em> I have entered the <strong><em>XamlIntelliSenseFileGenerator<\/em> <\/strong>.<\/p>\n<figure id=\"attachment_5864\" aria-describedby=\"caption-attachment-5864\" style=\"width: 535px\" class=\"wp-caption aligncenter\"><a href=\"\/wp-content\/uploads\/2024\/03\/solution-change-xaml-file-properties.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-5864\" src=\"\/wp-content\/uploads\/2024\/03\/solution-change-xaml-file-properties.jpg\" alt=\"Screenshot of the page properties of the XAML file without the error message for InitializeComponent\" width=\"535\" height=\"420\" srcset=\"\/wp-content\/uploads\/2024\/03\/solution-change-xaml-file-properties.jpg 535w, \/wp-content\/uploads\/2024\/03\/solution-change-xaml-file-properties-300x236.jpg 300w\" sizes=\"auto, (max-width: 535px) 100vw, 535px\" \/><\/a><figcaption id=\"caption-attachment-5864\" class=\"wp-caption-text\">Page properties of the XAML file without error message<\/figcaption><\/figure>\n<p>In a more or less magical way, it works and the above-mentioned error message for InitializeComponent disappears. I don&#8217;t really have a reliable explanation for this. But I think that MSBuild is possibly not really suitable for compiling XAML files or cannot distinguish properly between WindowsForms and XAML files. It is therefore better to use a XAML compiler directly.<\/p>","protected":false},"excerpt":{"rendered":"<p>A problem that has cost me a good few hours in Visual Studio when programming a WPF application with C#<\/p>\n","protected":false},"author":2,"featured_media":5727,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[870],"tags":[872,873,876,875,887],"class_list":["post-5870","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming","tag-c-en","tag-programmieren-en","tag-programming-en","tag-visual-studio-en","tag-xaml-en"],"_links":{"self":[{"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/posts\/5870","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/comments?post=5870"}],"version-history":[{"count":0,"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/posts\/5870\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/media\/5727"}],"wp:attachment":[{"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/media?parent=5870"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/categories?post=5870"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/tags?post=5870"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}