WPF中任意Object的XAML代码格式化输出
有时候,我们需要将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>
很明显,后者可读性强得多。
WPF中任意Object的XAML代码格式化输出的更多相关文章
- WPF中,怎样将XAML代码加载为相应的对象?
原文:WPF中,怎样将XAML代码加载为相应的对象? 在前面"在WPF中,如何得到任何Object对象的XAML代码?"一文中,我介绍了使用System.Windows.Marku ...
- WPF中动态加载XAML中的控件
原文:WPF中动态加载XAML中的控件 using System; using System.Collections.Generic; using System.Linq; using System. ...
- WPF中CAD control的XAML实现
原文:WPF中CAD control的XAML实现 下面这个XAML文件是cad control里面最重要的一部分,使用Grid包含Viewport,Viewport中包括Camera和mod ...
- 【WPF】CAD工程图纸转WPF可直接使用的xaml代码技巧
前言:随着工业化的进一步发展,制造业.工业自动化等多领域,都可能用到上位监控系统.而WPF在上位监控系统方面,应该算是当下最流行的前端框架之一了.而随着监控体系的不断完善与更新迭代,监控画面会变得越来 ...
- python中的循环和编码,运算符, 格式化输出
1.while循环 现在让我们来看看python中的while循环 格式为 while 条件 循环体 (break) (continue) 中断循环的关键字有break和continue, brea ...
- 亲测可用!!!golang如何在idea中保存时自动进行代码格式化
亲测可用,golang在idea中的代码自动格式化 1.ctrl+alt+s打开设置界面,选择[Plugins] -> [Install JetBrains plugin...] -> 搜 ...
- WPF中关于对前台Xaml中Triggers的一些重要思考。
今天在做一个小Demo的时候碰到了一个比较奇怪的问题,就是其中一个Trigger始终无法执行,<Trigger Property="Popup.IsOpen" Value=& ...
- XAML代码格式化神器扩展:XAML Styler,从安装到放弃
背景 平时,我们写XAML的时候,写着写着就多了,乱了,听说这个神器扩展可以一键格式化. XAML Styler -- VS格式化扩展 XAML Styler From MarketPlace 安装 ...
- 将一行很长的js代码格式化输出方便查看
之前的一行js代码,有2万多字符,打开这个网址,粘贴到左边空白框,点下面格式化: 参考下面文章: 数千行的js代码变成了一行,如何复原,该换行的换行,该对齐的对齐_开发工具_小邯韩的博客-CSDN博客 ...
随机推荐
- jQuery 淡入淡出
演示 jQuery fadeIn() 方法: <!DOCTYPE html> <html> <head> <meta charset="utf-8& ...
- System.Xml.XmlException: 引用了未声明的实体“nbsp”
在XML文件中<, >,&等有特殊含义,(前两个字符用于链接签,&用于转义),不能直接使用.使用这些个字符时,应使用它们的转义序列,下面是5个在XML文件中预定义好的实体: ...
- 关于 rman duplicate from active database 搭建dataguard--系列一
关于 rman duplicate from active database.详细操作实际为backup as copy .会拷贝非常多空块.对于那些数据库数据文件超过100G的都不是非常建议用:在非 ...
- iptables 重启系统生效
1. 重启系统生效 开启: chkconfig iptables on 关闭: chkconfig iptables off 2. 即时生效,重启后失效 开启: service iptables ...
- [Angular] Difference between ViewChild and ContentChild
*The children element which are located inside of its template of a component are called *view child ...
- 每天一个JavaScript实例-检測表单数据
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- 【44.19%】【codeforces 608D】Zuma
time limit per test2 seconds memory limit per test512 megabytes inputstandard input outputstandard o ...
- MapReduce 经典案例手机流量排序的分析
在进行流量排序之前,先要明白排序是发生在map阶段,排序之后(排序结束后map阶段才会显示100%完成)才会到reduce阶段(事实上reduce也会排序),.此外排序之前要已经完成了手机流量的统计工 ...
- SQLite的查询优化
SQLite是个典型的嵌入式DBMS,它有很多优点,它是轻量级的,在编译之后很小,其中一个原因就是在查询优化方面比较简单,它只是运用索引机制来进行优化的,经过对SQLite的查询优化的分析以及对源代码 ...
- 【16.05%】【codeforces 664B】Rebus
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...