C#WPF-使用XamlWriter

时间:2019-04-18 21:52:33

标签: c# wpf

我正在尝试使用XamlWriter将应用程序中所有合并的资源字典转储到字符串(后来写入文件)中。但是,下面的代码对一个资源字典抛出异常,然后仅对其余字典转储空字典:

StringBuilder sb = new StringBuilder();
int counter = 0;
foreach(var dic in Application.Current.Resources.MergedDictionaries)
{
    counter++;
    sb.AppendLine("Resource Dictionary #" + counter);
    try
    {
        sb.AppendLine(System.Windows.Markup.XamlWriter.Save(dic));
    }
    catch(Exception ex)
    {
        sb.AppendLine(ex.Message);
    }
    sb.AppendLine("----");
}

输出:

Resource Dictionary #1
Initialization of 'System.Windows.Controls.AdornedElementPlaceholder' threw an exception.
----
Resource Dictionary #2
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" />
----
Resource Dictionary #3
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" />
----
Resource Dictionary #4
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" />
----
Resource Dictionary #5
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" />
----
Resource Dictionary #6
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" />
----
Resource Dictionary #7
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" />
----
Resource Dictionary #8
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" />
----
Resource Dictionary #9
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" />
----
Resource Dictionary #10
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" />
----
Resource Dictionary #11
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" />
----

关于我为什么在这里得到空的ResourceDictionary标签的任何想法?

0 个答案:

没有答案