Nuget Package:
PM> Install-Package HBD.WPF.Shell
As the Workspace for WPF had been introduced a few day back. So is this post I would like to show you how to get started with that application.
I. Module Implementation.
1. Add New Project
- Open Visual Studio and create a new WPF User Control Library project with project name is WPF.Demo.Module.
- Install the latest version of HBD.WPF.Shell from Nuget.
- Add a new class named DemoStartup and inherited from HBD.WPF.Shell.Modularity.WpfModuleBase and then override 2 methods (GetStartUpViewTypes and MenuConfiguration(IShellMenuService menuSet)) as below.
- Remember to add the ModuleExport attribute into your DemoStartup class.
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 |
[Prism.Mef.Modularity.ModuleExport(typeof(DemoStartup))] [PartCreationPolicy(CreationPolicy.Shared)] public class DemoStartup : WpfModuleBase { /// <summary> /// The list of ViewType will be open automatically when app started. /// </summary> /// <returns></returns> protected override IEnumerable<IViewInfo> GetStartUpViewTypes() { yield return new ViewInfo(typeof(View1)); yield break; } /// <summary> /// The Main menu configuration. /// </summary> /// <param name="menuSet"></param> protected override void MenuConfiguration(IShellMenuService menuSet) { base.MenuConfiguration(menuSet); menuSet.Menu("Demo") .Children .AddNavigation("View 1") .For<View1>(); } } |
2. Setup the Module_*.json
- Add a new json named Module_Demo.json with the below content.
1 2 3 4 5 6 7 8 9 10 11 |
{ "Name": "WPF.Demo.Module", "Description": "The Demo Module for HBD.WPF.Shell", "Version": "1.0.0-beta1", "IsEnabled": true, //or false "AllowToManage": true, //The list of assemblies that you want to be exposed to the EMF. "AssemplyFiles": [ "WPF.Demo.Module.dll" ]//The name of Module dll. } |
- Set the build action of this config file is Copy Alway.
3. Add a new User control
- Add a new class named ViewModel1.cs inside folder ViewModels as below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
[Export] public class ViewModel1 : ViewModelBase { /// <summary> /// Set View tile and header. /// </summary> /// <param name="viewTitle"></param> /// <param name="viewHeader"></param> protected override void SetViewTitle(out string viewTitle, out string viewHeader) { viewTitle = "View 1"; viewHeader = "The View 1"; } } |
- Add a new User Control named View1.xaml inside Views folder with the code behind as below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[Export] [PartCreationPolicy(CreationPolicy.Shared)] public partial class View1 : UserControl { public View1() { InitializeComponent(); } [Import] public ViewModel1 Model { set { this.DataContext = value; } } } |
4. Setup the Public folder
- Copy folder [Solution Directory]\packages\HBD.WPF.Shell\tools\Shell to [Solution Directory]\Published\Shell.
- Right click on the project select Properties
- Add below code into Post-build event
1 2 3 4 5 |
if not exist "$(SolutionDir)Published\Shell\Modules\$(ProjectName)" mkdir "$(SolutionDir)Published\Shell\Modules\$(ProjectName)" xcopy "$(TargetDir)$(ProjectName).*" "$(SolutionDir)Published\Shell\Modules\$(ProjectName)" /s /y /r xcopy "$(TargetDir)*.json" "$(SolutionDir)Published\Shell\Modules\$(ProjectName)" /s /y /r |
- Set Start external program under Debug to [Solution Directory]\Published\Shell\HBD.WPF.Shell.exe
- And set Working directory to [Solution Directory]\Published\Shell folder.
5. Run the application with this Module.
- After finished all above steps, the project shall look like below.
- Build and Run the application you will see the new Demo menu had been added. When clicking on the child menu item the View1 will be open on the Tab control.
II. Source Code
Download the WPF.Demo.Module here.
Hope my Workspace is useful.