使用IEnumerable/yield返回的向导导航

mil*_*jos 5 .net c# ienumerable

我是C#的新手,刚刚发现如何使用yield return来创建自定义的IEnumerable枚举.我正在尝试使用MVVM来创建向导,但是我无法弄清楚如何控制从一个页面到下一个页面的流量.在某些情况下,我可能希望显示某个步骤,而在其他情况下,它不适用.

无论如何,我的问题是我使用IEnumerable来返回每个后续页面,这非常好用,但我知道我可能正在做一些不正确/无意识的语言.子类只需要覆盖抽象步骤IEnumerable访问器:

public class HPLDTWizardViewModel : WizardBase
{
  protected override IEnumerable<WizardStep> Steps
  {
    get 
    {
      WizardStep currentStep;

      // 1.a start with assay selection
      currentStep = new AssaySelectionViewModel();
      yield return currentStep;
      // 1.b return the selected assay.
      SigaDataSet.Assay assay = ((AssaySelectionViewModel)currentStep).SelectedAssay;
      sigaDataSet = (SigaDataSet)assay.Table.DataSet;

      // 2.a get the number of plates 
      currentStep = new NumPlatesViewModel(sigaDataSet);
      yield return currentStep;
      ...
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

父类包含使用Steps属性的枚举器的导航逻辑:

public abstract class WizardBase : ViewModelBase
{
  private ICommand _moveNextCommand;
  private ICommand _cancelCommand;
  private IEnumerator<WizardStep> _currentStepEnumerator;

  #region Events

  /// <summary>
  /// Raised when the wizard window should be closed.
  /// </summary>
  public event EventHandler RequestClose;

  #endregion // Events

  #region Public Properties

  /// <summary>
  /// Gets the steps.
  /// </summary>
  /// <value>The steps.</value>
  protected abstract IEnumerable<WizardStep> Steps { get;}

  /// <summary>
  /// Gets the current step.
  /// </summary>
  /// <value>The current step.</value>
  public WizardStep CurrentStep 
  {
    get 
    {
      if (_currentStepEnumerator == null)
      {
        _currentStepEnumerator = Steps.GetEnumerator();
        _currentStepEnumerator.MoveNext();
      }

      return _currentStepEnumerator.Current; 
    }
  }

  #endregion //Public Properties

  #region Commands

  public ICommand MoveNextCommand
  {
    get
    {
      if (_moveNextCommand == null)
        _moveNextCommand = new RelayCommand(
            () => this.MoveToNextPage(),
            () => this.CanMoveToNextPage());

      return _moveNextCommand;
    }
  }

  public ICommand CancelCommand
  {
    get
    {
      if (_cancelCommand == null)
        _cancelCommand = new RelayCommand(() => OnRequestClose());

      return _cancelCommand;
    }
  }

  #endregion //Commands

  #region Private Helpers

  /// <summary>
  /// Determines whether this instance [can move to next page].
  /// </summary>
  /// <returns>
  ///   <c>true</c> if this instance [can move to next page]; otherwise, <c>false</c>.
  /// </returns>
  bool CanMoveToNextPage()
  {
    if (CurrentStep == null)
      return false;
    else
      return CurrentStep.IsValid();
  }

  /// <summary>
  /// Moves to next page.
  /// </summary>
  void MoveToNextPage ()
  {
    _currentStepEnumerator.MoveNext();

    if (_currentStepEnumerator.Current == null)
      OnRequestClose();
    else
      OnPropertyChanged("CurrentStep");
  }

  /// <summary>
  /// Called when [request close].
  /// </summary>
  void OnRequestClose ()
  {
    EventHandler handler = this.RequestClose;
    if (handler != null)
      handler(this, EventArgs.Empty);
  }

  #endregion //Private Helpers
}
Run Code Online (Sandbox Code Playgroud)

这是每个向导页面实现的WizardStep抽象类:

public abstract class WizardStep : ViewModelBase
{
  public abstract string DisplayName { get; }

  public abstract bool IsValid ();

  public abstract List<string> GetValidationErrors ();
}
Run Code Online (Sandbox Code Playgroud)

正如我所说,这非常有效,因为我使用Enumerator导航列表.导航逻辑位于抽象父类中,所有子操作都要覆盖Steps属性.WizardSteps本身包含逻辑,以便他们知道它们何时有效并且用户可以继续.我正在使用MVVM,因此下一个按钮通过命令绑定到CanMoveToNextPage()和MoveToNextPage()函数.

我想我的问题是:在这种情况下滥用枚举模型有多么错误?有没有更好的办法? 我真的需要以某种方式定义控制流,它非常适合于yield return能力,因此我可以让流逻辑返回到Steps访问器以获取下一页.

And*_*uus 1

我认为只要满足要求、易于维护、易于阅读,那么它就不会那么糟糕。

显然,正如您所猜测的,这不是 IEnumerable 的经典用法。但是,收益率回报几乎总是用于(至少根据我的经验)像这样稍微不寻常的情况。只要您不需要“<返回”支持,我就会保持解决方案不变。

至于替代方案,我已经使用了多种方法,但没有一个真正符合我的口味。具有分支路径的巫师总是有点混乱。

对于重量级向导,一种选择是状态机。编写一两个知道如何在状态之间遍历以及哪些转换有效的方法。为每个状态构建一个 UserControl 并通过 ListCollectionView 将它们公开给 TabControl。

我曾经使用过的一个很好的轻量级解决方案是将向导中的所有页面堆叠在网格中,并通过绑定到由枚举表示的状态来切换它们的可见性。使用 ValueConverter 您甚至可以避免幻数。然后,页面之间的切换只需增加或减少 Status 属性即可。