错误:在类型上找不到匹配的构造函数 [英] Error: No matching constructor found on type

查看:61
本文介绍了错误:在类型上找不到匹配的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF应用程序,正在使用多种形式.启动我们的应用程序时会打开一种主要形式,称为 MainWindow.xaml .然后,此表单具有多个表单,这些表单将根据用户选项打开.格式为 StartClassWindow.xaml .目前,我正在处理此表单,因此我希望它直接启动,而不是 MainWindow.xaml .为此,我更改了 app.xaml startupuri :

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:

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

在类型'Class.StartClassWindow'上找不到匹配的构造函数.你可以使用Arguments或FactoryMethod指令来构造它类型.'行号"3"和行位置"9".

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

这是 StartClassWindow.xaml.cs :

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

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

推荐答案

您需要像这样将无参数的构造函数添加到 StartClassWindow :

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

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

public StartClassWindow()
{

}

或者如果您不想拥有其他构造函数,则可以覆盖中.如下所示:

Or if you don't want to have another constructor you can override the OnStartup method in the App.xaml.cs but you should remove the StartupUri="StartClassWindow.xaml" in your App.xaml first. Like below:

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

这篇关于错误:在类型上找不到匹配的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆