Why
- 最近闲着无聊就研究了一下几个好用点的
WPF
的框架,发现Prism
还是挺好用的,不管是在NetFrameWork
还是Core
上,大致步骤都是大差不差,以下以Core作为基本介绍。
How
- 注意使用
NetFrameWork
的时候需要选择的版本是8以下。如果是Core
的话选择最新的就可以了。(以下是Framework的安装截图)

- 更改你的
App.xaml.cs
public partial class App : PrismApplication
{
protected override Window CreateShell()
{
return Container.Resolve<Main>();
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
var iRegionManager = containerRegistry.GetContainer().Resolve<IRegionManager>();
if (iRegionManager == null)
return;
}
}
- 更改你的
App.xaml
<prism:PrismApplication
x:Class="PQToolBoxCore.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:PQToolBoxCore"
xmlns:prism="http://prismlibrary.com/">
<!-- 以下是如何使用Prism的资源字典 可忽略 -->
<prism:PrismApplication.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/SkinDefault.xaml" />
<ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/Theme.xaml" />
<ResourceDictionary Source="/Common/string.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</prism:PrismApplication.Resources>
</prism:PrismApplication>
- 删除默认配置的
MainWindow
- 在根目录下新建两个文件夹,分别为
Views
,ViewModels
,注意一定需要是这个名字。而且对于新建的视图和绑定的ViewModel
需要一一对应,截图如下:

- 对于
Prism
,如果需要View
和ViewModel
自动映射的话,需要在你的XAML
下加上一句代码prism:ViewModelLocator.AutoWireViewModel="True"
(如果没有prism则需要加上 xmlns:prism="http://prismlibrary.com/"
),如下图:

- 在
ViewModel
中可以如下使用依赖注入:
private IRegionManager iRegionManager;
private IEventAggregator iEventAggregator;
public MenuViewModel()
{
}
public MenuViewModel(IRegionManager iRegionManager, IEventAggregator iEventAggregator)
{
this.iRegionManager = iRegionManager;
this.iEventAggregator = iEventAggregator;
}
- 在
ViewModel
可以如下使用依赖属性和Command
。(需要继承BindableBase)
public partial class ToolsViewModel : BindableBase
{
private IRegionManager iRegionManager;
private IEventAggregator iEventAggregator;
public ToolsViewModel()
{
}
public ToolsViewModel(IRegionManager iRegionManager, IEventAggregator iEventAggregator)
{
this.iRegionManager = iRegionManager;
this.iEventAggregator = iEventAggregator;
}
private int _id;
public int ID
{
get => _id;
set => SetProperty(ref _id, value);
}
private DelegateCommand<string> _addCommand;
public DelegateCommand<string> AddCommand => _addCommand = _addCommand ?? new DelegateCommand<string>((id) => Console.WriteLine(id));
}
- 如果你想在任意地方使用
DI
,你可以先在App.xaml.cs
中增加:
public partial class App : PrismApplication
{
private static IUnityContainer _container;
public static new IUnityContainer Container => _container;
protected override Window CreateShell()
{
return Container.Resolve<Main>();
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
var iRegionManager = containerRegistry.GetContainer().Resolve<IRegionManager>();
if (iRegionManager == null)
return;
iRegionManager.RegisterViewWithRegion(RegionName.MenuRegion, typeof(Menu));
iRegionManager.RegisterViewWithRegion(RegionName.MainRegion, typeof(Tools));
_container = containerRegistry.GetContainer();
}
}
T obj= App.Container.Resolve<T>()
- 对于事件订阅发布器
IEventAggregator
,主要稍微了解委托的就很简单。需要继承PubSubEvent
。
public class AddToolEvent : PubSubEvent
{
}
public class DeleteToolEvent : PubSubEvent<object>
{
}
this.iEventAggregator.GetEvent<AddToolEvent>().Subscribe(() => Console.WriteLine("AddToolEvent"));
this.iEventAggregator.GetEvent<AddToolEvent>().Publish();
this.iEventAggregator.GetEvent<DeleteToolEvent>().Subscribe((obj) => Console.WriteLine($"DeleteToolEvent{obj}"));
this.iEventAggregator.GetEvent<DeleteToolEvent>().Publish(new object());
Tips
- 当前只是使用了差不多最基本的功能,其中最主要的有模块的使用还有
IRegionManager
和IEventAggregator
。
- 准备着手重写原来的一部分代码了。