XAML C# wpf如何从Main调用initializeComponent

uxhixvfz  于 11个月前  发布在  C#
关注(0)|答案(3)|浏览(140)

我有一个C#代码的问题。首先,我不是一个C#开发人员,我刚刚得到这个项目,这对我来说真的很沮丧。但供应商提供了一些代码给我们运行。他们的代码没有Main()函数,我猜这将是入口点,所以我试图添加它的基础上默认代码时,你创建一个WPF项目.在VS 2012
下面是代码。这是一个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“的定义,并且找不到接受类型Interfaces.Connection.ConnectionDialog”的第一个参数的扩展方法“Run”(是否缺少using指令或程序集引用?)
我刚刚从VS中创建WPF C#项目时的默认代码中复制了.Run()。如果不需要,我可以将其删除。
在我的场景中,我怎么能从Main()中得到InitializeComponent呢?正如我所说的,我不是一个C#开发人员,所以如果你能尽可能少地使用技术,这样一个初学者C#的人就能理解,那就太棒了!
如果你有进一步的澄清问题,我会尽力回答。
先谢了。

42fyovps

42fyovps1#

首先,你发布的代码看起来像是生成的代码,根本不应该手动操作。
此外,在WPF应用程序中不需要Main方法。当您刚刚创建一个新的WPF项目时,您也没有Main方法。框架会在后台为您生成Main方法,并自动显示主窗口。如果您想在应用程序启动时执行某些操作,App.xaml文件包含一个Application对象,它有一个Startup事件。使用这个代替Main方法。
另外,InitializeComponent应该由框架自动调用,不需要手动调用。

oxcyiej7

oxcyiej72#

关于Interfaces.Connection.ConnectionDialog does not contain a definition for 'Run'错误:编译器这样说是因为它是真的。你在那个类中没有Run方法。你试图调用一个不存在的方法。你可能应该删除app.Run();行。
要从Main()调用InitializeComponent,看起来你已经正确地做了。你的app.InitializeComponent();行正在调用正确的方法。

nwlqm0z1

nwlqm0z13#

如果你不知道足够的C#,而你的供应商知道,让你的供应商提供一个 * 完整的,可编译的例子 *。这应该是交易的一部分,如果不是,确保它尽快得到它的一部分。
这听起来可能有些奇怪,但如果我付钱让别人卖给我一辆车,我不会满足于得到一个行李箱、两个轮子、一些电线和一张组装后外观的破旧蓝图。我想要一辆功能齐全的车。没有车,就没有交易。你不应该做一笔写着“需要一些组装”的交易。你应该买一个产品,而不是一个建筑工具包。

相关问题