BlankPage构造函数无法初始化组件 [英] BlankPage constructor cannot initialize components
问题描述
我正在开始学习XAML ,我将一些代码添加到我的BlankPage应用程序中.突然,一个构造函数正在初始化一个组件:
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'不包含'InitializeComponent'的定义,也找不到扩展方法'InitializeComponent'接受类型为'BlankApplication.BlankPage'的第一个参数(您是否缺少using指令或程序集引用? )
'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.
屏幕截图:
推荐答案
如果主类的命名空间与.xaml文件的 x:Class 属性不同,您将获得这个错误.例如;
If your Main class's namespace is different than .xaml file's x:Class attribute, you will get this error. For instance;
您的MainPage.xaml.cs;
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();
}
}
}
您的MainPage.xaml;
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>
您将看到错误,直到将 x:class 更改为;
You're going to see the error until changing x:class to the;
x:Class ="UWPControls.MainPage"
x:Class="UWPControls.MainPage"
这篇关于BlankPage构造函数无法初始化组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!