首先让我介绍一下我的应用。
我有MainWindowViewModel控制我的2个ViewModels:——overviewviewmodel——configureviewmodel
public class MainWindowViewModel : ViewModelBase
{
/// <summary>
/// The current view.
/// </summary>
public ViewModelBase _currentViewModel;
/// <summary>
/// Static instance of one of the ViewModels.
/// </summary>
public readonly static OverviewWindowViewModel _overviewviewmodel = new OverviewWindowViewModel();
/// <summary>
/// Static instance of one of the ViewModels.
/// </summary>
public readonly static ConfigurationWindowViewModel _configurationviewmodel = new ConfigurationWindowViewModel();
/// <summary>
/// The CurrentView property. The setter is private since only this
/// class can change the view via a command. If the View is changed,
/// we need to raise a property changed event.
/// </summary>
public ViewModelBase CurrentViewModel
{
get
{
return _currentViewModel;
}
set
{
if (_currentViewModel == value)
return;
_currentViewModel = value;
NotifyPropertyChanged("CurrentViewModel");
}
}
/// <summary>
/// Simple property to hold the 'OverviewViewCommand' - when executed
/// it will change the current view to the 'Overview'
/// </summary>
public ICommand OverviewViewCommand { get; private set; }
/// <summary>
/// Simple property to hold the 'ConfigurationViewCommand' - when executed
/// it will change the current view to the 'Configuration'
/// </summary>
public ICommand ConfigurationViewCommand { get; private set; }
/// <summary>
/// Default constructor. We set the initial view-model to 'OverviewViewCommand'.
/// We also associate the commands with their execution actions.
/// </summary>
public MainWindowViewModel()
{
CurrentViewModel = _overviewviewmodel;
_overviewviewmodel.SwitchViewModel2Request += NavigateToView2;
_configurationviewmodel.SwitchViewModel1Request += NavigateToView1;
/*OverviewViewCommand = new RelayCommand(param => ExecuteOverviewViewCommand());
ConfigurationViewCommand = new RelayCommand(param => ExecuteConfigurationViewCommand());*/
}
private void NavigateToView2(object sender, EventArgs e)
{
CurrentViewModel = _configurationviewmodel;
}
private void NavigateToView1(object sender, EventArgs e)
{
CurrentViewModel = _overviewviewmodel;
}
}
切换在两个用户控件之间工作得很好。在"configurationviewmodel"中,我还为12个按钮定义了IPageViewModel:
private ICommand _changePageCommand;
private List<IPageViewModel> _pageViewModels;
...
PageViewModels.Add(Buttons[0]);
PageViewModels.Add(Buttons[1]);
PageViewModels.Add(Buttons[2]);
PageViewModels.Add(Buttons[3]);
PageViewModels.Add(Buttons[4]);
PageViewModels.Add(Buttons[5]);
PageViewModels.Add(Buttons[6]);
PageViewModels.Add(Buttons[7]);
PageViewModels.Add(Buttons[8]);
PageViewModels.Add(Buttons[9]);
PageViewModels.Add(Buttons[10]);
PageViewModels.Add(Buttons[11]);
CurrentPageViewModel = PageViewModels[0];
private IPageViewModel _currentPageViewModel;
public IPageViewModel CurrentPageViewModel
{
get
{
return _currentPageViewModel;
}
set
{
if (_currentPageViewModel != value)
{
_currentPageViewModel = value;
NotifyPropertyChanged("CurrentPageViewModel");
}
}
}
private void ChangeViewModel(IPageViewModel viewModel)
{
if (!PageViewModels.Contains(viewModel))
PageViewModels.Add(viewModel);
CurrentPageViewModel = PageViewModels
.FirstOrDefault(vm => vm == viewModel);
}
我的问题是,当按钮二被按下,CurrentPageViewModel必须改变为PageViewModels[1],但这不会发生(它总是停留在PageViewModels[0])。我知道我之前的代码可以工作,因为在旧版本中我没有两个UserControls,而是两个不同的窗口。所以以前在窗口"Configurationviewmodel"工作像一个魅力,现在在用户控制"Configurationviewmodel"不起作用。
也许我给出了足够的代码来理解我的问题,如果没有,请问。
既然您已经确保视图模型包含在PageViewModels集合中,为什么不写:
private void ChangeViewModel(IPageViewModel viewModel)
{
// If the view model is not in the collection, it is added.
if (!PageViewModels.Contains(viewModel))
PageViewModels.Add(viewModel);
// So we can just assign the view mdoel here, since we know for sure
// it is in the collection.
CurrentPageViewModel = viewModel;
}
另外,您是否尝试过vm.Equals(viewModel)来比较引用?