WinUI3 是微软提供的用于构建现代 Windows 应用程序的用户界面框架。通常,WinUI3 应用程序是用 C# 或其他 .NET 语言编写的。不过,使用 PowerShell 创建 WinUI3 GUI 也是可以的。
MainWindow.xaml
,内容如下:<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WinUI3 in PowerShell" Height="300" Width="300">
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<Button x:Name="myButton" Click="myButton_Click">Click Me</Button>
</StackPanel>
</Window>
MainWindow.xaml.cs
中:private void myButton_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Button clicked!");
}
Add-Type
命令加载 WinUI3 相关的 DLL:Add-Type -Path "path\to\Microsoft.WinUI.dll"
<em># 加载其他需要的 DLL</em>
$WindowType = (Add-Type -AssemblyName PresentationFramework)::Windows::Window
$window = $WindowType::new()
$xaml = @"
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WinUI3 in PowerShell" Height="300" Width="300">
<!-- Your XAML content here -->
</Window>
"@
$reader = New-Object System.Xml.XmlNodeReader
$reader.Content = $xaml
$window = [System.Windows.Markup.XamlReader]::Load($reader)
显示窗口并启动应用程序的主循环:
<code>$window.ShowDialog()
PowerShell 中的 WinUI3 支持不是官方支持的,可能会遇到兼容性和稳定性问题。因此,调试 PowerShell 脚本和 WinUI3 应用程序需要一些时间和耐心。
原文链接:https://www.tellmethecode.com/create-winui3-gui-in-powershell.html
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。