0

I got an XAML document as follows:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:TLApp.Themes.Schemes">

    <SolidColorBrush x:Key="list_expend_color" Color="#444444" />
    <SolidColorBrush x:Key="list_separator" Color="#8c8c8c" />
    <SolidColorBrush x:Key="tab_icon_color_selected" Color="#bfbfbf" />

    <SolidColorBrush x:Key="button_dark" Color="#616161" />
    <SolidColorBrush x:Key="button_light" Color="#919191" />
    <SolidColorBrush x:Key="text_color_custom" Color="#616161" />
    <SolidColorBrush x:Key="text_color_title" Color="#bfbfbf" />
    <SolidColorBrush x:Key="startscreen_background" Color="#1b1b1b" />

</ResourceDictionary>

I want to create new color elements based on the given color for each solidbrush. But the first thing i don't get clear is why am I not able to select any SolidColorBrush element? I ve tried Descendants("SolidColorBrush") and Elements("SolidColorBrush") none of them are working as expected. And if i use Elements() without selector i get them all, what i dont want cause i want to add elements, however if I now start adding color elements like shown in my code each Color element will have the "x"-Namespace attached, so how to fix that?

XDocument xdoc = XDocument.Load(file);

XNamespace x = "http://schemas.microsoft.com/winfx/2006/xaml";

foreach (XElement brush in xdoc.Root.Descendants("SolidColorBrush"))
{
    var key = brush.Attribute(x+"Key");
    var val = brush.Attribute("Color");

    string color_key = "cl_" + key.Value;
    string color_val = val.Value;

    xdoc.Root.Add(
        new XElement("Color",
            new XAttribute(x+"Key", color_key),
            new XText(color_val)
        )
    );
}

xdoc.Save(file);
4

1 回答 1

1

您还需要定义默认命名空间:

XNamespace ns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";

foreach (XElement brush in xdoc.Root.Elements(ns + "SolidColorBrush")) { ... }
于 2013-10-22T09:16:40.987 回答