2014-06-12 59 views
0

我有一个程序生成的System.Windows.Style类型的对象,我想将它导出为XAML代码,没有人知道任何可以做到的工具吗?这就好像是一种序列化,我猜想,但如果已经有东西出现,我不想自己写一个。从System.Windows.Style对象生成Xaml的工具?

谢谢!

回答

2

你可以使用XamlWriter

var style = new Style(typeof(Control)); 
style.Setters.Add(new Setter(Control.BackgroundProperty, Brushes.Red)); 

var xaml = XamlWriter.Save(style); 

上面的代码创建此XAML:

<Style TargetType="Control" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> 
    <Style.Resources> 
     <ResourceDictionary /> 
    </Style.Resources> 
    <Setter Property="Panel.Background"> 
     <Setter.Value> 
      <SolidColorBrush>#FFFF0000</SolidColorBrush> 
     </Setter.Value> 
    </Setter> 
</Style> 
+0

谢谢你,那其实并不是那么糟糕!我可以在PowerShell会话中使用它吗? – Ceottaki

+0

其实很容易从PowerShell中使用它: Add-Type -AssemblyName PresentationFramework [System.Windows.Markup.XamlWriter] :: Save($ myObj) – Ceottaki