WPF System.Reflection.TargetParameterCountException参数计数不匹配

本文关键字:不匹配 参数 TargetParameterCountException System Reflection WPF | 更新日期: 2023-09-27 17:57:49

我有以下代码,它正在打开一个对话框,向用户指示应用程序正在另一个线程中处理,然后在收到LoadCompletedEvent后关闭窗口。然而,我总是会遇到以下错误,并且不确定它具体指的是什么。

An unhandled exception of type 'System.Reflection.TargetParameterCountException' occurred in PresentationFramework.dll
Additional information: Parameter count mismatch.

线程就是这样创建的。

        Thread thread = new Thread(() =>
        {
            MetroProgressWindow metroProgressWindow = new MetroProgressWindow(this);
            metroProgressWindow.ShowDialog();
        });
        thread.SetApartmentState(ApartmentState.STA);
        //thread.IsBackground = true;
        thread.Name = "omega-thread";
        thread.Start();
        // create window, do loading, business logic, etc            
        // throw load completed event
        LoadCompletedEvent(this, EventArgs.Empty);
        Visibility = Visibility.Hidden;     // hide MainWindow
        renderWindow.ShowDialog();          // show the RenderWindow as a modal dialog NOTE: this is thread blocking

然后在另一个窗口中,后面的代码看起来是这样的。

public partial class MetroProgressWindow : MetroWindow
{
    public MetroProgressWindow(IOmegaWindow window)
    {
        InitializeComponent();
        window.LoadCompletedEvent += delegate
        {
            Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                new Action(() => { this.Close(); }),
                null);
        };
    }
}

WPF System.Reflection.TargetParameterCountException参数计数不匹配

我认为问题是由BeginInvoke中的null参数引起的。尝试例如没有它:

Dispatcher.BeginInvoke(DispatcherPriority.Normal,
    new Action(() => { this.Close(); }));