TargetParameterCountException:参数计数不匹配
TargetParameterCountException: the parameter count mismatch
在我的 WPF MVVM 应用程序的视图模型和数据模型下方。我在执行调用时遇到问题(见下文),抛出异常参数计数不匹配。方法 "getDataFromDatabase" 正在返回 "UserData" 的集合。那么如何解决呢?
查看模型:
public class MyViewModel : BaseViewModel
{
private static Dispatcher _dispatcher;
public ObservableCollection<UserData> lstUsers
public ObservableCollection<UserData> LstUsers
{
get
{
return this.lstUsers;
}
private set
{
this.lstUsers= value;
OnPropertyChanged("LstUsers");
}
}
// LoadData is called from the constructor of the view code-behind (xaml.cs) once DataContext is correctly set.
public void LoadData()
{
ThreadPool.QueueUserWorkItem(new WaitCallback((o) =>
{
var result = getDataFromDatabase();
UIThread((p) => LstUsers = result);
}));
}
ObservableCollection<UserData> getDataFromDatabase()
{
return this.RequestDataToDatabase();
}
static void UIThread(Action<object> a)
{
if(_dispatcher == null)
{
_dispatcher = Dispatcher.CurrentDispatcher;
}
_dispatcher.Invoke(a); <---- HERE EXCEPTION IS THROWN
}
}
数据模型:
public class UserData
{
public string ID{ get; set; }
public string Name { get; set; }
public string Surname { get; set; }
// Other properties
}
Action<object>
是一个带有对象类型参数的动作。它必须通过带有单个参数的 Invoke(Delegate method, params object[] args)
重载来调用:
Application.Current.Dispatcher.Invoke(a, someObject);
所以把Action<object>
改成Action
:
static void UIThread(Action a)
{
Application.Current.Dispatcher.Invoke(a);
}
并这样称呼它:
UIThread(() => LstUsers = result);
您可能还想使您的 LoadData 方法异步并像这样编写它:
public async Task LoadData()
{
await Task.Run(() =>
{
var result = getDataFromDatabase();
Application.Current.Dispatcher.Invoke(() => LstUsers = result);
});
}
在我的 WPF MVVM 应用程序的视图模型和数据模型下方。我在执行调用时遇到问题(见下文),抛出异常参数计数不匹配。方法 "getDataFromDatabase" 正在返回 "UserData" 的集合。那么如何解决呢?
查看模型:
public class MyViewModel : BaseViewModel
{
private static Dispatcher _dispatcher;
public ObservableCollection<UserData> lstUsers
public ObservableCollection<UserData> LstUsers
{
get
{
return this.lstUsers;
}
private set
{
this.lstUsers= value;
OnPropertyChanged("LstUsers");
}
}
// LoadData is called from the constructor of the view code-behind (xaml.cs) once DataContext is correctly set.
public void LoadData()
{
ThreadPool.QueueUserWorkItem(new WaitCallback((o) =>
{
var result = getDataFromDatabase();
UIThread((p) => LstUsers = result);
}));
}
ObservableCollection<UserData> getDataFromDatabase()
{
return this.RequestDataToDatabase();
}
static void UIThread(Action<object> a)
{
if(_dispatcher == null)
{
_dispatcher = Dispatcher.CurrentDispatcher;
}
_dispatcher.Invoke(a); <---- HERE EXCEPTION IS THROWN
}
}
数据模型:
public class UserData
{
public string ID{ get; set; }
public string Name { get; set; }
public string Surname { get; set; }
// Other properties
}
Action<object>
是一个带有对象类型参数的动作。它必须通过带有单个参数的 Invoke(Delegate method, params object[] args)
重载来调用:
Application.Current.Dispatcher.Invoke(a, someObject);
所以把Action<object>
改成Action
:
static void UIThread(Action a)
{
Application.Current.Dispatcher.Invoke(a);
}
并这样称呼它:
UIThread(() => LstUsers = result);
您可能还想使您的 LoadData 方法异步并像这样编写它:
public async Task LoadData()
{
await Task.Run(() =>
{
var result = getDataFromDatabase();
Application.Current.Dispatcher.Invoke(() => LstUsers = result);
});
}