繁体   English   中英

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

[英]Wizard navigation with IEnumerable / yield return

我是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;
      ...
    }
  }
}

父类包含使用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
}

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

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

  public abstract bool IsValid ();

  public abstract List<string> GetValidationErrors ();
}

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

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

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

显然你已经猜到这不是IEnumerable的经典用法。 但是,几乎总是使用收益率回报(至少在我的经验中),对于像这样的略微不合适的情况。 只要您不需要“<Back”支持,我就会按原样保留解决方案。

至于替代品,我使用了多种方法,没有一种方法符合我的口味。 有分支路径的巫师总是很邋..

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM