wpf应用程序中的'System.Reflection.TargetParameterCountException'

时间:2015-06-28 12:16:26

标签: c# wpf delegates

当我尝试将UI更改为我的wpf应用程序

时,我一直在改变这种异常
private delegate void MyFunctionDelegate2(double t1, double t2, double t3);

public void translate(double tx, double ty, double tz)
{
    Transform3DGroup transform = new Transform3DGroup();
    TranslateTransform3D t = new TranslateTransform3D(tx, ty, tz);
    transform.Children.Add(t);
    if (!Application.Current.Dispatcher.CheckAccess())
    {
        var objj = new MyFunctionDelegate2(translate);
        Application.Current.Dispatcher.BeginInvoke(objj);
    }
}

1 个答案:

答案 0 :(得分:0)

您没有在BeginInvoke调用中将三个参数传递给MyFunctionDelegate2。它应该是这样的:

private delegate void MyFunctionDelegate2(double t1, double t2, double t3);

public void translate(double tx, double ty, double tz)
{
    if (!Application.Current.Dispatcher.CheckAccess())
    {
        var t = new MyFunctionDelegate2(translate);
        Application.Current.Dispatcher.BeginInvoke(t, tx, ty, tz);
    }
    else
    {
        var transform = new Transform3DGroup();
        var t = new TranslateTransform3D(tx, ty, tz);
        transform.Children.Add(t);
    }
}

或者更简单,没有委托声明:

public void translate(double tx, double ty, double tz)
{
    if (!Application.Current.Dispatcher.CheckAccess())
    {
        Application.Current.Dispatcher.BeginInvoke(
            new Action(() => translate(tx, ty, tz)));
    }
    else
    {
        Transform3DGroup transform = new Transform3DGroup();
        TranslateTransform3D t = new TranslateTransform3D(tx, ty, tz);
        transform.Children.Add(t);
    }
}