BlankPage构造函数无法初始化组件

问题描述 投票:11回答:6

我正在启动qazxsw poi,我在我的空白页面应用程序中添加了一些代码。突然间,一个构造函数正在初始化一个组件:

learning XAML

停止工作。我现在有这个错误:

'BlankApplication.BlankPage'不包含'InitializeComponent'的定义,并且没有扩展方法'InitializeComponent'接受类型为'BlankApplication.BlankPage'的第一个参数'(您是否缺少using指令或程序集引用?)

老实说,我没有做任何事,我甚至没有看到这部分代码,现在它不起作用。

截图:

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

c# xaml windows-8 microsoft-metro
6个回答
14
投票

只是提供一些关于如何解决这个问题的更多信息(因为这个解释有点模糊不清)..

这个问题(对我而言)是因为我在创建项目后更改了代码中的命名空间。为了解决这个问题,我不得不在几个地方做一些改变:

1:在App.xaml中,我必须更改以下内容:

2:在MainPage.xaml中,我不得不更改以下内容:

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

您还需要确保更改App.xaml.cs中的“命名空间”行以及MainPage.xaml.cs。

最后,您还需要确保更新Package.appxmanifest中的Project Entrypoint以指向“New.Namespace.App”。


3
投票

更改类的命名空间时会发生这种情况,您必须在XAML文件中执行相同操作。

XAML文件中有两个带有旧命名空间的位置。


1
投票

如果Main类的命名空间与.xaml文件的x:Class属性不同,则会出现此错误。例如;

你的MainPage.xaml.cs;

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

你的MainPage.xaml;

    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();
            }
        }
    }

在将x:class更改为;之前,您将看到错误。

X:类= “UWPControls.MainPage”


0
投票

对我来说,该项目是与其他项目的内部解决方案。当这里的建议不起作用时,从解决方案中简单地卸载和重新加载项目就可以解决所有错误(当然还有重建)。


-1
投票

我的解决方案:对于我的“SomePage:ContentPage”,我更改了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> MSBuild:Compile
  • BuildAction:从MSBuild:UpdateDesignTimeXamlPage

使用Visual Studio 2017 Enterprise 15.3.4


-3
投票

问题解决了。原因:我忘了也在xaml代码中更改自定义应用程序名称。解决方案:我在XAML中更改了应用程序名称,现在它运行良好。

© www.soinside.com 2019 - 2024. All rights reserved.