BlankPage constructor cannot initialize components

我是研究僧i 提交于 2019-12-30 04:03:14

问题


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:


回答1:


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"




回答2:


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




回答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.




回答4:


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




回答5:


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




回答6:


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.



来源:https://stackoverflow.com/questions/10266558/blankpage-constructor-cannot-initialize-components

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!