簡體   English   中英

C#wpf如何從Main調用initializeComponent

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

我在使用ac#代碼時遇到問題。 首先,我不是ac#開發人員,我剛剛得到了這個項目,這確實讓我感到沮喪。 但是供應商已經提供了一些代碼供我們運行。 那里的代碼沒有Main()函數,我想這是切入點,所以我試圖在VS2012中創建WPF項目時根據默認代碼添加它。

這是代碼。 這是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()初始化InitialComponent? 如前所述,我不是ac#開發人員,所以如果您可以盡量減少技術方面的知識,以便初學者c#的人都能理解,那的確非常好!

如果您還有其他需要澄清的問題,我會盡力回答。

提前致謝。

首先,您發布的代碼看起來像生成的代碼。 完全不應手動觸摸它。

此外,在WPF應用程序中不需要Main方法。 當您僅創建一個新的WPF項目時,您也沒有Main方法。 該框架在后台為您生成該文件,並自動顯示主窗口。 如果要在應用程序啟動時執行某些操作,則App.xaml文件包含一個具有Startup事件的Application對象。 使用它代替Main方法。

另外,框架應自動調用InitializeComponent 無需手動調用。

關於Interfaces.Connection.ConnectionDialog does not contain a definition for 'Run'錯誤Interfaces.Connection.ConnectionDialog does not contain a definition for 'Run' :編譯器之所以這樣說是因為它是正確的。 該類中沒有Run方法。 您正在嘗試調用不存在的方法。 您可能應該只取出app.Run(); 線。

要從Main()調用InitializeComponent ,似乎您已經正確地進行了操作。 您的app.InitializeComponent(); 行正在調用正確的方法。

如果您對C#的了解不夠,而您的供應商卻不知道,請讓您的供應商提供一個完整的,可編譯的示例 那應該是交易的一部分,如果不是的話,請確保它盡快得到一部分。

這聽起來可能很奇怪,但是如果我付錢給我賣車,我會不滿足於得到一個后備箱和兩個輪子,一些金屬絲和一張破舊的藍圖,說明它們組裝后的外觀。 我想要一輛功能齊全的汽車。 沒有車,沒有交易。 您不應做出“需要一些組裝”的交易。 您應該購買產品 ,而不是建築套件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM