Extending PoshUI
PoshUI is designed to be extensible, allowing developers to add new controls and templates to meet their specific needs. This guide covers how to extend both the PowerShell and C# layers of the framework.
Adding a New PowerShell Control
To add a new control to a module, follow these steps:
- Create the Cmdlet: Add a new
.ps1file in thePublic/Controlsdirectory of the relevant module (e.g.,PoshUI.Wizard/Public/Controls/Add-UINewControl.ps1). - Define Parameters: Use standard PowerShell parameters for common properties (Step, Name, Label, Mandatory).
- Implement Logic: Inside the cmdlet, create a new
[UIControl]object and set its properties. - Register the Control: Add the new cmdlet name to the
FunctionsToExportarray in the module's.psd1manifest.
powershell
function Add-UINewControl {
[CmdletBinding()]
param($Step, $Name, $Label, $CustomProperty)
$wizardStep = $script:CurrentWizard.GetStep($Step)
$control = [UIControl]::new($Name, $Label, 'NewControlType')
$control.SetProperty('CustomProperty', $CustomProperty)
$wizardStep.AddControl($control)
return $control
}Extending the C# UI Engine
After adding the PowerShell cmdlet, you must update the C# Launcher project to render the new control.
- Create a ViewModel: Add a new class in
Launcher/ViewModels/Controls/that inherits fromParameterViewModel. - Create a View: Add a new XAML
UserControlinLauncher/Views/Controls/to define the visual appearance. - Register the Type: Update the
ReflectionService.cs(for Wizards) orJsonDefinitionLoader.cs(for Dashboards) to map the PowerShell control type string to your new ViewModel. - Data Binding: Ensure your XAML view correctly binds to the properties in your ViewModel.
Adding Custom Templates
PoshUI currently supports Wizard, Dashboard, and Workflow templates. To add a new template:
- Hardcode Template Type: Create a new module where the
UIDefinitionclass constructor sets a uniqueTemplateproperty. - Update MainWindow: In the C# project, modify
MainWindow.xamlandMainWindowViewModel.csto handle the new template type, likely by adding a newViewandViewModelfor the primary content area.
Best Practices for Extensions
- No External Dependencies: Ensure your new controls do not require any third-party DLLs or NuGet packages.
- Maintain Consistency: Follow the existing naming conventions and UI styling (Windows 11 look).
- Documentation: Always update the documentation in the
Docs/folder when adding new capabilities.
Next: Examples - All Controls
