Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BlankPage constructor cannot initialize components

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:

like image 325
r9s Avatar asked Apr 22 '12 08:04

r9s


4 Answers

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).

like image 133
Izik Avatar answered Oct 31 '22 20:10

Izik


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".

like image 25
Briggs Avatar answered Oct 31 '22 21:10

Briggs


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"

like image 43
nuriselcuk Avatar answered Oct 31 '22 22:10

nuriselcuk


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.

like image 39
long.h Avatar answered Oct 31 '22 22:10

long.h