简体   繁体   中英

C# wpf How do I call initializeComponent from Main

I am having an issue with ac# code. First, I'm not ac# 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

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.

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.

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?)

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.

How can I initializeComponent from the Main() in my scenario? 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!

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. 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.

Also, InitializeComponent should automatically be called by the framework. 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. 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.

To call InitializeComponent from Main() , it looks like you're already doing it correctly. Your 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 . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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