'System.Reflection.TargetParameterCountException' 在 wpf 应用程序中
'System.Reflection.TargetParameterCountException' in wpf application
当我尝试将 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);
}
}
您没有在 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);
}
}
当我尝试将 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);
}
}
您没有在 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);
}
}