C# 当前上下文中不存在名称“InitializeComponent”。无法获得有关网络搜索的任何帮助
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17853898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The name 'InitializeComponent' does not exist in the current context. Cannot get any help on net searches
提问by nik
Hi I am getting an error of InitializeComponent
in my app.xaml.cs
page I have checked the net and everything but no solution works. Please help.
嗨InitializeComponent
,我的app.xaml.cs
页面出现错误,我已经检查了网络和所有内容,但没有解决方案。请帮忙。
InitializeComponent does not exist
C# file:
C#文件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Newtonsoft.Json;
namespace Miser_sApp
{
public partial class App : Application
{
/// <summary>
/// Provides easy access to the root frame of the Phone Application.
/// </summary>
/// <returns>The root frame of the Phone Application.</returns>
public PhoneApplicationFrame RootFrame { get; private set; }
/// <summary>
/// Constructor for the Application object.
/// </summary>
public App()
{
// Global handler for uncaught exceptions.
UnhandledException += Application_UnhandledException;
// Standard Silverlight initialization
InitializeComponent();
// Phone-specific initialization
InitializePhoneApplication();
// Show graphics profiling information while debugging.
if (System.Diagnostics.Debugger.IsAttached)
{
// Display the current frame rate counters.
Application.Current.Host.Settings.EnableFrameRateCounter = true;
// Show the areas of the app that are being redrawn in each frame.
//Application.Current.Host.Settings.EnableRedrawRegions = true;
// Enable non-production analysis visualization mode,
// which shows areas of a page that are handed off to GPU with a colored overlay.
//Application.Current.Host.Settings.EnableCacheVisualization = true;
// Disable the application idle detection by setting the UserIdleDetectionMode property of the
// application's PhoneApplicationService object to Disabled.
// Caution:- Use this under debug mode only. Application that disables user idle detection will continue to run
// and consume battery power when the user is not using the phone.
PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled;
}
}
// Code to execute when the application is launching (eg, from Start)
// This code will not execute when the application is reactivated
private void Application_Launching(object sender, LaunchingEventArgs e)
{
}
// Code to execute when the application is activated (brought to foreground)
// This code will not execute when the application is first launched
private void Application_Activated(object sender, ActivatedEventArgs e)
{
}
// Code to execute when the application is deactivated (sent to background)
// This code will not execute when the application is closing
private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
}
// Code to execute when the application is closing (eg, user hit Back)
// This code will not execute when the application is deactivated
private void Application_Closing(object sender, ClosingEventArgs e)
{
}
// Code to execute if a navigation fails
private void RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
{
if (System.Diagnostics.Debugger.IsAttached)
{
// A navigation has failed; break into the debugger
System.Diagnostics.Debugger.Break();
}
}
// Code to execute on Unhandled Exceptions
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
if (System.Diagnostics.Debugger.IsAttached)
{
// An unhandled exception has occurred; break into the debugger
System.Diagnostics.Debugger.Break();
}
}
#region Phone application initialization
// Avoid double-initialization
private bool phoneApplicationInitialized = false;
// Do not add any additional code to this method
private void InitializePhoneApplication()
{
if (phoneApplicationInitialized)
return;
// Create the frame but don't set it as RootVisual yet; this allows the splash
// screen to remain active until the application is ready to render.
RootFrame = new PhoneApplicationFrame();
RootFrame.Navigated += CompleteInitializePhoneApplication;
// Handle navigation failures
RootFrame.NavigationFailed += RootFrame_NavigationFailed;
// Ensure we don't initialize again
phoneApplicationInitialized = true;
}
// Do not add any additional code to this method
private void CompleteInitializePhoneApplication(object sender, NavigationEventArgs e)
{
// Set the root visual to allow the application to render
if (RootVisual != RootFrame)
RootVisual = RootFrame;
// Remove this handler since it is no longer needed
RootFrame.Navigated -= CompleteInitializePhoneApplication;
}
#endregion
}
}
XAML file:
XAML 文件:
<Application
x:Class="Miser_sApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone">
<!--Application Resources-->
<Application.Resources>
</Application.Resources>
<Application.ApplicationLifetimeObjects>
<!--Required object that handles lifetime events for the application-->
<shell:PhoneApplicationService
Launching="Application_Launching" Closing="Application_Closing"
Activated="Application_Activated" Deactivated="Application_Deactivated"/>
</Application.ApplicationLifetimeObjects>
</Application>
I have uploaded the app.xaml
contents.
I have not made any changes in it.
我已经上传了app.xaml
内容。我没有对其进行任何更改。
采纳答案by Rich Bianco
There are two potential causes of this.
这有两个潜在的原因。
The most common is the x:Classdoesn't match up with the MainPage.xaml namespace. Make sure that x:Class in MainPage.xaml has the correct namespace.
The second most common cause of this problem is that the "Build Action" is not set to "Page" for MainPage.xaml!
最常见的是x:Class与 MainPage.xaml 命名空间不匹配。确保 MainPage.xaml 中的 x:Class 具有正确的命名空间。
此问题的第二个最常见原因是 MainPage.xaml 的“构建操作”未设置为“页面”!
回答by Soonts
- Ensure the BuildAction of your App.xaml is set to "ApplicationDefinition"
- Delete the "obj" folder in the project, rebuild.
- If the problem persist, get rid of the "_" character in your namespace.
- 确保 App.xaml 的 BuildAction 设置为“ApplicationDefinition”
- 删除项目中的“obj”文件夹,重建。
- 如果问题仍然存在,请去掉命名空间中的“_”字符。
回答by Roland
I had the same build error but the build action was already set to Page. Trying Build Action set to ApplicationDefinition (error: there can only one instance of that), and setting it back to Page, fixed the build error. Sounds like black magic, but it worked for me.
我有同样的构建错误,但构建操作已经设置为 Page。尝试将 Build Action 设置为 ApplicationDefinition(错误:只能有一个实例),并将其设置回 Page,修复了构建错误。听起来像黑魔法,但它对我有用。
回答by gcyoung
Here's one other possibility, after exhausting all the above (as well as a few others scattered about the internet): make sure that your Startup object is correctly set to [Project].App in your Project Properties > Application tab.
在用尽上述所有内容(以及散布在互联网上的其他一些内容)之后,还有另一种可能性:确保在“项目属性”>“应用程序”选项卡中将 Startup 对象正确设置为 [Project].App。
I had renamed some namespaces, and somewhere in the process VS set the Startup object to "(not set)".
我重命名了一些命名空间,并且在 VS 进程中的某个地方将 Startup 对象设置为“(未设置)”。
回答by debeka
My solution was to set the Build Action property of Package.appxmanifest to AppxManifest. :)
我的解决方案是将 Package.appxmanifest 的 Build Action 属性设置为 AppxManifest。:)
回答by Reader Man San
This is the same question and answer here: The name 'InitializeComponent' does not exist in the current context
这是相同的问题和答案: 当前上下文中不存在名称“InitializeComponent”
You might get this error when you import a class from another project, or change the path of the xaml file, or the namespace of either the xaml or behind .cs file.
当您从另一个项目导入类时,或更改 xaml 文件的路径或 xaml 或 .cs 文件后面的命名空间时,您可能会收到此错误。
One:It might have a namespace that is not the same as what you have in you new project
一:它的命名空间可能与您在新项目中的命名空间不同
namespace TrainerB.MVC.Forms
{
public partial class AboutDeveloper : ContentPage
{
public AboutDeveloper()
{
InitializeComponent();
}
}
}
As you can see the name space in the imported file begins with the old project name: "TrainerB", but your new project might have a different name, so just change it to the correct new project name, in both the .xaml file and the behind .cs file.
正如您所看到的,导入文件中的命名空间以旧项目名称开头:"TrainerB",但您的新项目可能具有不同的名称,因此只需在 .xaml 文件和 .xaml 文件中将其更改为正确的新项目名称即可后面的 .cs 文件。
Two:
二:
change the properties of the .xaml file to:
将 .xaml 文件的属性更改为:
Build Action:Embedded Resource
构建操作:嵌入式资源
Custom Tool:MSBuild:UpdateDesignTimeXaml
自定义工具:MSBuild:UpdateDesignTimeXaml
回答by Nahid
1) In the xaml file, check the x:Name of the main layout. Rename it 2) Compile. It should throw errors 3) Go back to the xaml file and give the same class name as it associated code behind file has (.cs file) Also include the namespace. eg: if namespace is "X" and class name is "Y", x:Name = "X.Y" 4) Compile. It should work.
1) 在 xaml 文件中,检查主布局的 x:Name。重命名它 2) 编译。它应该抛出错误 3) 返回到 xaml 文件并给出与它关联的代码隐藏文件具有相同的类名(.cs 文件)还包括命名空间。例如:如果命名空间是“X”,类名是“Y”,x:Name = “XY” 4) 编译。它应该工作。
回答by Terry Mosoma
This worked for me, Try Ctrl+Son the pages that give you this error. The error came about, when my visual studio crashed(restarted). The pages I was working on(before the restart) didnt failed to build. Which lead me to think the didnt save correctly. Hence, Ctrl+S. That solved my issue.
这对我有用,请在出现此错误的页面上尝试Ctrl+ S。当我的视觉工作室崩溃(重新启动)时,出现了错误。我正在处理的页面(在重新启动之前)没有构建失败。这让我认为没有正确保存。因此,Ctrl+ S。那解决了我的问题。
回答by pollaris
After successful builds, when the error occurs, close VS, delete the hidden .vs folder in your project (this clears intellisense). Open VS, the error is gone.
成功构建后,出现错误时,关闭VS,删除项目中隐藏的.vs文件夹(这将清除intellisense)。打开VS,错误消失了。
回答by Morse
In my case, I had set build action of XAML page to Embedded Resource
,
reverting it to Page
fixed the issue.
就我而言,我已将 XAML 页面的构建操作设置为Embedded Resource
,将其还原以Page
解决问题。