我在使用 ac# 代码时遇到问题。首先,我不是 ac# 开发人员,我刚刚获得了这个项目,这对我来说真的很沮丧。但是供应商提供了一些代码供我们运行。那里的代码没有 Main() 函数,我猜这将是入口点,所以我试图在创建 WPF 项目时根据默认代码添加它...在 VS2012
这是代码。这是 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();
}
}
}
我得到的一个错误是:
Interfaces.Connection.ConnectionDialog' 不包含采用 0 个参数的构造函数。
不确定构造函数是什么,也不确定我需要传入哪些参数。
另一个错误:
Interfaces.Connection.ConnectionDialog' 不包含“Run”的定义,并且找不到接受 Interfaces.Connection.ConnectionDialog' 类型的第一个参数的扩展方法“Run”(您是否缺少 using 指令或程序集引用?)
每当您在 VS 中创建 WPF C# 项目时,我只是从默认代码中复制了 .Run() 。如果不需要,我可以把它拿出来。
如何在我的场景中从 Main() 初始化组件?如前所述,我不是 ac# 开发人员,所以如果你能做到一点技术含量,以便初学者 c# 的人能够理解,那真的很棒!
如果您有进一步的澄清问题,我会尽力回答。
提前致谢。