ShellBootstrapper 繼承 UnityBootstrapper,重寫 CreateShell、GetModuleEnumeratorApp.xaml 移除 StartupUri,在 App 建構子呼叫 new Bootstrapper().Run()internal class Bootstrapper : UnityBootstrapper {
protected override DependencyObject CreateShell() {
var shell = new Shell(); shell.Show(); return shell;
}
protected override IModuleEnumerator GetModuleEnumerator() {
return new StaticModuleEnumerator(); // 暫時用靜態,稍後改配置檔
}
}
HelloWorldModule,參考 CALIModule,Initialize 顯示 MessageBoxpublic class HelloWorldModule : IModule {
public void Initialize() { MessageBox.Show("this is hello module"); }
}
App.config,設定 <modules> 區段ConfigurationModuleEnumeratorxcopy 複製輸出至 Shell bin\Debug<configSections>
<section name="modules" type="Microsoft.Practices.Composite.Modularity.ModulesConfigurationSection, Microsoft.Practices.Composite"/>
</configSections>
<modules>
<module assemblyFile="HelloWorldModule.dll" moduleType="HelloWorldModule.HelloWorldModule" moduleName="HelloWorldModule"/>
</modules>
protected override IModuleEnumerator GetModuleEnumerator() {
var configStore = new ConfigurationStore();
return new ConfigurationModuleEnumerator(configStore);
}
ItemsControl 並設定 cal:RegionManager.RegionName="MainRegion"HelloWorldView (UserControl)IRegionManager,Initialize 中取得 Region、Add/Activate 視圖public class HelloWorldModule : IModule {
private readonly IRegionManager _regionManager;
public HelloWorldModule(IRegionManager regionManager) { _regionManager = regionManager; }
public void Initialize() {
var mainRegion = _regionManager.Regions["MainRegion"];
var view = new HelloWorldView();
mainRegion.Add(view);
mainRegion.Activate(view);
}
}
完成!啟動即可見到視圖顯示於 Shell 內。此範例為簡化示範,實際專案建議遵循 MVP/MVC 模式組織模組內部。