| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- # WPF 覆盖字典切换机制实验(自包含、-STA 进程主线程直跑)
- $ErrorActionPreference = "Continue"
- try {
- Add-Type -AssemblyName PresentationFramework
- Add-Type -AssemblyName PresentationCore
- Add-Type -AssemblyName WindowsBase
- $base = "D:\Program\TeamAAS\ExE\win-x64"
- $hcDll = (Get-ChildItem $base -Recurse -Filter "HandyControl.dll" -ErrorAction SilentlyContinue | Select-Object -First 1).FullName
- Write-Output ("HC DLL: " + $hcDll)
- [void][System.Reflection.Assembly]::LoadFrom($hcDll)
- $app = New-Object System.Windows.Application
- $skinDark = New-Object System.Windows.ResourceDictionary
- $skinDark.Source = New-Object System.Uri("pack://application:,,,/HandyControl;component/Themes/SkinDark.xaml")
- $theme = New-Object System.Windows.ResourceDictionary
- $theme.Source = New-Object System.Uri("pack://application:,,,/HandyControl;component/Themes/Theme.xaml")
- [void]$app.Resources.MergedDictionaries.Add($skinDark)
- [void]$app.Resources.MergedDictionaries.Add($theme)
- $border = New-Object System.Windows.Controls.Border
- $border.SetResourceReference([System.Windows.Controls.Border]::BackgroundProperty, "RegionBrush")
- function Get-Bg { param($b)
- $brush = $b.GetValue([System.Windows.Controls.Border]::BackgroundProperty)
- if ($brush -is [System.Windows.Media.SolidColorBrush]) { return $brush.Color.ToString() }
- elseif ($null -eq $brush) { return "<null>" }
- else { return $brush.GetType().Name }
- }
- Write-Output ("1 初始(暗): " + (Get-Bg $border))
- # 第一次覆盖:Insert(0) 红色
- $ov1 = New-Object System.Windows.ResourceDictionary
- $ov1["RegionBrush"] = New-Object System.Windows.Media.SolidColorBrush([System.Windows.Media.Color]::FromRgb(0xFF,0,0))
- $app.Resources.MergedDictionaries.Insert(0, $ov1)
- Write-Output ("2 Insert 红覆盖: " + (Get-Bg $border))
- # 第二次切换:Remove 旧 + Insert(0) 蓝色(模拟 UpdateTheme 的 Remove+Insert 序列)
- [void]$app.Resources.MergedDictionaries.Remove($ov1)
- $ov2 = New-Object System.Windows.ResourceDictionary
- $ov2["RegionBrush"] = New-Object System.Windows.Media.SolidColorBrush([System.Windows.Media.Color]::FromRgb(0,0,0xFF))
- $app.Resources.MergedDictionaries.Insert(0, $ov2)
- Write-Output ("3 Remove+Insert 蓝覆盖: " + (Get-Bg $border))
- # 第三次:不 Remove 直接 Insert(0) 绿色
- $ov3 = New-Object System.Windows.ResourceDictionary
- $ov3["RegionBrush"] = New-Object System.Windows.Media.SolidColorBrush([System.Windows.Media.Color]::FromRgb(0,0xFF,0))
- $app.Resources.MergedDictionaries.Insert(0, $ov3)
- Write-Output ("4 再 Insert 绿覆盖: " + (Get-Bg $border))
- # 对照:移除全部覆盖回官方
- [void]$app.Resources.MergedDictionaries.Remove($ov3)
- [void]$app.Resources.MergedDictionaries.Remove($ov2)
- Write-Output ("5 移除全部覆盖: " + (Get-Bg $border))
- } catch {
- Write-Output ("EXCEPTION: " + $_.Exception.GetType().FullName + " :: " + $_.Exception.Message)
- if ($_.Exception.InnerException) { Write-Output ("INNER: " + $_.Exception.InnerException.Message) }
- }
|