2014-06-12 61 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