简体   繁体   中英

TargetParameterCountException in WPF

TargetParameterCountException: Parameter count mismatch.

What's going on here please?

After the method finish, this error is thrown.

I already tried some other topics, but my case is different.

Any clue?

I am using this Dispatcher, because I had a cross thread problem.

void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
    if (backgroundWorker.CancellationPending == true)
    {
        e.Cancel = true;
        return;
    }

    e.Result = ...;

    Dispatcher.BeginInvoke(DispatcherPriority.Normal,
        (RunWorkerCompletedEventHandler)delegate
        {
            image1.Source = (BitmapImage)e.Result;
        });
}

The RunWorkerCompletedEventHandler delegate expects parameters, which you do not provide (or use), you could probably do with changing it to an Action :

Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)delegate
{
    image1.Source = (BitmapImage)e.Result;
});

If you must use the RunWorkerCompletedEventHandler (which is pointless) you could call the respective BeginInvoke overload and provide an empty array of two objects which represents sender and event-args:

Application.Current.Dispatcher.BeginInvoke((RunWorkerCompletedEventHandler)delegate
{
    image1.Source = (BitmapImage)e.Result;
}, DispatcherPriority.Normal, new object[2]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM