13

I'm starting learning XAML and I add some code to my BlankPage application. And suddenly a constructor which is initializing a component:

    public BlankPage()
    {
        this.InitializeComponent();
    }

Stop working. I've got now that error:

'BlankApplication.BlankPage' does not contain a definition for 'InitializeComponent' and no extension method 'InitializeComponent' accepting a first argument of type 'BlankApplication.BlankPage' could be found (are you missing a using directive or an assembly reference?)

Honestly I didn't do anything, I didn't even look at this part of code and now it doesn't work.

Screenshot:

H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
r9s
  • 227
  • 2
  • 3
  • 13
  • And even I start a new project there's still this error. – r9s Apr 22 '12 at 09:14
  • You said you added some code. What did you add? – McGarnagle Apr 22 '12 at 09:24
  • Problem solved, I didn't change custom application name in xaml code. – r9s Apr 22 '12 at 09:38
  • 1
    Please post solution as an answer so it can help others too. – jv42 Apr 23 '12 at 07:39
  • 1
    Hi - Exactly the same thing happened to me, working through the MS "Metro" tutorial. "I didn't do anything"... except start the project with the wrong name ("WindowsBlogViewer", instead of "WindowsBlogReader"). The moment I cut/pasted XAML code from the tutorial (code with the *correct* namespace: "using:WindowsBlogReader") I got the "xyz.BlankPage' does not contain a definition for 'InitializeComponent" error. The fix was to recreate the project with the correct namespace ("WindowsBlogReader"). – paulsm4 May 30 '12 at 00:46
  • This is ideal solution for this problem. http://stackoverflow.com/a/7730022/2149459 – Arjun Feb 28 '17 at 21:59
  • @r9s You may want to mark the response from `nuriselcuk` as the `Answer` since he, I think, has correctly described your issue and has provided a correct solution. If you mark his response as an `Answer` it would help other readers (like me) of your post better. Thank you. – nam Jun 05 '19 at 20:37

6 Answers6

14

Just to give a bit more information on how to fix this (since this explanation is a bit on the vague side)..

This issue (for me) was caused because I changed the namespace in the code after I created the project. In order to fix this issue I had to make some changes in a couple of locations:

1: In App.xaml I had to change the following:

<Application
  x:Class="New.Namespace.App"

2: In MainPage.xaml I had to change the following:

<Page
  x:Class="New.Namespace.MainPage"

You will also want to make sure that you change the 'namespace' line in your App.xaml.cs as well as your MainPage.xaml.cs.

Finally, you will also want to make sure you update the Project Entrypoint in the Package.appxmanifest to point to "New.Namespace.App".

Briggs
  • 490
  • 1
  • 4
  • 10
  • My problem was an unseen error shown at the bottom of the MainPage.xaml page. – 27k1 Feb 13 '16 at 11:57
  • I added a ContentDialog in the wrong project in my solution, and when I moved it, I didn't change the namespace in the XAML. This tipped me off to the problem. Thanks! – dex3703 Mar 01 '17 at 22:21
3

This happens when you change a namespace for a class, you must do the same inside the XAML file.

There are two places inside the XAML file with the old namespace.

Axel Isouard
  • 1,498
  • 1
  • 24
  • 38
long.h
  • 31
  • 1
3

If your Main class's namespace is different than .xaml file's x:Class attribute, you will get this error. For instance;

Your MainPage.xaml.cs;

    namespace UWPControls
    {
        /// <summary>
        /// An empty page that can be used on its own or navigated to within a Frame.
        /// </summary>
        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
            }
        }
    }

Your MainPage.xaml;

<Page
    x:Class="UWPControls_Different.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:UWPHelloWorld"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
</page>

You're going to see the error until changing x:class to the;

x:Class="UWPControls.MainPage"

nuriselcuk
  • 515
  • 1
  • 4
  • 17
  • Your solution is best for the problem at hand (issue posted by the `OP`). Nice explanation related to the issue. I think `OP` tried to explain it later but probably could not do it as well as you did - and hence he deleted his response as show below. The `OP` should mark your response as an `Answer` so others can benefit from it. – nam Jun 05 '19 at 20:33
2

For me, the project was inside solution with other projects. When suggestions here did not work, a simple unloading and reloading the project from solution did the trick and fix all errors (with rebuilding of course).

Izik
  • 746
  • 1
  • 9
  • 25
-1

My Solution: For my "SomePage : ContentPage", I changed the XAML properties:

  • Generator (Custom Tool): From MSBuild:Compile to MSBuild:UpdateDesignTimeXaml
  • BuildAction: From Page to Embedded resource

Using Visual Studio 2017 Enterprise 15.3.4

Quality Catalyst
  • 6,531
  • 8
  • 38
  • 62
-3

Problem solved. Cause: I forgot to also change custom application name in xaml code. Solution: I have changed application name in XAML, now it works well.

brett
  • 744
  • 5
  • 15
r9s
  • 227
  • 2
  • 3
  • 13
  • -1. If you're going to answer your own question, you should answer it with the same quality as you would answer someone else's question. – Sinaesthetic Nov 27 '15 at 01:12
  • By changing it. I didn't before and that was the problem. – r9s May 31 '16 at 09:56
  • This actually helped me. I edited the phrasing of the answer to make it clearer, but it pointed out the right issue. You English purists stop the down-voting already :) – brett Jan 28 '18 at 18:36