ProgressChanged
的定义:
// Summary:
// Event called whenever the progress of the upload changes.
public event Action<IUploadProgress> ProgressChanged;
public void insertFile(String filePath)
{
//.. some code
insertRequest.ProgressChanged += Upload_ProgressChanged;
}
public void Upload_ProgressChanged(Google.Apis.Upload.IUploadProgress progress)
{
//.. I need filePath from insertFile() here!
}
如何将其他参数传递给Upload_ProgressChanged
?
我做了以下事情:
public void insertFile(String filePath)
{
//.. some code
ProgressChangedEventArgs args = new ProgressChangedEventArgs()
{
path = filePath
};
insertRequest.ProgressChanged += Upload_ProgressChanged;
}
static void Upload_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
}
public class ProgressChangedEventArgs : EventArgs
{
public string path { get; set; }
}
我错了Can not implicitly convert type 'void' to 'System.Action<Google.Apis.Upload.IUploadProgress>'
答案 0 :(得分:2)
您可以在闭包中捕获变量
,而不是使用事件insertRequest.ProgressChanges += progress => { /* Do something with filePath here */ };
答案 1 :(得分:0)
首先,定义EventArgs类 - 这将让您拥有您喜欢的任何信息......
public class ProgressChgEventArgs : System.EventArgs
{
public string Name { get;set; }
public int InstanceId { get;set; }
public ProgressChgEventArgs(string name, int id)
{
Name = name;
InstanceId = id;
}
}
接下来,创建使用这些参数的事件:
public event EventHandler<ProgressChgEventArgs> ProgressChanged;
然后,有一个&#39; On ....&#39;调用处理程序的方法
public void OnProgressChanged(ProgressChgEventArgs e)
{
var handler = new EventHandler<ProgressChgEventArgs>();
if (handler != null)
handler(this, e);
}
现在,在代码中的相关点(可能是当进度发生变化时!),调用OnProgressChanged(),传入ProgressChgEventArgs的相应实例:
private void Progress(string caller, int callerId)
{
var arguments = new ProgressChgEventArgs(caller, callerId);
OnProgressChanged(arguments);
}