0

我正在使用向我建议的以下课程来跟踪时间:

public sealed class TimeEvent
{
  public long Elapsed {get; private set;}
  public string Description { get; private set;}
  public TimeEvent(long elapsedTime, string descriptionOfEvent)
  {
    this.Elapsed = elapsedTime;
    this.Description = descriptionOfEvent;
  }
}

这是我的班级,其中包含时间列表:

public abstract class BaseGridViewModel
{

    protected BaseGridViewModel()
    {
        Times = new List<TimeEvent>();
    }
    public IList<TimeEvent> Times {get; set;}

}

以下是如何启动秒表并添加事件:

var sw = Stopwatch.StartNew();
vm.Times.Add(new TimeEvent(sw.ElapsedMillisends, "After event 1"));
sw.Stop();

有没有一种方法可以让我在我的视图模型中有一个方法,我可以将值传递给它,然后它会在内部执行与 Add 相同的操作。例如这样的方法:

vm.Event(sw, "After event 1"));

甚至

4

1 回答 1

5

为什么不?

public abstract class BaseGridViewModel
{

    protected BaseGridViewModel()
    {
        Times = new List<TimeEvent>();
    }
    public IList<TimeEvent> Times {get; set;}

    public void Event(StopWatch watch, string message)
    {
        Times.Add(new TimeEvent(watch.ElapsedMilliseconds, message));
    }

}
于 2012-09-13T02:09:15.293 回答