我有遍历一个的IEnumerable
收集和传递他们每个人的另一种方法一种方法。是这样的:
I have a method that loops through an IEnumerable
collection and passes each of them to another method. Something like:
void Run (object item)
{
foreach(var method in this.Methods)
method (item);
)
我如何实现某种进展,这将反映在进度条?我能做到这一点很容易,如果有人直接codeD这种方法中,但这种方法包含在外面类型的视图模型
,当然,我可以打电话。
How can I implement sort of progress that will be reflected in a progress bar? I could do this easily if it was directly coded inside this method but this method is contained in a type outside the ViewModel
, which I can call of course.
我只是不知道如何实现它,并从该方法得到它,并反映了变化中的用户界面,通过它传递给视图模型
等。
I just don't know how to implement it and get it from that method and reflect the changes in the UI, by passing it to ViewModel
, etc.
任何想法?
我会用事件如下处理这个。
I would approach this by using events as follows.
public class ViewModel : ViewModelBase
{
private int m_CurrentProgress;
private int m_MethodCount;
// Bind this to the progress bar
public int CurrentProgress
{
get { return m_CurrentProgress; }
set
{
m_CurrentProgress = value;
OnPropertyChanged("CurrentProgress");
}
}
// Bind this to the progress bar
public int MethodCount
{
get { return m_MethodCount; }
set
{
m_MethodCount = value;
OnPropertyChanged("MethodCount");
}
}
private void MethodExecuted(object sender, EventArgs e)
{
CurrentProgress++;
}
public void Run()
{
var c = new ExternalClass();
MethodCount = c.Methods.Count;
c.MethodExecuted += MethodExecuted;
c.Run(null);
}
}
public class ExternalClass
{
public List<object> Methods { get; set; }
public event EventHandler<EventArgs> MethodExecuted;
public void InvokeMethodExecuted(EventArgs e)
{
EventHandler<EventArgs> handler = MethodExecuted;
if (handler != null)
{
handler(this, e);
}
}
public void Run(object item)
{
foreach (var method in Methods)
{
method(item);
InvokeMethodExecuted(null);
}
}
}