Skip to main content

Host a custom window from XAML

Show-FluenceWindow displays a XAML document or a window built in PowerShell. It handles library loading, the WPF application, and the modal window lifetime. Load only trusted XAML: XamlReader constructs objects and does not sandbox the document.

Start with a XAML string​

Declare the WPF and Fluence namespaces. A fluence:FluenceWindow root supplies Fluence window chrome; a non-window root is hosted in a new FluenceWindow.

$xaml = @'
<fluence:FluenceWindow
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:fluence="http://schemas.fluencewpf.com"
Title="Sample" Width="480" Height="240">
<StackPanel Margin="24">
<TextBlock Text="A PowerShell window" Foreground="{DynamicResource TextFillColorPrimaryBrush}" />
<fluence:Button x:Name="CloseButton" Content="Close" Margin="0,16,0,0" />
</StackPanel>
</fluence:FluenceWindow>
'@

Show-FluenceWindow -Xaml $xaml -Initialize {
param($Window, $Data)
$Window.FindName('CloseButton').add_Click({
Close-FluenceWindow -Window $Window
}.GetNewClosure())
}

The call blocks until the window closes. Use DynamicResource for theme colors and brushes so open controls can respond to theme and accent changes. See the theme key catalog.

Move layout into a file​

The module can host XAML in a Fluence window in either theme.

XAML-hosted Fluence window in light mode XAML-hosted Fluence window in dark mode

Store a complete XAML document beside your script and pass its path. The runnable example loads MainWindow.xaml.

Show-FluenceWindow -XamlPath (Join-Path $PSScriptRoot 'MainWindow.xaml') -WatchSystemTheme -Initialize {
param($Window, $Data)
# Find named controls and attach handlers here.
}

-WatchSystemTheme watches Windows theme changes while this window remains open. With the requested theme at Auto, an open window follows the Windows Light and Dark setting.

Attach handlers and retain state​

-Initialize receives the window and the -Data hashtable. Find controls by x:Name, then attach handlers through add_<Event>. Capture local variables with .GetNewClosure().

Show-FluenceWindow -Xaml $xaml -Data @{ Clicks = 0 } -Initialize {
param($Window, $Data)
$button = $Window.FindName('CloseButton')
$button.add_Click({
$Data.Clicks++
Close-FluenceWindow -Window $Window -Result $Data.Clicks
}.GetNewClosure())
}

On an MTA caller, -Initialize, -Content, and event handlers run in a module-owned STA runspace. They cannot read caller variables or functions. Pass state through -Data and keep callbacks self-contained. On an inline STA caller, they run on the caller's WPF thread. See UI threading.

Bind PowerShell objects to a DataGrid​

A Fluence DataGrid displays ordinary PowerShell objects. Declare one column per property, with each Binding naming a property of the row objects. In -Initialize, set ItemsSource to an array of [pscustomobject] rows. A header click sorts by that column. EmptyContent shows until rows are set.

$xaml = @'
<fluence:FluenceWindow
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:fluence="http://schemas.fluencewpf.com"
Title="Tasks" Width="560" Height="320">
<fluence:DataGrid x:Name="TaskGrid" Margin="24" AutoGenerateColumns="False" IsReadOnly="True">
<fluence:DataGrid.EmptyContent>
<TextBlock Text="No tasks." Foreground="{DynamicResource TextFillColorSecondaryBrush}" />
</fluence:DataGrid.EmptyContent>
<fluence:DataGrid.Columns>
<DataGridTextColumn Header="Task" Binding="{Binding Name}" Width="2*" />
<DataGridTextColumn Header="Owner" Binding="{Binding Owner}" Width="*" />
<DataGridTextColumn Header="Progress" Binding="{Binding Progress, StringFormat='{}{0}%'}" Width="*" />
<DataGridCheckBoxColumn Header="Done" Binding="{Binding Done}" Width="Auto" />
</fluence:DataGrid.Columns>
</fluence:DataGrid>
</fluence:FluenceWindow>
'@

Show-FluenceWindow -Xaml $xaml -Initialize {
param($Window, $Data)
$grid = $Window.FindName('TaskGrid')
$grid.ItemsSource = @(
[pscustomobject]@{ Name = 'Collect inventory'; Owner = 'Avery'; Progress = 100; Done = $true }
[pscustomobject]@{ Name = 'Patch servers'; Owner = 'Jordan'; Progress = 60; Done = $false }
[pscustomobject]@{ Name = 'Rotate certificates'; Owner = 'Riley'; Progress = 25; Done = $false }
)
$grid.add_SelectionChanged({
$task = $grid.SelectedItem
if ($null -ne $task) {
$Window.Title = "Selected: $($task.Name), owned by $($task.Owner)"
}
}.GetNewClosure())
}

SelectedItem returns the selected row object, so the handler reads its properties directly. The Data page in the ControlsTour example binds five PowerShell objects to a sortable grid and reports the selected row in a shared InfoBar. Its event handlers capture local controls and state with .GetNewClosure(), including the callback that updates the status bar. See the tour guide for all ten categories and their Light and Dark captures.

Add an in-app acrylic surface​

In Show-FluenceWindow XAML, place a named source visual before fluence:AcrylicElement as an overlapping sibling. Bind the host's BackdropSource to that name and set Recipe="{DynamicResource AcrylicInAppDefaultRecipe}". Add the controls you want to keep sharp as the host's content. The AcrylicElement example shows the layout. The Controls Tour Acrylic page demonstrates moving the source and forcing a solid fill.

Return a result​

Close-FluenceWindow -Result <value> stores a value before closing. Show-FluenceWindow returns that value; a title bar close returns $null. Add -PassThru to return a Fluence.WindowResult with Result and Closed properties.

$outcome = Show-FluenceWindow -Xaml $xaml -PassThru -Initialize {
param($Window, $Data)
$Window.FindName('CloseButton').add_Click({
Close-FluenceWindow -Window $Window -Result 'Done'
}.GetNewClosure())
}
$outcome.Result

Configure the window​

When the root is a non-window element, use -Title, -Width, -Height, and the chrome options on Show-FluenceWindow. These options also apply to a FluenceWindow root. Common options include -Topmost, -ResizeMode, -SizeToContent, -StartupLocation, -CornerStyle, and switches for title and caption buttons.

Use -Title and -TitleBarIcon to customize the host title bar; see title-bar customization for accepted icon formats. -ShowIcon:$false hides the built-in icon. A custom TitleBar or -ExtendsContentIntoTitleBar hides the built-in icon and text so custom content can take their place. For a XAML FluenceWindow root, these parameters configure its host window icon and title-bar text.

Show-FluenceWindow -Xaml $xaml -Title 'Setup' -TitleBarIcon "$PSScriptRoot\assets\app.ico" -ResizeMode NoResize -CornerStyle RoundSmall -NoMaximizeButton

-Owner works when the owner is on the Fluence UI thread. A cross-thread owner on an MTA host produces a warning and the new window appears without a parent.

Use -Content { param($Window, $Data) ... } when building the body in PowerShell rather than XAML. The block must set $Window.Content and can attach handlers in the same way as -Initialize.

Command details​