The Goal
Creating and rendering a simple WinUI3 GUI in PowerShell 7.5 which is based on .NET 9. Nothing complicated, just a window and a button in it, such as this XAML
<?xml version="1.0" encoding="utf-8"?>
<Window
x:Class="App1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<Button x:Name="myButton" Click="myButton_Click">Click Me</Button>
</StackPanel>
</Window>
What I’ve tried so far
I’ve created a fully working WinUI3 app in Visual Studio 2022 using the latest WindowsApps SDK. Then inside of the Winui3 projectApp1binx64Debugnet8.0-windows10.0.22621.0win-x64
folder I’ve tried loading all of the DLLs
in there in PowerShell. Some 200 dlls loaded and a few failed to load.
In PowerShell now I have access to the type [Microsoft.UI.Xaml.Window]
but when I try to create an instance of it
New-Object -TypeName Microsoft.UI.Xaml.Window
# Or
[Microsoft.UI.Xaml.Window]::new()
I get the following error
MethodInvocationException: Exception calling ".ctor" with "0" argument(s): "The type initializer for '_IWindowFactory' threw an exception."
It looks like there is a dependency missing for _IWindowFactory
.
Other people have tried this too and had similar results. Another issue related to this problem asking for some guidance from Microsoft.
I don’t know how Visual Studio does this that makes it all so easy and automated, but I believe I need to do the same tasks manually in PowerShell.