PowerShell XAML |更新样式编程失败



我正在编写一个简单的自动化工具,使用按钮样式翻转按钮的颜色,但在PowerShell中,按钮只是重置回Windows默认样式。我试过用谷歌搜索,但我一直在寻找不再简单工作的答案(可能是由于更新的PS5)。

下面是我的代码:

PowerShell

## v0.09 New CORE ##############
################################
$dev = $true
$ver = 0.09
$DIR_APP = $PSScriptRoot
cls
## Console
################################################################
$Script:showWindowAsync = Add-Type -MemberDefinition @"
[DllImport("user32.dll")]
public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
"@ -Name "Win32ShowWindowAsync" -Namespace Win32Functions -PassThru
if(!$DEV){ $null = $showWindowAsync::ShowWindowAsync((Get-process -Id $pid).MainWindowHandle, 0); }
function Get-KeyState([uint16]$k)
{
$s = '[DllImport("user32.dll")]public static extern short GetKeyState(int nVirtKey);'
$t = Add-Type -MemberDefinition $s -Name User32 -Namespace GetKeyState -PassThru
return [bool]($t::GetKeyState($k) -band 0x80)
}

## Startup Framework
################################################################
Add-Type -Assembly PresentationFramework
Add-Type -Assembly PresentationCore
$app = [Windows.Application]::New()
[xml]$XAML = Get-Content ($DIR_APP + "Z4.xaml")
$Read = (New-Object System.Xml.XmlNodeReader $XAML);
try{ $Script:core = [Windows.Markup.XamlReader]::Load($Read) }catch{ write-host "[ERR]::XAML >> CORE_MAIN" -f "red"; exit; }
$XAML.SelectNodes("//*[@Name]") | %{ Set-Variable -Name ($_.Name) -Value $Script:core.FindName($_.Name) -Scope Script; }
$core.Add_Activated({ Initialization })
## Framework
################################################################
function Initialization()
{
# Button = 2 + INCOMPLETE
$coreAppName.Text = "Core v" + $ver;

$coreClose.Add_Click({ $core.Close(); });
$coreTest.Add_Click({ ClickTest });
$core.show();
}
## Application
################################################################

## GUI
################################################################
function ClickTest()
{
$coreTest.Style = $core.tryFindResource("BlueMe");
}
$app.Run($core);

XAML

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title=""
MinWidth="1"
MinHeight="1"

UseLayoutRounding="True"
SnapsToDevicePixels="True"
SizeToContent="WidthAndHeight"
ResizeMode="NoResize"

Topmost="True"
Background="#111"
WindowStartupLocation="CenterScreen"
WindowStyle="None">

<Grid Background="#111" Margin="4">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="24"/>
</Grid.ColumnDefinitions>

<Grid.RowDefinitions>
<RowDefinition Height="24"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>

<Grid.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="#FFF"/>
<Setter Property="Margin" Value="4, 4, 0, 0"/>
</Style>

<Style x:Key="CloseRed" TargetType="{x:Type Button}">
<Setter Property="Background" Value="#111"/>
<Setter Property="HorizontalAlignment" Value="Right"/>
<Setter Property="Height" Value="23"/>
<Setter Property="Width" Value="23"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}" BorderBrush="White" BorderThickness="0">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#300"/>
</Trigger>
</Style.Triggers>
</Style>

<Style TargetType="{x:Type Button}">
<Setter Property="Foreground" Value="#FFF"/>
<Setter Property="Background" Value="#222"/>
<Setter Property="Margin" Value="4"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}" BorderBrush="White" BorderThickness="0">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#333"/>
</Trigger>
</Style.Triggers>
</Style>

<Style x:Key="BlueMe" TargetType="{x:Type Button}">
<Setter Property="Foreground" Value="#FFF"/>
<Setter Property="Background" Value="#138"/>
<Setter Property="Margin" Value="4"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}" BorderBrush="White" BorderThickness="0">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#14A"/>
</Trigger>
</Style.Triggers>
</Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="#222"/>
<Setter Property="BorderBrush" Value="#333"/>
<Setter Property="Foreground" Value="#FFF"/>
<Setter Property="Margin" Value="4, 0, 4, 0"/>
</Style>
</Grid.Resources>

<TextBlock Grid.Row="0" Grid.Column="0" Name="coreAppName" Text="Core" Margin="4,2" FontSize="12"/>
<Button    Grid.Row="0" Grid.Column="1" Name="coreClose" Style="{StaticResource CloseRed}" HorizontalAlignment="Right" VerticalAlignment="Top">
<Path Margin="-8,-8,0,0" Fill="#F00" Data="M 26.9166,22.1667L 37.9999,33.25L 49.0832,22.1668L 53.8332,26.9168L 42.7499,38L 53.8332,49.0834L 49.0833,53.8334L 37.9999,42.75L 26.9166,53.8334L 22.1666,49.0833L 33.25,38L 22.1667,26.9167L 26.9166,22.1667 Z">
<Path.LayoutTransform>
<ScaleTransform ScaleX="0.4" ScaleY="0.4"/>
</Path.LayoutTransform>
</Path>
</Button>

<Button Grid.Row="1" Grid.ColumnSpan="2" Name="coreTest" Content="Test" Width="96" Height="48"/>
</Grid>
</Window>```

经过多次尝试和错误(以及阅读MS XAML KB),我弄清楚了。

样式和键必须在窗口中。$core.tryFindResource("Key")的资源查找。

最新更新