I am having an issue with a c# code. First, I'm not a c# developer, I've just been given this project and it's been really frustrating for me. But the vendor has provided some code for us to run. There code did not have Main() function, which I guess would be the entry point, so I am trying to add it based on the default code when you create a WPF project...in VS2012
我有一个c#代码的问题。首先,我不是c#开发人员,我刚刚得到这个项目,这让我很沮丧。但是供应商提供了一些代码让我们运行。那里的代码没有Main()函数,我猜应该是入口点,所以在创建WPF项目时,我试图基于默认代码添加它……在VS2012
Here is the code. This is the C# code of a xaml window. At the very end, you can see I am trying to add Main() to call initializeComponent. But I am receiving some errors.
这是代码。这是xaml窗口的c#代码。最后,您可以看到我正在尝试添加Main()来调用initializeComponent。但是我收到了一些错误。
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Automation;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Effects;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Media.TextFormatting;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Shell;
namespace Interfaces.Connection {
/// <summary>
/// ConnectionDialog
/// </summary>
public partial class ConnectionDialog : System.Windows.Window, System.Windows.Markup.IComponentConnector {
#line 30 "..\..\MainWindow.xaml"
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
internal System.Windows.Controls.TextBox txtConnectionServer;
#line default
#line hidden
#line 33 "..\..\MainWindow.xaml"
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
internal System.Windows.Controls.TextBox txtUsername;
#line default
#line hidden
#line 36 "..\..\MainWindow.xaml"
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
internal System.Windows.Controls.TextBox txtContext;
#line default
#line hidden
#line 39 "..\..\MainWindow.xaml"
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
internal System.Windows.Controls.PasswordBox txtPassword;
#line default
#line hidden
#line 45 "..\..\MainWindow.xaml"
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
internal System.Windows.Controls.Button btnOk;
#line default
#line hidden
#line 46 "..\..\MainWindow.xaml"
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
internal System.Windows.Controls.Button btnCancel;
#line default
#line hidden
private bool _contentLoaded;
/// <summary>
/// InitializeComponent
/// </summary>
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
public void InitializeComponent() {
if (_contentLoaded) {
return;
}
_contentLoaded = true;
System.Uri resourceLocater = new System.Uri("/Interfaces.Connection;component/mainwindow.xaml", System.UriKind.Relative);
#line 1 "..\..\MainWindow.xaml"
System.Windows.Application.LoadComponent(this, resourceLocater);
#line default
#line hidden
}
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Design", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes")]
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily")]
void System.Windows.Markup.IComponentConnector.Connect(int connectionId, object target) {
switch (connectionId)
{
case 1:
this.txtConnectionServer = ((System.Windows.Controls.TextBox)(target));
return;
case 2:
this.txtUsername = ((System.Windows.Controls.TextBox)(target));
return;
case 3:
this.txtContext = ((System.Windows.Controls.TextBox)(target));
return;
case 4:
this.txtPassword = ((System.Windows.Controls.PasswordBox)(target));
return;
case 5:
this.btnOk = ((System.Windows.Controls.Button)(target));
#line 45 "..\..\MainWindow.xaml"
this.btnOk.Click += new System.Windows.RoutedEventHandler(this.btnOk_Click);
#line default
#line hidden
return;
case 6:
this.btnCancel = ((System.Windows.Controls.Button)(target));
#line 46 "..\..\MainWindow.xaml"
this.btnCancel.Click += new System.Windows.RoutedEventHandler(this.btnCancel_Click);
#line default
#line hidden
return;
}
this._contentLoaded = true;
}
/// <summary>
/// Application Entry Point.
/// </summary>
[System.STAThreadAttribute()]
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
public static void Main()
{
Interfaces.Connection.ConnectionDialog app = new Interfaces.Connection.ConnectionDialog();
app.InitializeComponent();
app.Run();
}
}
}
}
One error I am getting is:
我得到的一个错误是:
Interfaces.Connection.ConnectionDialog' does not contain a constructor that takes 0 arguments.
Interfaces.Connection。ConnectionDialog'不包含接受0参数的构造函数。
Not sure what constructor would be and not sure what arguments I need to pass in.
不确定构造函数是什么,也不确定需要传入哪些参数。
Another error:
另一个错误:
Interfaces.Connection.ConnectionDialog' does not contain a defintion for 'Run' and no extension method 'Run' accepting a first argument of type Interfaces.Connection.ConnectionDialog' could be found(are you missing a using directive or an assembly reference?)
Interfaces.Connection。ConnectionDialog'不包含'Run'的定义,也不包含接受类型Interfaces.Connection的第一个参数的扩展方法'Run'。可以找到ConnectionDialog'(是否缺少使用指令或程序集引用?)
I just had .Run() copied over from the default code whenever you create a WPF C# project in VS. If its not needed, I can take it out.
当你在vs中创建一个WPF c#项目,如果不需要的话,我可以把它拿出来。run()从默认代码中复制过来。
How can I initializeComponent from the Main() in my scenario? As said, I'm not a c# dev, so if you can please be as little as technical so that a beginner c# person would understand, that would be really really great!
在我的场景中,如何从Main()初始化ecomponent ?如前所述,我不是一个c#开发人员,所以如果您能尽可能少地使用c#,以便初学者能够理解,那将是非常棒的!
And if you have further questions on clarification, I'll try my best to answer.
如果你对澄清有任何疑问,我将尽力回答。
Thanks in advance.
提前谢谢。
3 个解决方案
#1
6
First, the code you have posted looks like generated code. It should not be touched manually at all.
首先,您所发布的代码看起来像生成的代码。完全不应该手动触摸它。
In addition, you do not need a Main
method in a WPF application. When you just create a new WPF project, you don't have a Main
method either. The framework generates that for you behind the scenes and shows the main window automatically. If you want to do something on application startup, the App.xaml
file holds an Application
object which has a Startup
event. Use this instead of a Main
method.
此外,在WPF应用程序中不需要主方法。当您刚刚创建一个新的WPF项目时,您也没有一个主方法。框架会在幕后为您生成这些内容,并自动显示主窗口。如果您希望在应用程序启动时执行某些操作,那么App.xaml文件包含一个具有启动事件的应用程序对象。用这个代替主方法。
Also, InitializeComponent
should automatically be called by the framework. There is no need to call it manually.
而且,InitializeComponent应该由框架自动调用。不需要手动调用它。
#2
1
With regard to the Interfaces.Connection.ConnectionDialog does not contain a definition for 'Run'
error: the compiler is saying this because it's true. You don't have a Run method in that class. You're trying to call a nonexistent method. You should probably just take out the app.Run();
line.
关于接口。连接。ConnectionDialog不包含“运行”错误的定义:编译器之所以这么说,是因为它是正确的。在那个类中没有Run方法。你在尝试调用一个不存在的方法。您应该取出app.Run();线。
To call InitializeComponent
from Main()
, it looks like you're already doing it correctly. Your app.InitializeComponent();
line is calling the correct method.
要从Main()调用InitializeComponent,看起来您已经做对了。你app.InitializeComponent();line正在调用正确的方法。
#3
0
If you don't know enough C# and your vendor does, let your vendor supply a complete, compilable example. That should be part of the deal, if it's not, make sure it gets part of it ASAP.
如果您不了解c#,并且您的供应商不了解c#,请让您的供应商提供一个完整的、可编译的示例。这应该是协议的一部分,如果不是的话,确保它能尽快得到一部分。
That may sound strange, but if I pay somebody to sell me a car, I would not be satisfied getting a trunk and two wheels, some wire and a shabby blueprint of how it might look once assembled. I want a fully functional car. No car, no deal. You should not make a deal that reads "some assembly required". You should buy a product, not a construction kit.
这听起来可能有点奇怪,但如果我花钱请人卖给我一辆车,我肯定不会满足于得到一个箱子、两个*、一些电线和一张破旧的蓝图——一旦组装起来,它会是什么样子。我想要一辆功能齐全的汽车。没有汽车,没有交易。你不应该做一个“需要组装”的交易。你应该买一个产品,而不是一个建筑材料。
#1
6
First, the code you have posted looks like generated code. It should not be touched manually at all.
首先,您所发布的代码看起来像生成的代码。完全不应该手动触摸它。
In addition, you do not need a Main
method in a WPF application. When you just create a new WPF project, you don't have a Main
method either. The framework generates that for you behind the scenes and shows the main window automatically. If you want to do something on application startup, the App.xaml
file holds an Application
object which has a Startup
event. Use this instead of a Main
method.
此外,在WPF应用程序中不需要主方法。当您刚刚创建一个新的WPF项目时,您也没有一个主方法。框架会在幕后为您生成这些内容,并自动显示主窗口。如果您希望在应用程序启动时执行某些操作,那么App.xaml文件包含一个具有启动事件的应用程序对象。用这个代替主方法。
Also, InitializeComponent
should automatically be called by the framework. There is no need to call it manually.
而且,InitializeComponent应该由框架自动调用。不需要手动调用它。
#2
1
With regard to the Interfaces.Connection.ConnectionDialog does not contain a definition for 'Run'
error: the compiler is saying this because it's true. You don't have a Run method in that class. You're trying to call a nonexistent method. You should probably just take out the app.Run();
line.
关于接口。连接。ConnectionDialog不包含“运行”错误的定义:编译器之所以这么说,是因为它是正确的。在那个类中没有Run方法。你在尝试调用一个不存在的方法。您应该取出app.Run();线。
To call InitializeComponent
from Main()
, it looks like you're already doing it correctly. Your app.InitializeComponent();
line is calling the correct method.
要从Main()调用InitializeComponent,看起来您已经做对了。你app.InitializeComponent();line正在调用正确的方法。
#3
0
If you don't know enough C# and your vendor does, let your vendor supply a complete, compilable example. That should be part of the deal, if it's not, make sure it gets part of it ASAP.
如果您不了解c#,并且您的供应商不了解c#,请让您的供应商提供一个完整的、可编译的示例。这应该是协议的一部分,如果不是的话,确保它能尽快得到一部分。
That may sound strange, but if I pay somebody to sell me a car, I would not be satisfied getting a trunk and two wheels, some wire and a shabby blueprint of how it might look once assembled. I want a fully functional car. No car, no deal. You should not make a deal that reads "some assembly required". You should buy a product, not a construction kit.
这听起来可能有点奇怪,但如果我花钱请人卖给我一辆车,我肯定不会满足于得到一个箱子、两个*、一些电线和一张破旧的蓝图——一旦组装起来,它会是什么样子。我想要一辆功能齐全的汽车。没有汽车,没有交易。你不应该做一个“需要组装”的交易。你应该买一个产品,而不是一个建筑材料。