C# RaisePropertyChanged时Propertychanged为null
我正在制作Carcasonne游戏,我正在使用MVVM。因此,我有一个窗口应用程序视图,每个视图有几个用户控件。该窗口包含所有viewModels和Datacontext中的视图。在这个窗口中,我使用ContentControl在正确的时间显示正确的视图内容数据绑定到窗口的viewModel中的一个道具,视图中的这个更改在每个viewModel中完成。例如,从loginview到menuview,我使用位于ApplicationViewModel中的命令Datacontext.changePage的一个按钮,并将下一个viewmodel绑定到commandparameter 这对于所有不同的视图都非常有效,但对于登录视图,我首先需要检查用户名和密码,因此我编写了一个新命令,而不是Datacontext.changePage,该命令不需要任何参数,它只运行一个方法,控制密码和用户名,并将其数据绑定到同一viewmodel中的一个道具。这一切都很好,所以在我通过singleton设计模式运行changePage命令之后,它将CurrentPageViewModel更改为正确的 问题是它不会更新我的GUI,因此它不会将viewModel更改为正确的视图模型,当我调试它时,MainWindowViewModel中的databounded prop的propertychanged为null。这意味着它不会识别属性中的任何更改,但我确实更改了道具,所以我不知道这里有什么问题 ApplicationView xaml:C# RaisePropertyChanged时Propertychanged为null,c#,wpf,mvvm,relaycommand,propertychanged,C#,Wpf,Mvvm,Relaycommand,Propertychanged,我正在制作Carcasonne游戏,我正在使用MVVM。因此,我有一个窗口应用程序视图,每个视图有几个用户控件。该窗口包含所有viewModels和Datacontext中的视图。在这个窗口中,我使用ContentControl在正确的时间显示正确的视图内容数据绑定到窗口的viewModel中的一个道具,视图中的这个更改在每个viewModel中完成。例如,从loginview到menuview,我使用位于ApplicationViewModel中的命令Datacontext.changePag
<Window.Resources>
<DataTemplate DataType="{x:Type vm:LoginViewModel}">
<vw:LoginView />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:MenuViewModel}">
<vw:MenuView />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:NewGameViewModel}">
<vw:NewGameView />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:BoardViewModel}">
<vw:BoardView />
</DataTemplate>
</Window.Resources>
<Grid Cursor="/Images/Arrow.cur">
<ContentControl Content="{Binding CurrentPageViewModel,UpdateSourceTrigger=PropertyChanged,NotifyOnSourceUpdated=True,NotifyOnTargetUpdated=True,NotifyOnValidationError=True}" />
</Grid>
您是否使用MVVM库,如MVVM Light或Caliburn Micro?如果没有,您能否为我们提供您的RaisePropertyChanged实现?我使用的是MVVM Light,处理程序=this.propertyChanged始终为空。因此,如果您在ifready中放置断点,您是否曾经到达该断点并分配mvm属性?是的,它会更改ApplicationViewModel中的CurrentPageViewModel,当我运行RaisePropertyChangedCurrentPageViewModel时;PropertyChanged为Null的是ApplicationViewModel。实例是否为主窗口的DataContext?
public ICommand ChangePageCommand
{
get
{
if (_changePageCommand == null)
{
_changePageCommand = new RelayCommand<IPageViewModel>(p =>ChangeViewModel(p),p => p is IPageViewModel);
}
return _changePageCommand;
}
}
public void ChangeViewModel(IPageViewModel viewModel)
{
Console.WriteLine("Changing viewmodel");
CurrentPageViewModel = viewModel;
}
public IPageViewModel CurrentPageViewModel
{
get
{
return _currentPageViewModel;
}
set
{
if (_currentPageViewModel != value)
{
_currentPageViewModel = value;
RaisePropertyChanged("CurrentPageViewModel");
}
}
}
private MenuViewModel _mvm = new MenuViewModel(null);
public MenuViewModel mvm
{
get { return _mvm;}
set {
if (value != _mvm)
{
_mvm = value;
}
}
}
private RelayCommand _loginCommand;
public ICommand LoginCommand
{
get
{
if (_loginCommand == null)
{
_loginCommand = new RelayCommand(CheckLogin);
}
return _loginCommand;
}
}
private bool _ready;
public bool ready
{
get
{
_ready = false;
if (_pswd != null && _pswd != "" && _usrn != null && _usrn != "")
{
_loggedPlayer = checkLoginDb();
if (_loggedPlayer != null)
return true;
}
return _ready;
}
}
private void CheckLogin()
{
if (ready) {
mvm = new MenuViewModel(_loggedPlayer);
RaisePropertyChanged("mvm");
ApplicationViewModel.Instance.ChangeViewModel((IPageViewModel)mvm);
}
}