Prism.Interactivity: 主要用来截取View即界面的一些处理,而这些功能通过vm 不好实现,只能用 CommandBehaviorBase 来截取处理,特别是在处理界面异常很有用。
定义如下:
public class ValidationExceptionBehavior : Behavior<FrameworkElement>
{
// 方法体
}
使用:
界面错误信息的处理:
<Interactivity:Interaction.Behaviors> <common:ValidationExceptionBehavior></common:ValidationExceptionBehavior>
</Interactivity:Interaction.Behaviors>
键盘事件的处理
<Interactivity:Interaction.Triggers>
<Interactivity:EventTrigger EventName="KeyDown">
<Interactivity:InvokeCommandAction Command="{Binding ButtonKeyDown}"
CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"/>
</Interactivity:EventTrigger>
</Interactivity:Interaction.Triggers>
注意界面要引用:
xmlns:Interactivity=http://schemas.microsoft.com/expression/2010/interactivity
Prism.Modularity: 模块定义,加载,管理
继承 IModule
public class PositionModule : IModule
{
// 其他内容
///必须实现 Initialize ,注册该模块中使用的服务,vm 等。提供主程序中使用。
public void Initialize()
{
.....
this.container.RegisterType<IOrdersViewModel, OrdersViewModel>();
this.container.RegisterType<IOrdersView, OrdersView>();
this.container.RegisterType<IOrderCompositeViewModel, OrderCompositeViewModel>();
this.container.RegisterType<IPositionSummaryViewModel, PositionSummaryViewModel>();
this.container.RegisterType<IPositionPieChartViewModel, PositionPieChartViewModel>();
this.regionManager.RegisterViewWithRegion(RegionNames.MainRegion,
() => this.container.Resolve<PositionSummaryView>());
this._ordersController = this.container.Resolve<OrdersController>();
}
}
主程序中使用方法:
在 Bootstrapper 中使用:
public class StockTraderRIBootstrapper : UnityBootstrapper
{
protected override void ConfigureModuleCatalog()
{
base.ConfigureModuleCatalog();
ModuleCatalog moduleCatalog = (ModuleCatalog)this.ModuleCatalog; moduleCatalog.AddModule(typeof(StockTraderRI.Modules.Position.PositionModule)); moduleCatalog.AddModule(typeof(StockTraderRI.Modules.News.NewsModule));
}
protected override DependencyObject CreateShell()
{
// Use the container to create an instance of the shell.
Shell view = this.Container.TryResolve<Shell>();
view.DataContext = new ShellViewModel();
return view;
}
protected override void InitializeShell()
{
base.InitializeShell();
App.Current.MainWindow = (Window)this.Shell;
App.Current.MainWindow.Show();
}
protected override Prism.Regions.IRegionBehaviorFactory ConfigureDefaultRegionBehaviors()
{
var factory = base.ConfigureDefaultRegionBehaviors();
return factory;
}
}
这是通过代码实现的,也可以通过 xml 配置文件实现
protected override IModuleCatalog CreateModuleCatalog() { return ModuleCatalog.CreateFromXaml(new Uri("/MyProject;component/ModulesCatalog.xaml", UriKind.Relative)); }