MVVM导航ViewModels

时间:2017-01-09 03:18:48

标签: c# wpf mvvm

我对C#有点新,并在https://www.tutorialspoint.com/mvvm/mvvm_hierarchies_and_navigation.htm

处理MVVM教程

我遇到了MainWindowViewModel

的问题
class MainWindowViewModel : BindableBase {

    public MainWindowViewModel( ) {
        NavCommand = new MyICommand<string>(OnNav);
    }

    private CustomerListViewModel custListViewModel = new CustomerListViewModel( );

    private OrderViewModel orderViewModelModel = new OrderViewModel( );

    private BindableBase _CurrentViewModel;

    public BindableBase CurrentViewModel {
        get { return _CurrentViewModel; }
        set { SetProperty(ref _CurrentViewModel, value); }
    }

    public MyICommand<string> NavCommand { get; private set; }

    private void OnNav(string destination) {

        switch (destination) {
            case "orders":
                //CurrentViewModel = orderViewModelModel;
                break;

            case "customers":
            default:
                //CurrentViewModel = custListViewModel;
                break;
        }
    }
}

我无法使用以上2行设置应用程序来设置CurrentViewModel取消注释。我得到了:

  • 无法隐式转换类型&#39; MVVMHierarchiesDemo.ViewModel.OrderViewModel&#39;到&#39; MVVMHierarchiesDemo.BindableBase&#39;

  • 无法隐式转换类型&#39; MVVMHierarchiesDemo.ViewModel.CustomerListViewModel&#39;到&#39; MVVMHierarchiesDemo.BindableBase&#39;

我确实在前面的(tutorualspoint)教程中发现了一个拼写错误/错误,但在层次结构教程中没有看到任何错误。

我有没有看到这个例子的问题?

2 个答案:

答案 0 :(得分:0)

我不知道为什么,但你可以试试。

    CustomerListViewModel.cs中的
  • class OrderViewModel :BindableBase

    {

    }

  • OrderViewModel.cs中的
  • class OrderViewModel :BindableBase

    {

    }

答案 1 :(得分:0)

您的$ ls -al /dev | grep fd dr-xr-xr-x 1 root wheel 0 Jan 23 20:42 fd/ $ stat /dev/fd/4 # same result with -L flag stat: /dev/fd/4: stat: Bad file descriptor 属性属于CurrentViewModel类型,因此您要为其指定的任何对象应为BindableBase或从中派生。

这就是为什么您必须将BindableBase添加到您的课程: BindableBaseCustomerListViewModel中:

OrderViewModel