Prism Sample 17-BasicRegionNavigation
本例是基础的导航应用
在窗口中布局了2个按钮,一个区域
<DockPanel LastChildFill="True"> <StackPanel Orientation="Horizontal" DockPanel.Dock="Top" Margin="5" > <Button Command="{Binding NavigateCommand}" CommandParameter="ViewA" Margin="5">Navigate to View A</Button> <Button Command="{Binding NavigateCommand}" CommandParameter="ViewB" Margin="5">Navigate to View B</Button> </StackPanel> <ContentControl prism:RegionManager.RegionName="ContentRegion" Margin="5" /> </DockPanel>
其中2个按钮绑定了同一个命令,只是参数不同,即命令是导航,目标不同。
public DelegateCommand<string> NavigateCommand { get; private set; } public MainWindowViewModel(IRegionManager regionManager) { _regionManager = regionManager; NavigateCommand = new DelegateCommand<string>(Navigate); } private void Navigate(string navigatePath) { if (navigatePath != null) _regionManager.RequestNavigate("ContentRegion", navigatePath); }
扩展模块定义了2个视图,用作导航的视图。要点是在模块中注册为导航视图。
public class ModuleAModule : IModule { public void OnInitialized(IContainerProvider containerProvider) { } public void RegisterTypes(IContainerRegistry containerRegistry) { containerRegistry.RegisterForNavigation<ViewA>(); containerRegistry.RegisterForNavigation<ViewB>(); } }