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 Microsoft.Practices.Unity; 7 using System.Windows; 8 using Prism.Unity; 9 using ViewDiscovery.Views;10 11 namespace ViewDiscovery12 {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。
110 11 1312
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 Prism.Regions;16 17 namespace ViewDiscovery.Views18 {19 ///20 /// MainWindow.xaml 的交互逻辑21 /// 22 public partial class MainWindow : Window23 {24 public MainWindow(IRegionManager regionManager)25 {26 InitializeComponent();27 //View discovery28 regionManager.RegisterViewWithRegion("ContentRegion", typeof(ViewA));29 }30 }31 }
19 10 1211