简体   繁体   English

C#wpf如何从Main调用initializeComponent

[英]C# wpf How do I call initializeComponent from Main

I am having an issue with ac# code. 我在使用ac#代码时遇到问题。 First, I'm not ac# developer, I've just been given this project and it's been really frustrating for me. 首先,我不是ac#开发人员,我刚刚得到了这个项目,这确实让我感到沮丧。 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 那里的代码没有Main()函数,我想这是切入点,所以我试图在VS2012中创建WPF项目时根据默认代码添加它。

Here is the code. 这是代码。 This is the C# code of a xaml window. 这是xaml窗口的C#代码。 At the very end, you can see I am trying to add Main() to call initializeComponent. 在最后,您可以看到我正在尝试添加Main()来调用initializeComponent。 But I am receiving some errors. 但是我收到一些错误。

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'的定义,并且找不到扩展方法'Run'接受类型为Interfaces.Connection.ConnectionDialog'的第一个参数(您是否缺少using指令或程序集引用?)

I just had .Run() copied over from the default code whenever you create a WPF C# project in VS. 每当您在VS中创建WPF C#项目时,都只是从默认代码中复制了.Run()。 If its not needed, I can take it out. 如果不需要,我可以将其取出。

How can I initializeComponent from the Main() in my scenario? 在我的场景中,如何从Main()初始化InitialComponent? As said, I'm not ac# 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! 如前所述,我不是ac#开发人员,所以如果您可以尽量减少技术方面的知识,以便初学者c#的人都能理解,那的确非常好!

And if you have further questions on clarification, I'll try my best to answer. 如果您还有其他需要澄清的问题,我会尽力回答。

Thanks in advance. 提前致谢。

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. 此外,在WPF应用程序中不需要Main方法。 When you just create a new WPF project, you don't have a Main method either. 当您仅创建一个新的WPF项目时,您也没有Main方法。 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. 如果要在应用程序启动时执行某些操作,则App.xaml文件包含一个具有Startup事件的Application对象。 Use this instead of a Main method. 使用它代替Main方法。

Also, InitializeComponent should automatically be called by the framework. 另外,框架应自动调用InitializeComponent There is no need to call it manually. 无需手动调用。

With regard to the Interfaces.Connection.ConnectionDialog does not contain a definition for 'Run' error: the compiler is saying this because it's true. 关于Interfaces.Connection.ConnectionDialog does not contain a definition for 'Run'错误Interfaces.Connection.ConnectionDialog does not contain a definition for 'Run' :编译器之所以这样说是因为它是正确的。 You don't have a Run method in that class. 该类中没有Run方法。 You're trying to call a nonexistent method. 您正在尝试调用不存在的方法。 You should probably just take out the app.Run(); 您可能应该只取出app.Run(); line. 线。

To call InitializeComponent from Main() , it looks like you're already doing it correctly. 要从Main()调用InitializeComponent ,似乎您已经正确地进行了操作。 Your app.InitializeComponent(); 您的app.InitializeComponent(); line is calling the correct method. 行正在调用正确的方法。

If you don't know enough C# and your vendor does, let your vendor supply a complete, compilable example . 如果您对C#的了解不够,而您的供应商却不知道,请让您的供应商提供一个完整的,可编译的示例 That should be part of the deal, if it's not, make sure it gets part of it ASAP. 那应该是交易的一部分,如果不是的话,请确保它尽快得到一部分。

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. 您应该购买产品 ,而不是建筑套件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM