Prism_区域
MainView.xaml
<Grid> <Grid.RowDefinitions> <RowDefinition Height="30" /> <RowDefinition /> </Grid.RowDefinitions> <StackPanel Orientation="Horizontal"> <Button Width="80" Command="{Binding ShowContentCmm}" CommandParameter="A" Content="区域A" /> <Button Width="80" Command="{Binding ShowContentCmm}" CommandParameter="B" Content="区域B" /> <Button Width="80" Command="{Binding ShowContentCmm}" CommandParameter="C" Content="区域C" /> </StackPanel> <ContentControl Grid.Row="1" prism:RegionManager.RegionName="ContentRegion" /> </Grid>
构造函数通过 IRegionManager 找到视图中可用的区域(prism:RegionManager.RegionName="ContentRegion")。
MainViewModel.cs
/// <summary> /// 区域管理 /// </summary> public readonly IRegionManager RegionManager; public DelegateCommand<string> ShowContentCmm { get; set; } public MainWindowViewModel(IRegionManager regionManager) { RegionManager = regionManager; ShowContentCmm = new DelegateCommand<string>(ShowContentFun); } /// <summary> /// 改变显示用户控件 /// </summary> /// <param name="ViewName"></param> private void ShowContentFun(string ViewName) { RegionManager.Regions["ContentRegion"].RequestNavigate(ViewName); }
在App中完成区域视图的注册
App.xaml.cs
public partial class App : PrismApplication { /// <summary> /// 设置启动页 /// </summary> /// <returns></returns> /// <exception cref="NotImplementedException"></exception> protected override Window CreateShell() { return Container.Resolve<MainWindow>(); } /// <summary> /// 注入服务 /// </summary> /// <param name="containerRegistry"></param> /// <exception cref="NotImplementedException"></exception> protected override void RegisterTypes(IContainerRegistry containerRegistry) { containerRegistry.RegisterForNavigation<UCA>("A"); containerRegistry.RegisterForNavigation<UCB>("B"); containerRegistry.RegisterForNavigation<UCC>("C"); } }