一个快速而肮脏的导航示例(这是在这里而不是在 VS 中编写的,因此如果拼写错误,请应用修复):
让我们为我们的视图创建一个分层描述:
public abstract Class ViewModelBase : INotifyPropertyChanged{}
public abstract Class ViewModelNavigationBase : ViewModelBase {}
public Class ViewModel1 : ViewModelNavigationBase {}
public Class ViewModel2 : ViewModelNavigationBase {}
public Class ViewModel3 : ViewModelNavigationBase {}
public Class MainViewModel : ViewModelBase
{
private ViewModelNavigationBase currentViewModel;
public ViewModelNavigationBase CurrentViewModel
{
get{return currentViewModel;}
set
{
currentViewModel = value;
OnPropertyChanged("CurrentViewModel");
}
}
private const int numberOfPages = 3 ;
private int index;
private ViewModelNavigationBase[numberOfPages] pages;
....
... OnNavigateCommand()
{
index++;
index = index % numberOfPages;
CurrentViewModel = pages[index];
}
}
资源 :
<local:MainViewModel x:Key="MainVm" />
<DataTemplate TargetType={x:Type local:ViewModel1}>
<local:View1 />
</DataTemplate>
<DataTemplate TargetType={x:Type local:ViewModel2}>
<local:View2 />
</DataTemplate>
<DataTemplate TargetType={x:Type local:ViewModel3}>
<local:View3 />
</DataTemplate>
xml:
<Window x:Name="MainWindow"
DataContext={StaticResource MainVm}>
<StackPanel>
<ContentControl Content={Binding CurrentViewModel} />
<Button Content="Navigate" Command="{Binding NavigateCommand}"/>
</StackPanel>
</Window>