前端:
先引用Microsoft.Practices.Prism,Microsoft.Practices.Prism.UnityExtensions,Microsoft.Practices.Unity
App类中启动界面:
protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); CommandingBootstrapper bootstrapper = new CommandingBootstrapper(); bootstrapper.Run(); }CommandingBootstrapper类定义如下
public class CommandingBootstrapper : UnityBootstrapper { protected override DependencyObject CreateShell() { return Container.Resolve<Shell>(); } protected override void InitializeShell() { base.InitializeShell(); #if SILVERLIGHT App.Current.RootVisual = (UIElement)this.Shell; #else App.Current.MainWindow = (Window)this.Shell; App.Current.MainWindow.Show(); #endif } protected override void ConfigureModuleCatalog() { base.ConfigureModuleCatalog(); ModuleCatalog moduleCatalog = (ModuleCatalog)this.ModuleCatalog; moduleCatalog.AddModule(typeof(OrderModule)); } }页面中需要如下代码:
<ItemsControl prism:RegionManager.RegionName="MainRegion" />
public class OrderModule : IModule
public class OrderModule : IModule { private readonly IRegionManager regionManager; private readonly IUnityContainer container; private readonly IRegionViewRegistry iRegionViewRegistry; public OrderModule( IUnityContainer container, IRegionManager regionManager,IRegionViewRegistry irvr ) { this.container = container; this.regionManager = regionManager; this.iRegionViewRegistry = irvr; } public void Initialize() { //这里注册了两个类型,当在下面 OrdersEditorPresentationModel类的构造函数中使用了IOrdersRepository接口,OrdersRepository类实现接口,所以也需要注册,如果在OrdersEditorPresentationModel构造函数中不使用接口,而是使用OrderRepository类,则可以不需要注册,即使OrderRepository实现IOrdersRepository接口也不需要注册。 this.container.RegisterType<IOrdersRepository, OrdersRepository>(new ContainerControlledLifetimeManager()); // 注册同一个Region多次,会在界面上出现同一个控件多次,此外使用IRegionManager 和IRegionViewRegistry 都可以注册Region this.regionManager.RegisterViewWithRegion( "MainRegion", () => this.container.Resolve<OrdersEditorView>() ); iRegionViewRegistry.RegisterViewWithRegion ("MainRegion",typeof(Views.OrdersEditorView )); iRegionViewRegistry.RegisterViewWithRegion("GlobalCommandsRegion", typeof(OrdersToolBar)); // Show the Orders Toolbar view in the shell's toolbar region. //this.regionManager.RegisterViewWithRegion( "GlobalCommandsRegion", // () => this.container.Resolve<OrdersToolBar>() ); } }
OrdersEditorPresentationModel类定义
public class OrdersEditorPresentationModel : INotifyPropertyChanged { private readonly IOrdersRepository ordersRepository; private readonly OrdersCommandProxy commandProxy; private ObservableCollection<OrderPresentationModel> _orders { get; set; } public OrdersEditorPresentationModel( IOrdersRepository ordersRepository, OrdersCommandProxy commandProxy ) { this.ordersRepository = ordersRepository; this.commandProxy = commandProxy; // Create dummy order data. this.PopulateOrders(); // Initialize a CollectionView for the underlying Orders collection. #if SILVERLIGHT this.Orders = new PagedCollectionView( _orders ); #else this.Orders = new ListCollectionView( _orders ); #endif // Track the current selection. this.Orders.CurrentChanged += SelectedOrderChanged; this.Orders.MoveCurrentTo(null); } public ICollectionView Orders { get; private set; } private void SelectedOrderChanged( object sender, EventArgs e ) { SelectedOrder = Orders.CurrentItem as OrderPresentationModel; NotifyPropertyChanged( "SelectedOrder" ); } public OrderPresentationModel SelectedOrder { get; private set; } private void PopulateOrders() { _orders = new ObservableCollection<OrderPresentationModel>(); foreach ( Services.Order order in this.ordersRepository.GetOrdersToEdit() ) { // Wrap the Order object in a presentation model object. var orderPresentationModel = new OrderPresentationModel( order ); _orders.Add( orderPresentationModel ); // Subscribe to the Save event on the individual orders. orderPresentationModel.Saved += this.OrderSaved; //TODO: 04 - Each Order Save command is registered with the application's SaveAll command. commandProxy.SaveAllOrdersCommand.RegisterCommand( orderPresentationModel.SaveOrderCommand ); } } private void OrderSaved( object sender, DataEventArgs<OrderPresentationModel> e ) { if (e != null && e.Value != null) { OrderPresentationModel order = e.Value; if ( this.Orders.Contains( order ) ) { order.Saved -= this.OrderSaved; //TODO: 05 - As each order is saved, it is unregistered from the application's SaveAll command. this.commandProxy.SaveAllOrdersCommand.UnregisterCommand( order.SaveOrderCommand ); // Remove saved orders from the collection. this._orders.Remove( order ); } } } #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged( string propertyName ) { if ( this.PropertyChanged != null ) { this.PropertyChanged( this, new PropertyChangedEventArgs( propertyName ) ); } } #endregion }用户控件定义
虽然没有地方显示调用用户控件的构造函数(这都是Prism完成的工作),但是用户控件中可以直接使用定义的Model来作为数据源,在调用如下的构造函数之前会去初始化一个OrdersEditorPresentationModel对象。所以当用户控件中有好多参数的时候,Prism都会去初始化里面的参数,在调用该构造函数。
public partial class OrdersEditorView : UserControl { public OrdersEditorView( OrdersEditorPresentationModel model ) { InitializeComponent(); // Set the presentation model as this views data context. DataContext = model; } }其他代码省略