视图未在MVVM中显示

时间:2016-01-29 05:53:22

标签: c# mvvm

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

我想从主窗口导航并显示视图,但我的代码不起作用。

如果没有<Window.Resources> ~ </Window.Resources>部分,ContentControl元素会显示文本&#39; MyProject.ViewModel.AuthViewModel&#39;。

但如果我放<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>

1 个答案:

答案 0 :(得分:1)

dayum。 <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);
}