C#wpf如何从Main调用initializeComponent

Scr*_*Guy 1 c# wpf xaml

我遇到了ac#code的问题.首先,我不是开发人员,我刚刚接受了这个项目,这对我来说真的很令人沮丧.但供应商提供了一些代码供我们运行.代码没有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();
    }
}
Run Code Online (Sandbox Code Playgroud)

}

我得到的一个错误是:

Interfaces.Connection.ConnectionDialog'不包含带0个参数的构造函数.

不确定构造函数是什么,不确定我需要传递哪些参数.

另一个错误:

Interfaces.Connection.ConnectionDialog'不包含'Run'的定义,也没有扩展方法'Run'接受类型Interfaces.Connection.ConnectionDialog'的第一个参数(你是否缺少using指令或程序集引用?)

每当你在VS中创建一个WPF C#项目时,我就会从默认代码中复制.Run().如果不需要,我可以把它拿出来.

如何在我的场景中从Main()初始化Component?如上所述,我不是ac#dev,所以如果你可以请尽可能少的技术,以便初学c#的人会理解,那真的很棒!

如果您有进一步的澄清问题,我会尽力回答.

提前致谢.

mar*_*rio 6

首先,您发布的代码看起来像生成的代码.它根本不应该手动触摸.

此外,您不需要MainWPF应用程序中的方法.当您刚刚创建一个新的WPF项目时,您也没有Main方法.框架在幕后为您生成,并自动显示主窗口.如果要在应用程序启动时执行某些操作,该App.xaml文件将包含一个Application具有Startup事件的对象.使用此而不是Main方法.

此外,InitializeComponent应该由框架自动调用.无需手动调用它.