《Prism 5.0源码走读》Bootstrapper

Prism框架需要在应用程序启动的时候进行一些初始化的工作,Bootstrapper就是来做这些的,是其切入点。

Bootstrapper主要要做的事有:创建和配置module catalog,创建DI Container,为UI配置默认的region适配器,创建和初始化shell以及初始化module。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/// <summary>
/// Base class that provides a basic bootstrapping sequence and hooks
/// that specific implementations can override
/// </summary>
/// <remarks>
/// This class must be overridden to provide application specific configuration.
/// </remarks>
public abstract class Bootstrapper
{
    /// <summary>
    /// Gets the <see cref="ILoggerFacade"/> for the application.
    /// </summary>
    /// <value>A <see cref="ILoggerFacade"/> instance.</value>
    protected ILoggerFacade Logger { get; set; }
 
    /// <summary>
    /// Gets the default <see cref="IModuleCatalog"/> for the application.
    /// </summary>
    /// <value>The default <see cref="IModuleCatalog"/> instance.</value>
    protected IModuleCatalog ModuleCatalog { get; set; }
 
    /// <summary>
    /// Gets the shell user interface
    /// </summary>
    /// <value>The shell user interface.</value>
    protected DependencyObject Shell { get; set; }
 
    /// <summary>
    /// Create the <see cref="ILoggerFacade" /> used by the bootstrapper.
    /// </summary>
    /// <remarks>
    /// The base implementation returns a new TextLogger.
    /// </remarks>
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "The Logger is added to the container which will dispose it when the container goes out of scope.")]
    protected virtual ILoggerFacade CreateLogger()
    {
        return new TextLogger();
    }
 
    /// <summary>
    /// Runs the bootstrapper process.
    /// </summary>
    public void Run()
    {
        this.Run(true);
    }
 
 
    /// <summary>
    /// Creates the <see cref="IModuleCatalog"/> used by Prism.
    /// </summary>
    ///  <remarks>
    /// The base implementation returns a new ModuleCatalog.
    /// </remarks>
    protected virtual IModuleCatalog CreateModuleCatalog()
    {
        return new ModuleCatalog();
    }
 
    /// <summary>
    /// Configures the <see cref="IModuleCatalog"/> used by Prism.
    /// </summary>
    protected virtual void ConfigureModuleCatalog()
    {
    }
 
    /// <summary>
    /// Registers the <see cref="Type"/>s of the Exceptions that are not considered
    /// root exceptions by the <see cref="ExceptionExtensions"/>.
    /// </summary>
    protected virtual void RegisterFrameworkExceptionTypes()
    {
        ExceptionExtensions.RegisterFrameworkExceptionType(
            typeof(Microsoft.Practices.ServiceLocation.ActivationException));
    }
 
    /// <summary>
    /// Initializes the modules. May be overwritten in a derived class to use a custom Modules Catalog
    /// </summary>
    protected virtual void InitializeModules()
    {
        IModuleManager manager = ServiceLocator.Current.GetInstance<IModuleManager>();
        manager.Run();
    }
 
    /// <summary>
    /// Configures the default region adapter mappings to use in the application, in order
    /// to adapt UI controls defined in XAML to use a region and register it automatically.
    /// May be overwritten in a derived class to add specific mappings required by the application.
    /// </summary>
    /// <returns>The <see cref="RegionAdapterMappings"/> instance containing all the mappings.</returns>
    protected virtual RegionAdapterMappings ConfigureRegionAdapterMappings()
    {
        RegionAdapterMappings regionAdapterMappings = ServiceLocator.Current.GetInstance<RegionAdapterMappings>();
        if (regionAdapterMappings != null)
        {
            regionAdapterMappings.RegisterMapping(typeof(Selector), ServiceLocator.Current.GetInstance<SelectorRegionAdapter>());
            regionAdapterMappings.RegisterMapping(typeof(ItemsControl), ServiceLocator.Current.GetInstance<ItemsControlRegionAdapter>());
            regionAdapterMappings.RegisterMapping(typeof(ContentControl), ServiceLocator.Current.GetInstance<ContentControlRegionAdapter>());
        }
 
        return regionAdapterMappings;
    }
 
    /// <summary>
    /// Configures the <see cref="IRegionBehaviorFactory"/>.
    /// This will be the list of default behaviors that will be added to a region.
    /// </summary>
    protected virtual IRegionBehaviorFactory ConfigureDefaultRegionBehaviors()
    {
        var defaultRegionBehaviorTypesDictionary = ServiceLocator.Current.GetInstance<IRegionBehaviorFactory>();
 
        if (defaultRegionBehaviorTypesDictionary != null)
        {
            defaultRegionBehaviorTypesDictionary.AddIfMissing(BindRegionContextToDependencyObjectBehavior.BehaviorKey,
                                                              typeof(BindRegionContextToDependencyObjectBehavior));
 
            defaultRegionBehaviorTypesDictionary.AddIfMissing(RegionActiveAwareBehavior.BehaviorKey,
                                                              typeof(RegionActiveAwareBehavior));
 
            defaultRegionBehaviorTypesDictionary.AddIfMissing(SyncRegionContextWithHostBehavior.BehaviorKey,
                                                              typeof(SyncRegionContextWithHostBehavior));
 
            defaultRegionBehaviorTypesDictionary.AddIfMissing(RegionManagerRegistrationBehavior.BehaviorKey,
                                                              typeof(RegionManagerRegistrationBehavior));
 
            defaultRegionBehaviorTypesDictionary.AddIfMissing(RegionMemberLifetimeBehavior.BehaviorKey,
                                              typeof(RegionMemberLifetimeBehavior));
 
            defaultRegionBehaviorTypesDictionary.AddIfMissing(ClearChildViewsRegionBehavior.BehaviorKey,
                                              typeof(ClearChildViewsRegionBehavior));
 
            defaultRegionBehaviorTypesDictionary.AddIfMissing(AutoPopulateRegionBehavior.BehaviorKey,
                                              typeof(AutoPopulateRegionBehavior));
        }
 
        return defaultRegionBehaviorTypesDictionary;
    }
 
    /// <summary>
    /// Initializes the shell.
    /// </summary>
    protected virtual void InitializeShell()
    {
    }
 
    /// <summary>
    /// Run the bootstrapper process.
    /// </summary>
    /// <param name="runWithDefaultConfiguration">If <see langword="true"/>, registers default
    /// Prism Library services in the container. This is the default behavior.</param>
    public abstract void Run(bool runWithDefaultConfiguration);
 
    /// <summary>
    /// Creates the shell or main window of the application.
    /// </summary>
    /// <returns>The shell of the application.</returns>
    /// <remarks>
    /// If the returned instance is a <see cref="DependencyObject"/>, the
    /// <see cref="Bootstrapper"/> will attach the default <see cref="IRegionManager"/> of
    /// the application in its <see cref="RegionManager.RegionManagerProperty"/> attached property
    /// in order to be able to add regions by using the <see cref="RegionManager.RegionNameProperty"/>
    /// attached property from XAML.
    /// </remarks>
    protected abstract DependencyObject CreateShell();
 
    /// <summary>
    /// Configures the LocatorProvider for the <see cref="Microsoft.Practices.ServiceLocation.ServiceLocator" />.
    /// </summary>
    protected abstract void ConfigureServiceLocator();
}

  

posted on   AI应用技术  阅读(1281)  评论(4编辑  收藏  举报

努力加载评论中...

导航

点击右上角即可分享
微信分享提示