如何使用IEnumerable的实施进展如何? [英] How to implement progress using IEnumerable?

查看:169
本文介绍了如何使用IEnumerable的实施进展如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有遍历一个的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);
        }
    }
}

这篇关于如何使用IEnumerable的实施进展如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆