wpf 错误:在类型上找不到匹配的构造函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41327777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:58:33  来源:igfitidea点击:

Error: No matching constructor found on type

c#wpfxaml

提问by S Andrew

I have a WPF application where I am using multiple forms. There is one main form which gets opened when we start the application which is know as MainWindow.xaml. This form then have multiple forms which gets opened depending on the user option. There is a form StartClassWindow.xaml. Currently I am working on this form so I want it to start directly instead of MainWindow.xaml. So to do this I changed the app.xaml startupuri:

我有一个 WPF 应用程序,我在其中使用了多个表单。当我们启动应用程序时,会打开一个主要窗体,称为MainWindow.xaml. 这个表单然后有多个表单,这些表单根据用户选项打开。有一个表格StartClassWindow.xaml。目前我正在处理这个表单,所以我希望它直接启动而不是MainWindow.xaml. 所以为了做到这一点,我改变了app.xaml startupuri

<Application x:Class="Class.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         DispatcherUnhandledException="Application_DispatcherUnhandledException"
         StartupUri="StartClassWindow.xaml">
<Application.Resources>

</Application.Resources>
</Application>

But then it started giving error like below:

但随后它开始给出如下错误:

No matching constructor found on type 'Class.StartClassWindow'. You can use the Arguments or FactoryMethod directives to construct this type.' Line number '3' and line position '9'.

在类型“Class.StartClassWindow”上找不到匹配的构造函数。您可以使用 Arguments 或 FactoryMethod 指令来构造此类型。行号“3”和行位置“9”。

Here is the StartClassWindow.xaml.cs:

这是StartClassWindow.xaml.cs

namespace Class
{
    public partial class StartClassWindow : System.Windows.Window
    {

       public StartClassWindow(string classData)
       {
          InitializeComponent();
          className = classData;
          function();
       }
       //rest of the code.
    }
}

回答by Salah Akbari

You need to add a parameter-less constructor to your StartClassWindowlike this:

您需要StartClassWindow像这样添加一个无参数构造函数:

public StartClassWindow(string classData)
{
    InitializeComponent();
    className = classData;
    function();
}

public StartClassWindow()
{

}

Or if you don't want to have another constructor you can override the OnStartupmethod in the App.xaml.csbut you should removethe StartupUri="StartClassWindow.xaml"in your App.xamlfirst. Like below:

或者,如果你不希望有另一个构造函数,你可以覆盖OnStartup的方法App.xaml.cs,但你应该删除StartupUri="StartClassWindow.xaml"你的App.xaml第一个。像下面这样:

protected override void OnStartup(StartupEventArgs e)
{
    StartClassWindow st = new StartClassWindow("");
    st.Show();
}