C#wpf如何从Main调用initializeComponent

时间:2013-07-03 16:38:45

标签: c# wpf xaml

我遇到了c#代码的问题。首先,我不是一个c#开发人员,我刚刚接受了这个项目,这对我来说真的很令人沮丧。 但供应商提供了一些代码供我们运行。代码没有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'而没有扩展方法'Run'接受第一个参数   可以找到类型Interfaces.Connection.ConnectionDialog'(是吗?   缺少using指令或程序集引用?)

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

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

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

提前致谢。

3 个答案:

答案 0 :(得分:6)

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

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

此外,框架会自动调用InitializeComponent。无需手动调用它。

答案 1 :(得分:1)

关于Interfaces.Connection.ConnectionDialog does not contain a definition for 'Run'错误:编译器说这是因为它是真的。您在该类中没有Run方法。你试图调用一个不存在的方法。您应该只取出app.Run();行。

要从InitializeComponent拨打Main(),看起来您已经正确地执行了此操作。您的app.InitializeComponent();行正在调用正确的方法。

答案 2 :(得分:0)

如果您对C#和您的供应商不了解,请让您的供应商提供完整,可编译的示例。这应该是交易的一部分,如果不是,请尽快确保它成为其中的一部分。

这可能听起来很奇怪,但是如果我付钱给别人卖给我一辆车,我就不会满足于获得一个行李箱和两个轮子,一些电线和一个破旧的蓝图,说明它组装后的样子。我想要一辆功能齐全的汽车。没有车,没有交易。你不应该达成“需要一些装配”的交易。您应该购买产品,而不是建筑套件。

相关问题