c# - View 未显示在 MVVM 中

标签 c# mvvm

我正在尝试使用 mvvm 设计模式。我搜索并阅读了很多 mvvm 文档和示例,但这对我来说真的很难。

我想从主窗口导航并显示 View ,但我的代码无法正常工作。

没有<Window.Resources> ~ </Window.Resources>部分,ContentControl 元素显示文本“MyProject.ViewModel.AuthViewModel”。

但是如果我把<Window.Resources>部分,NullReferenceException: Object reference not set to an instance of an object. at MyProject.Helper.DelegateCommand.CanExecute(Object parameter)发生错误

怎么了?

MainViewModel.cs

public class MainViewModel : ViewModelBase
{
    private ICommand _changeViewCommand;
    private ViewModelBase _currentViewModel;
    private List<ViewModelBase> _viewModels;

    public MainViewModel(){
        ViewModels.Add(new AuthViewModel());
        CurrentViewModel = ViewModels[0];   
    }

    #region Commands
    public ICommand ChangeViewCommand
    {
        get
        {
            if (_changeViewCommand == null)
            {
                _changeViewCommand = new DelegateCommand(
                    param => ChangeViewModel((ViewModelBase)param),
                    param => param is ViewModelBase
                );
            }
            return _changeViewCommand;
        }
    }
    #endregion

    #region Properties
    public List<ViewModelBase> ViewModels
    {
        get
        {
            if (_viewModels == null)
                _viewModels = new List<ViewModelBase>();

            return _viewModels;
        }
    }

    public ViewModelBase CurrentViewModel
    {
        get
        {
            return _currentViewModel;
        }
        set
        {
            if(_currentViewModel != value)
            {
                _currentViewModel = value;
                RaisePropertyChanged("CurrentViewModel");
            }
        }
    }
    #endregion

    private void ChangeViewModel(ViewModelBase _viewModel)
    {
        if (_viewModel != null && !ViewModels.Contains(_viewModel))
            ViewModels.Add(_viewModel);

        CurrentViewModel = ViewModels.FirstOrDefault(_vm => _vm == _viewModel);
    }

MainView.xaml

<Window x:Class="MyProject.View.MainView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:v="clr-namespace:MyProject.View"
        xmlns:vm="clr-namespace:MyProject.ViewModel"
        xmlns:mp="clr-namespace:MyProject"
        Title="MyProject" Height="630" Width="460" WindowStartupLocation="CenterScreen" SnapsToDevicePixels="True">
    <Window.Resources>
        <DataTemplate DataType="{x:Type vm:MainViewViewModel}">
            <v:BeamView />
        </DataTemplate>
        <DataTemplate DataType="{x:Type vm:AuthViewModel}">
            <v:AuthView/>
        </DataTemplate>
    </Window.Resources>

    <Window.DataContext>
        <vm:MainViewModel/>
    </Window.DataContext>

    <Grid>
        <ContentControl Content="{Binding CurrentViewModel}" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
    </Grid>

</Window>

最佳答案

白天。 <DateTemplate>没有引起这个问题。

我刚刚在 DelegateCommand 写错了代码类

public override bool CanExecute(object parameter)
{
    if (parameter != null) // what the hell ?
        return true;
    return _canExecute(parameter);
}

我修好了

public override bool CanExecute(object parameter)
{
    if (parameter == null)
        return true;
    return _canExecute(parameter);
}

关于c# - View 未显示在 MVVM 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35077790/

相关文章:

c# - 如何使用 C# 代码实现 100% cpu 负载

来自 base64 的 C# Asp.net RazorPdf/iTextSharp 图像

c# - 委托(delegate)给实例方法不能有 null 'this'

c# - 如何让我的 DataGridView 的 CellPainting 事件与部分显示的单元格一起使用?

NHibernate MVVM session 最佳实践

android - 如何在我的 View 模型中重试请求

c# - 如何在wpf中的窗口中设置多个Datacontext

c# - C#.NET 中的 App.config 是什么?如何使用它?

asp.net - 什么技术(如果有的话)将取代 ASP.NET?

c# - Universal App中的NavigationSevice