gpt4 book ai didi

c# - View 未显示在 MVVM 中

转载 作者:太空宇宙 更新时间:2023-11-03 12:52:25 25 4
gpt4 key购买 nike

我正在尝试使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com