Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 异步将多个参数传递到ReportProgress方法_C#_Wpf_Asynchronous - Fatal编程技术网

C# 异步将多个参数传递到ReportProgress方法

C# 异步将多个参数传递到ReportProgress方法,c#,wpf,asynchronous,C#,Wpf,Asynchronous,如何将多个参数传递到ReportProgress方法? 我遵循以下指南:创建progressbar。我的代码看起来像这样 MainWindow.xaml public User User { get; set; } public MainWindow() { InitializeComponent(); this.User = new User(); this.DataContext = User; } private async void Button_Click(

如何将多个参数传递到
ReportProgress
方法? 我遵循以下指南:创建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;
}
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;
}
正如您所看到的,我只是尝试向
ReportProgress
方法添加一个新参数(
User pUser
)。现在我在
按钮\u Click
方法中得到一个错误(行被标记)

参数1:无法从方法组转换为System.Action

“System.Progress.Progress(System.Action)”的最佳重载方法匹配-方法具有一些无效参数

报告方法的无重载
采用2个参数


我是这样尝试的,因为在我的实际应用程序中,我将有一个
ObersableCollection
。还有更好的方法吗?

您应该手动传递第二个参数,因为“Progress”构造函数只对一个参数执行操作。试试这个:

new Progress<int>(i => ReportProgress(i, this.User));

我更喜欢创建一条消息并传递多个值来报告进度

public class RMssg
{
    public int ProgressIndicator { get; set; }
    public User userInstance { get; set; }
}

private async void Button_Click(object sender, RoutedEventArgs e)
{
    var progressIndicator = new Progress<RMssg>(r => ReportProgress(r));
    await this.User.ReadUsers(progressIndicator, this.User);
}

void ReportProgress(RMssg rMssg)
{
    this.User.Val = rMssg.ProgressIndicator;
    var user = rMssg.userInstance;
}



public async Task<bool> ReadUsers(IProgress<RMssg> pProgress, User pUser)
{
    for (int i = 1; i < 11; i++)
    {
        await Task.Delay(500);
        var rMssg = new RMssg() { ProgressIndicator = i, userInstance = pUser };
        pProgress.Report(rMssg);
    }

    return true;
}
公共类RMssg
{
公共进程指示器{get;set;}
公共用户用户实例{get;set;}
}
专用异步无效按钮\u单击(对象发送方,路由目标)
{
var progressIndicator=新进展(r=>ReportProgress(r));
等待this.User.ReadUsers(progressIndicator,this.User);
}
无效报告进度(RMssg RMssg)
{
this.User.Val=rMssg.ProgressIndicator;
var user=rMssg.userInstance;
}
公共异步任务ReadUsers(IProgress-pProgress,User-pUser)
{
对于(int i=1;i<11;i++)
{
等待任务。延迟(500);
var rMssg=new rMssg(){ProgressIndicator=i,userInstance=pUser};
进度报告(rMssg);
}
返回true;
}

Progress alreay允许您报告多个值。只需使用包含所需字段的类,而不是
int
,甚至
Tuple
@PanagiotisKanavos即可。非常感谢!为什么要使用
进度
而不是订阅
可观察集合
?您的代码没有报告进度,它会更新另一个DTO以响应集合的更改。例如,如果您关心绑定,那么WPF会根据对集合的更改进行自我更新是的,我正在使用绑定。你能给我举一个使用
observateCollection
的小例子吗?(我想我知道如何使用它,但我不知道在这种情况下如何使用它)注意OP不是使用
进度
来报告进度,而是更新另一个DTO。未使用
userInstance
值。这对我来说非常有用-感谢您发布这篇文章!
public class RMssg
{
    public int ProgressIndicator { get; set; }
    public User userInstance { get; set; }
}

private async void Button_Click(object sender, RoutedEventArgs e)
{
    var progressIndicator = new Progress<RMssg>(r => ReportProgress(r));
    await this.User.ReadUsers(progressIndicator, this.User);
}

void ReportProgress(RMssg rMssg)
{
    this.User.Val = rMssg.ProgressIndicator;
    var user = rMssg.userInstance;
}



public async Task<bool> ReadUsers(IProgress<RMssg> pProgress, User pUser)
{
    for (int i = 1; i < 11; i++)
    {
        await Task.Delay(500);
        var rMssg = new RMssg() { ProgressIndicator = i, userInstance = pUser };
        pProgress.Report(rMssg);
    }

    return true;
}