1 新建wpf程序
2 通过nuget添加prism.unity的引用注意要选V7.2版本,自动会添加其他依赖的引用
3 在项目中添加目录Prism,并新建类StackPanelRegionAdapter
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
namespace OldRegions.Prism
{
public class StackPanelRegionAdapter : RegionAdapterBase<StackPanel>
{
public StackPanelRegionAdapter(IRegionBehaviorFactory regionBehiaviorFactory)
: base(regionBehiaviorFactory)
{
}
protected override void Adapt(IRegion region, StackPanel regionTarget)
{
region.Views.CollectionChanged += (s,e)=>
{
if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
{
foreach (FrameworkElement item in e.NewItems)
{
regionTarget.Children.Add(item);
}
}
};
}
protected override IRegion CreateRegion()
{
return new AllActiveRegion();
}
}
}
4 在项目中新建Bootstrapper,使其继承与UnityBootstrapper
using OldRegions.Prism;
using Prism.Regions;
using Prism.Unity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using Unity;
namespace OldRegions
{
public class Bootstrapper:UnityBootstrapper
{
protected override DependencyObject CreateShell()
{
return Container.Resolve<MainWindow>();
}
protected override void InitializeShell()
{
Application.Current.MainWindow.Show();
}
protected override RegionAdapterMappings ConfigureRegionAdapterMappings()
{
RegionAdapterMappings mappings = base.ConfigureRegionAdapterMappings();
mappings.RegisterMapping(typeof(StackPanel), Container.Resolve<StackPanelRegionAdapter>());
return mappings;
}
}
}
5 删除App.xaml中的StartupUri属性,在App.xaml.cs中重写OnStartup方法
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
var boot = new Bootstrapper();
boot.Run();
}
6 在MainWindow.xaml中添加
xmlns:prism="http://prismlibrary.com/"
并在Grid里面添加下面的代码
<StackPanel prism:RegionManager.RegionName="ContentRegion"/>