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

21

我有一个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>

但后来它开始产生以下错误:

在类型“Class.StartClassWindow”上找不到匹配的构造函数。您可以使用Arguments或FactoryMethod指令来构造此类型。行号为“3”,行位置为“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.
    }
}
3个回答

36

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

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

public StartClassWindow()
{

}

如果您不想拥有另一个构造函数,可以覆盖 App.xaml.cs 中的 OnStartup 方法,但是首先应该删除您在App.xaml中的 StartupUri="StartClassWindow.xaml"。 如下所示:

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

4
"通常情况下",你的构造函数必须是无参的:
public Login()

但是,如果您正在使用依赖注入,就像这样:

public Login(IUserService userService)

构造函数不是无参数的,如果没有告诉框架如何实例化页面,框架就无法实例化。

因此有几个选项:

从构造函数中删除服务

像这样做,但需要以不同方式访问用户服务:

public Login()

使用Unity(或Prism)在 .NET 4.8、4.7之前

你可以使用诸如Unity的依赖注入框架来注册组件。具体内容可参考:

https://www.wpftutorial.net/ReferenceArchitecture.html

public class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        IUnityContainer container = new UnityContainer();
        container.RegisterType<IUserService, UserService>();
       
        MainWindow mainWindow = container.Resolve<MainWindow>();
        mainWindow.Show();
    }
}

手动使用导航服务

手动导航,进行自己的构建:

NavigationService.Navigate(new LoginPage(new UserService);

如此描述:https://learn.microsoft.com/zh-cn/dotnet/desktop/wpf/app-development/navigation-overview?view=netframeworkdesktop-4.8


.Net 5 WPF,使用内置的 DI

如果您正在使用 .Net 5,则可以参考以下教程。确保注册窗口和服务:

https://executecommands.com/dependency-injection-in-wpf-net-core-csharp/

以下是示例:

private void ConfigureServices(ServiceCollection services)
{
    services.AddScoped<IUserService,UserService>();
    services.AddSingleton<MainWindow>();
}

private void OnStartup(object sender, StartupEventArgs e)
{
    var mainWindow = serviceProvider.GetService<MainWindow>();
    mainWindow.Show();
}

1
我不得不注释掉Startup和StartupUri。

<Application x:Class="Data_Export_Refinity.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             >
    <!-- Startup="Application_Startup">
    StartupUri="MainWindow.xaml" -->
    <Application.Resources>

    </Application.Resources>
</Application>


哦,谢谢,我忘记移除StartupUri - Trương Quốc Khánh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接