BlankPage constructor cannot initialize components
Asked Answered
C

6

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:

Chapland answered 22/4, 2012 at 8:53 Comment(7)
And even I start a new project there's still this error.Chapland
You said you added some code. What did you add?Timmytimocracy
Problem solved, I didn't change custom application name in xaml code.Chapland
Please post solution as an answer so it can help others too.Sauer
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").School
This is ideal solution for this problem. https://mcmap.net/q/73875/-the-name-39-initializecomponent-39-does-not-exist-in-the-current-contextLeucomaine
@Chapland 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.Piedadpiedmont
S
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"

Sardius answered 8/8, 2018 at 12:56 Comment(1)
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.Piedadpiedmont
D
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".

Darceldarcey answered 3/1, 2013 at 21:18 Comment(2)
My problem was an unseen error shown at the bottom of the MainPage.xaml page.Wayfarer
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!Pamphlet
E
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.

Eaglewood answered 13/10, 2016 at 11:8 Comment(0)
S
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"

Sardius answered 8/8, 2018 at 12:56 Comment(1)
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.Piedadpiedmont
M
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).

Manvell answered 29/4, 2019 at 14:38 Comment(0)
C
-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

Cimbura answered 22/9, 2017 at 17:45 Comment(0)
C
-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.

Chapland answered 23/4, 2012 at 13:58 Comment(3)
-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.Jampack
By changing it. I didn't before and that was the problem.Chapland
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 :)Amateurish

© 2022 - 2024 — McMap. All rights reserved.