Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 8583

WPF Insert Absolute Path for Resource Dictionary without using 'here-string'.

$
0
0

After much research I have discovered a way to load dynamically the required absolute path into a separate xaml Resource Dictionary file, as PowerShell seems unable to find any relative path to this file. Apparently it is possible to use the [xml] class as illustrated here:

function Add-PSScriptRoot ($file) {       #From 'Windows PowerShell In Action v2' (Payette)
   $caller = Get-Variable -Value -Scope 1 MyInvocation
   $caller.MyCommand.Definition |
      Split-Path -Parent |
         Join-Path -Resolve -ChildPath $file
}
 
$xamlPath = Add-PSScriptRoot Test-Xaml.xaml
$resource = [xml](Get-Content $xamlPath)
$dictItem = $resource.window.'Window.Resources'.ResourceDictionary.'ResourceDictionary.MergedDictionaries'.ResourceDictionary
$dictItem.Source = [string](Add-PSScriptRoot Resource.xaml)
$resource.Save($xamlPath)

$stream = [System.IO.StreamReader] $xamlPath
$form = [System.Windows.Markup.XamlReader]::Load($stream.BaseStream)
$stream.Close()
...
Is there a better way to achieve this though?


Viewing all articles
Browse latest Browse all 8583

Trending Articles