MyN*_*ame 5 c# wpf asynchronous
如何将多个参数传递给ReportProgress方法?我正在遵循本指南:MSDN to create a progressbar。我的代码看起来像这样。
MainWindow.xaml
public User User { get; set; }
public MainWindow()
{
InitializeComponent();
this.User = new User();
this.DataContext = User;
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
var progressIndicator = new Progress<int>(ReportProgress); //here is the error
await this.User.ReadUsers(progressIndicator, this.User)
}
void ReportProgress(int value, User pUser)
{
this.User.Val = value;
}
Run Code Online (Sandbox Code Playgroud)
User.xaml
public async Task<bool> ReadUsers(IProgress<int> pProgress, User pUser)
{
for (int i = 1; i < 11; i++)
{
await Task.Delay(500);
pProgress.Report(i, pUser);
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我只是尝试User pUser向该ReportProgress方法添加一个新参数 ( ) 。现在我在Button_Click方法中遇到错误(标记了行)。
参数 1:无法从方法组转换为 System.Action
'System.Progress.Progress(System.Action)'-method 的最佳重载方法匹配有一些无效参数
没有重载
Report-Method需要 2 个参数
我是这样尝试的,因为在我的实际应用程序中,我将有一个ObersableCollection<User>. 有没有更好的方法我应该去?
您应该手动传递第二个参数,因为 'Progress' 构造函数只使用一个参数执行操作。尝试这个:
new Progress<int>(i => ReportProgress(i, this.User));
Run Code Online (Sandbox Code Playgroud)
并从 'pProgress.Report' 方法中删除第二个参数:
pProgress.Report(i);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1261 次 |
| 最近记录: |