1、将App.xaml中的StartupUri="MainWindow.xaml"删除。
2、使用NuGet安装Prism.Wpf、Prism.Core、Prism.Unity。
3、添加类“Bootstrapper”,编辑如下:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Windows; 7 using Microsoft.Practices.Unity; 8 using ActivationDeactivation.Views; 9 using Prism.Unity;10 11 namespace ActivationDeactivation12 {13 class Bootstrapper:UnityBootstrapper14 {15 protected override DependencyObject CreateShell()16 {17 return Container.Resolve();18 }19 20 protected override void InitializeShell()21 {22 Application.Current.MainWindow.Show();23 }24 }25 }
4、创建文件夹Views,将MainWindow.xaml移动到此文件夹中。向Views文件夹中添加ViewA.xaml,ViewB.xaml。
110 11 1912 13 14 15 16 1718
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Windows; 7 using System.Windows.Controls; 8 using System.Windows.Data; 9 using System.Windows.Documents;10 using System.Windows.Input;11 using System.Windows.Media;12 using System.Windows.Media.Imaging;13 using System.Windows.Navigation;14 using System.Windows.Shapes;15 using Microsoft.Practices.Unity;16 using Prism.Regions;17 18 namespace ActivationDeactivation.Views19 {20 ///21 /// MainWindow.xaml 的交互逻辑22 /// 23 public partial class MainWindow : Window24 {25 private IUnityContainer _container;26 private IRegionManager _regionManager;27 private IRegion _region;28 29 private ViewA _viewA;30 private ViewB _viewB;31 public MainWindow(IUnityContainer unityContainer,IRegionManager regionManager)32 {33 InitializeComponent();34 _container = unityContainer;35 _regionManager = regionManager;36 37 38 this.Loaded += MainWindow_Loaded;39 }40 41 private void MainWindow_Loaded(object sender, RoutedEventArgs e)42 {43 _viewA = _container.Resolve();44 _viewB = _container.Resolve ();45 46 _region = _regionManager.Regions["ContentRegion"];47 48 _region.Add(_viewA);49 _region.Add(_viewB);50 }51 52 private void BtnViewA_OnClick(object sender, RoutedEventArgs e)53 {54 _region.Activate(_viewA);55 }56 57 private void BtnDeactiveViewA_OnClick(object sender, RoutedEventArgs e)58 {59 _region.Deactivate(_viewA);60 }61 62 private void BtnViewB_OnClick(object sender, RoutedEventArgs e)63 {64 _region.Activate(_viewB);65 }66 67 private void BtnDeactiveViewB_OnClick(object sender, RoutedEventArgs e)68 {69 _region.Deactivate(_viewB);70 }71 }72 }
19 10 1211
19 10 1211
5、修改App.xaml
1 using System; 2 using System.Collections.Generic; 3 using System.Configuration; 4 using System.Data; 5 using System.Linq; 6 using System.Threading.Tasks; 7 using System.Windows; 8 9 namespace ActivationDeactivation10 {11 ///12 /// App.xaml 的交互逻辑13 /// 14 public partial class App : Application15 {16 protected override void OnStartup(StartupEventArgs e)17 {18 base.OnStartup(e);19 20 var bootstrapper = new Bootstrapper();21 bootstrapper.Run();22 }23 }24 }