有時候,我們需要將WPF中的控制項自身的XAML代碼輸出成文本,那麼,我們可以使用System.Windows.Markup.XamlWriter.Save()方法來完成此任務。關於XamlWriter.Save()的樣本,我曾經在“在WPF中,如何得到任何Object對象的XAML代碼?”(http://blog.csdn.net/johnsuna/archive/2007/11/23/1899875.aspx)Blog中有所介紹,此處不再贅述。
使用上述方法時,我們發現,輸出的XAML代碼並不“標準”,不是格式化的XML代碼,我們看這樣的代碼時,會有一種頭暈的感覺。那麼,怎樣輸出成已格式化過的XAML代碼呢?
答案是藉助System.Xml.XmlWriter及對System.Xml.XmlWriterSettings設定來解決。
代碼:
以下程式碼範例示範在txtBoxXamlCode文字框中顯示名為“canvasContent”的Canvas控制項的自身XAML代碼:
// Line.xaml中的部分關鍵代碼:
<Canvas Width="630" Height="400" Name="canvasContent">
// Line.xaml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using Shapes = System.Windows.Shapes;
using System.Windows.Markup;
using System.Xml;
namespace BrawDraw.Com.Book.WPF.Demo.Lines
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class LineDemo : Window
{
public LineDemo()
{
InitializeComponent();
InitCanvasChildren();
}
private void InitCanvasChildren()
{
double angle = 120;
double centerX = 200;
double centerY = 200;
double strokeThickness = 10;
for (int i = 0; i < 360; i += (int)angle)
{
Shapes.Line lineRotate = new System.Windows.Shapes.Line();
lineRotate.Stroke = new SolidColorBrush(Colors.Black);
lineRotate.X1 = 0;
lineRotate.Y1 = centerY;
lineRotate.X2 = centerX;
lineRotate.Y2 = centerY;
lineRotate.StrokeDashArray = new DoubleCollection(new double[] { 0, 3 });
lineRotate.StrokeThickness = strokeThickness;
lineRotate.StrokeDashCap = PenLineCap.Round;
lineRotate.StrokeStartLineCap = PenLineCap.Round;
lineRotate.StrokeEndLineCap = PenLineCap.Round;
RotateTransform rt = new RotateTransform(i, centerX, centerY);
lineRotate.RenderTransform = rt;
canvasContent.Children.Add(lineRotate);
}
}
// 輸出顯示未格式化的XAML代碼
private void btnViewXaml_Click(object sender, RoutedEventArgs e)
{
txtBoxXamlCode.Text = XamlWriter.Save(canvasContent);
}
// 輸出顯示已格式化過的XAML代碼
private void btnViewFormattedXaml_Click(object sender, RoutedEventArgs e)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = new string(' ', 4);
settings.NewLineOnAttributes = true;
StringBuilder sb = new StringBuilder();
XmlWriter xmlWriter = XmlWriter.Create(sb, settings);
XamlWriter.Save(canvasContent, xmlWriter);
txtBoxXamlCode.Text = sb.ToString();
xmlWriter.Close();
sb = null;
}
}
}
運行:
點擊顯示文字為XamlCode的按鈕後,輸出的代碼為:
<Canvas Name="canvasContent" Width="630" Height="400" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"><Line X1="0" Y1="200" X2="200" Y2="200" Stroke="#FF000000" StrokeThickness="10" StrokeStartLineCap="Round" StrokeEndLineCap="Round" StrokeDashCap="Round" StrokeDashArray="0 3"><Line.RenderTransform><RotateTransform Angle="0" CenterX="200" CenterY="200" /></Line.RenderTransform></Line><Line X1="0" Y1="200" X2="200" Y2="200" Stroke="#FF000000" StrokeThickness="10" StrokeStartLineCap="Round" StrokeEndLineCap="Round" StrokeDashCap="Round" StrokeDashArray="0 3"><Line.RenderTransform><RotateTransform Angle="120" CenterX="200" CenterY="200" /></Line.RenderTransform></Line><Line X1="0" Y1="200" X2="200" Y2="200" Stroke="#FF000000" StrokeThickness="10" StrokeStartLineCap="Round" StrokeEndLineCap="Round" StrokeDashCap="Round" StrokeDashArray="0 3"><Line.RenderTransform><RotateTransform Angle="240" CenterX="200" CenterY="200" /></Line.RenderTransform></Line></Canvas>
點擊Formatted Xaml的按鈕後,得到的代碼為:
<?xml version="1.0" encoding="utf-16"?>
<Canvas
Name="canvasContent"
Width="630"
Height="400" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Line
X1="0"
Y1="200"
X2="200"
Y2="200"
Stroke="#FF000000"
StrokeThickness="10"
StrokeStartLineCap="Round"
StrokeEndLineCap="Round"
StrokeDashCap="Round"
StrokeDashArray="0 3">
<Line.RenderTransform>
<RotateTransform
Angle="0"
CenterX="200"
CenterY="200" />
</Line.RenderTransform>
</Line>
<Line
X1="0"
Y1="200"
X2="200"
Y2="200"
Stroke="#FF000000"
StrokeThickness="10"
StrokeStartLineCap="Round"
StrokeEndLineCap="Round"
StrokeDashCap="Round"
StrokeDashArray="0 3">
<Line.RenderTransform>
<RotateTransform
Angle="120"
CenterX="200"
CenterY="200" />
</Line.RenderTransform>
</Line>
<Line
X1="0"
Y1="200"
X2="200"
Y2="200"
Stroke="#FF000000"
StrokeThickness="10"
StrokeStartLineCap="Round"
StrokeEndLineCap="Round"
StrokeDashCap="Round"
StrokeDashArray="0 3">
<Line.RenderTransform>
<RotateTransform
Angle="240"
CenterX="200"
CenterY="200" />
</Line.RenderTransform>
</Line>
</Canvas>
很明顯,後者可讀性強得多。