如果我在Visual Studio 2010 SP1中创建一个新项目并选择"WPF应用程序"并尝试生成生成的应用程序,我会收到错误
当前上下文中不存在名称"InitializeComponent".
今天早上,当我尝试构建当前项目时,我遇到了类似的错误.昨天,编译和运行它没有问题.
我创建了一个新项目,每当我编译项目时都会收到错误.我刚刚将项目发送给了一位同事,他刚编译时没有任何错误.
怎么了?
在我正在研究FxCop的项目中,我向Windows窗体设计器生成的InitializeComponent()方法显示了很多(我的意思是400多个)错误.大多数错误只是标签的Text属性的赋值.
我想在源代码中禁止这些方法,因此我将FxCop生成的抑制代码复制到AssemblyInfo.cs中,但它不起作用.
这是FxCop复制到剪贴板的属性.
[module: SuppressMessage("Microsoft.Globalization",
"CA1303:DoNotPassLiteralsAsLocalizedParameters",
Scope = "member",
Target = "WindowsClient.MainForm.InitializeComponent():System.Void",
MessageId = "System.Windows.Forms.Control.set_Text(System.String)")]
Run Code Online (Sandbox Code Playgroud)
任何人都知道正确的属性来压制这些消息?
PS:我使用的是Visual Studio 2005,C#,FxCop 1.36 beta.
code-analysis fxcop initializecomponent suppression visual-studio
在设计时修改ListView的列标题时,设计器会生成在运行时序列化列标题的代码:
private void InitializeComponent()
{
this.listView1 = new System.Windows.Forms.ListView();
this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.columnHeader1,
this.columnHeader2
});
}
Run Code Online (Sandbox Code Playgroud)
表单设计者如何知道它应该为每个列调用构造函数,然后调用ListView的Columns属性的AddRange方法?我需要这个像我正在写的UserControl ListView.
场景:我正在重构我们的一个应用程序以使用Nhibernate,并在几周之前遇到了这个问题.这个问题最初是与Nhibernate和Castle一起解决的,所以他们都重新编译了[assembly: AllowPartiallyTrustedCallers]
.但是,在对UI和代码库进行一些更改后,此错误再次出现.另外值得注意的是,我从Form_Main以编程方式控制加载我的用户控件.
问题:每当生成用户控件时,我都会收到以下错误.如果我注释掉加载,程序将运行.当我调试它结束于自动生成的InitializeComponent()函数.请注意,我无法进入该功能.
System.Security.SecurityException was unhandled
Message="That assembly does not allow partially trusted callers."
Source="A"
GrantedSet=""
PermissionState=""
RefusedSet=""
Url="file:///C:/Documents and Settings/ID/Desktop/A-NHIB2/bin/Debug/A.EXE"
StackTrace:
at A.UserControlCyber.InitializeComponent()
at A.UserControlCyber..ctor() in C:\Documents and Settings\ID\Desktop\A-NHIB2\UserControl_Cyber.cs:line 34
at A.FormMain.FormMainLoad(Object sender, EventArgs e) in C:\Documents and Settings\ID\Desktop\A-NHIB2\Form_Main.cs:line 30
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at …
Run Code Online (Sandbox Code Playgroud) 我正在尝试自定义Windows窗体设计器的代码生成InitializeComponent
.MSDN文章"在.NET Framework可视化设计器中自定义代码生成"包含"控制代码生成"部分,该部分解释了如何执行此操作的基础知识.
我在上面的文章中密切关注了一个例子:
//using System.ComponentModel.Design.Serialization;
class SomeFormSerializer : CodeDomSerializer
{
public override object Serialize(IDesignerSerializationManager manager,
object value)
{
// first, let the default serializer do its work:
var baseSerializer = (CodeDomSerializer)manager.GetSerializer(
typeof(Form).BaseType, typeof(CodeDomSerializer));
object codeObject = baseSerializer.Serialize(manager, value);
// then, modify the generated CodeDOM -- add a comment as the 1st line:
if (codeObject is CodeStatementCollection)
{
var statements = (CodeStatementCollection)codeObject;
statements.Insert(0, new CodeCommentStatement("CODEDOM WAS HERE"));
}
// finally, return the modified CodeDOM:
return …
Run Code Online (Sandbox Code Playgroud) 我的WPF用户控件的公共接口包含自动生成的InitializeComponent方法(包含在部分类中).这对我来说是一个惊喜,因为我预计这样的内部东西是私密的.
有没有办法从用户控件公共接口中删除InitializeComponent?
我有一个带有完整GUI的程序,需要大约750毫秒来加载,这要归功于InitializeComponent()
.经过一些研究,似乎有一些技术可以改善.NET在整个InitializeComponent
部分中所用的时间.这些包括:
1:使用NGen或类似产品.
2:使用Speed-optimize Windows Forms应用程序中显示的多线程
3:其他技术,例如在添加它们之前设置控件属性,如优化InitializeComponent所示(需要1秒!).
不幸的是(1)只改进了我的情况大约20%,并且(2)和(3)实现起来很费时间,并且牺牲了完整GUI设计者的便利性.
是否有任何自动化解决方案可以直接获取源代码,并生成更短,更高效的解决方案InitializeComponent()
?
InitializeComponent
在表单上设置组件,但是对于我创建的用户控件,它调用默认构造函数,但我想在用户控件上调用我自己的构造函数(带参数)。样板代码说不要编辑内容,那么最好的方法是什么?
我在XP英文机器和XP日本机器上运行相同的UI代码.出于某种原因 - 这两台机器之间的行为是不同的:
在日语机器上,OnResize事件在InitializeComponent调用期间被触发(我认为是来自ResumeLayout函数).在英语机器上它没有.
我有一个OnResize的覆盖方法,它改变我定义的私有变量的值.由于组件未完全加载(Ctor仍在运行),这些变量为空,我得到一个异常.
我可以通过检查变量是否为null来修复代码,但我想了解为什么它首先被调用以及为什么它只在这台机器上发生.
在我通过网络搜索期间,我发现了其他人发布的相同问题,但没有解决方案(http://social.msdn.microsoft.com/Forums/en/winforms/thread/95aefae4-45d8-4ac5-a8f2- 6e2142dfb631).
我已经开始在c#2008上的XP 32位Windows操作系统上制作C#程序,现在我试图在Windows 7 64位操作系统中打开它.出现的问题是我无法在64位系统上运行c#2010中的程序,它不会超过Initialize Component(); 我的代码中的行.它出现了一个错误,"BadImageFormatException未处理",我认为这是因为我打开它的系统更改.对此有任何帮助我们都很棒.
谢谢
c# operating-system initializecomponent badimageformatexception
我有一个Xamarin便携式项目。
我调试的Xaml页面完全空白,在Android和IOS上的页面上都看不到任何组件。
我怎样才能解决这个问题?
注意:没有任何错误消息,页面正在打开,我看不到任何内容。
发生此错误后出现了问题。当我修复它时,
尽管调试的页面在InitializeComponent错误之前仍在工作,但它们打开的页面还是空的。
任何帮助将不胜感激。
这是我的xaml:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="AcikAkademi3.Layoutlar.GridOrnek3">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label BackgroundColor="Red" Text="0,0" Grid.Column="0" Grid.Row="0">
</Label>
<Label BackgroundColor="Blue" Text="1,0" Grid.Column="1" Grid.Row="0">
</Label>
<Label BackgroundColor="Yellow" Text="Aç?k Akademi" Grid.Column="2" Grid.Row="0"></Label>
<Label BackgroundColor="White" Text="0,1" Grid.Column="0" Grid.Row="1">
</Label>
<Label BackgroundColor="Silver" Text="1,1" Grid.Column="1" Grid.Row="1">
</Label>
<Label BackgroundColor="Lime" Text="2,1" Grid.Column="2" Grid.Row="1">
</Label>
</Grid>
</ContentPage>
Run Code Online (Sandbox Code Playgroud)
这是我的CS:
using System;
using System.Collections.Generic;
using …
Run Code Online (Sandbox Code Playgroud) xaml initializecomponent xamarin.ios xamarin.android xamarin
所以我完成了我的程序,并尝试在顶部编辑一些小东西,并开始收到错误,无法更改它们.这是代码:
namespace FinalProject
{
public partial class Form : Form
{
public Form();
{
InitializeComponent();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我更改了Form:Form部分,然后它InitializeComponent();
变成了一个错误.试图改变它,它仍然显示错误.撤消不是一个选项,我相信这是一个简单的解决方案.
因此,突然间,常见错误“名称 InitializeComponent 在当前上下文中不存在”出现在我所有解决方案中的每个 xaml.cs 类中,我已经尝试了针对此问题的多种常见解决方案,但似乎都不起作用。
\n继承内容视图的 xaml.cs:
\nusing System;\nusing System.Collections.Generic;\nusing Xamarin.Forms;\n\nnamespace FirstApp.Renderers\n{\n public partial class CustomExpansibleMenu : ContentView\n {\n public CustomExpansibleMenu()\n {\n//Error here\n InitializeComponent();\n }\n }\n}\n
Run Code Online (Sandbox Code Playgroud)\n从内容页继承的 xaml.cs:
\nusing System;\nusing System.Collections.Generic;\nusing System.Threading.Tasks;\nusing FirstApp.Services;\nusing Xamarin.Essentials;\nusing Xamarin.Forms;\n\nnamespace FirstApp.Pages\n{\n public partial class ChatMenuPage : ContentPage\n {\n private NavigationService navigation;\n public static double numPixelsW;\n public static double numPixelsH;\n\n public ChatMenuPage()\n {\n numPixelsH = DeviceDisplay.MainDisplayInfo.Height / DeviceDisplay.MainDisplayInfo.Density;\n numPixelsW = DeviceDisplay.MainDisplayInfo.Width / DeviceDisplay.MainDisplayInfo.Density;\n\n navigation = new NavigationService();\n\n//Error here \n InitializeComponent();\n\n//Another error here, …
Run Code Online (Sandbox Code Playgroud) c# ×8
winforms ×4
.net ×2
designer ×2
wpf ×2
xamarin ×2
xaml ×2
.net-3.5 ×1
fxcop ×1
onresize ×1
optimization ×1
performance ×1
suppression ×1
xamarin.ios ×1