使用PowerShell更改和保存XAML ResourceDictionary

时间:2015-04-28 11:29:37

标签: xaml powershell

我有文件ResourceDictionary.xaml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:MyApp">
    <Style x:Key="GridStyle" TargetType="Grid">
        <Setter Property="Background" Value="Red"></Setter>
    </Style>
    <x:String x:Key="AppTitle">My App</x:String>
</ResourceDictionary>

我想要更改背景属性和AppTitle。

输出ResourceDictionary2.xaml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:MyApp">
    <Style x:Key="GridStyle" TargetType="Grid">
        <Setter Property="Background" Value="Green"></Setter>
    </Style>
    <x:String x:Key="AppTitle">My App 2</x:String>
</ResourceDictionary>

如何使用Power Shell进行操作?

1 个答案:

答案 0 :(得分:1)

将文件作为XML文件读取,更改值,然后将其保存回文件:

[xml]$xml = Get-Content 'C:\path\to\ResourceDictionary.xaml'

$xml.ResourceDictionary.Style.Setter.Value = 'Green'
$xml.ResourceDictionary.'#text' = 'My App 2'

$xml.Save('C:\path\to\Output ResourceDictionary2.xaml')