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博客 ...
随机推荐
- 基于 MySQL 5.6 keepalived的双主搭建
环境介绍: 说明 IP 节点1 192.168.56.56 节点2 192.168.56.57 w_ip 192.168.56.6 安装keepalived tar -zxvf keepalived- ...
- scss 常用语法
点击查看 sass 官方文档 1.编译 初学可以在atom 中编译 安装命令 gem install sass atom中安装atom-sass ,mac 中"control+option+ ...
- [Redux] Avoid action type naming conflicts
In redux, the action type is just a normal string type, it is easy to get naming conflicts in large ...
- [NPM] Add comments to your npm scripts
The need for comments in your package.json file becomes desirable the more and more npm scripts you ...
- 关闭 You need to use a Theme.AppCompat theme (or descendant) with this activity解决方法
当我的MainActivity继承自v7包中的ActionBarActivity或者AppCompatActivity时,如果在style.xml文件中指定MainActivity所使用的样式如下: ...
- php如何实现万年历的开发(每日一课真是非常有效率)
php如何实现万年历的开发(每日一课真是非常有效率) 一.总结 一句话总结: 1.判断每月有多少天: 通过data函数来判断,$days=date('t',$firstday); 2.判断每月的第一天 ...
- html常用样式margin、border怎么使用
html常用样式margin.border怎么使用 一.总结 一句话总结:1.margin:auto配合width才能居中:2.border的三个属性依次是边框宽度,边框样式,边框颜色 1.html中 ...
- Oracle 11g对依赖的推断达到字段级
在Oracle 10g下,推断依赖性仅仅达到了对象级.也就是说存储过程訪问的对象一旦发生了变化.那么Oracle就会将存储过程置为INVALID状态.所以在为表做了DDL操作后.须要把存储过程又一次进 ...
- java使用mongoDB和freemarker注意事项
mongoDB和关系数据库的差异最大的优势就是表结构不正确,说:.弱事务.mongoDB以牺牲换来的交易进行更快的访问速度,有人说mongoDB对于内存太贪婪,事实上,它不会.随着windows版本, ...
- Winform 中tabcontrol 美化
需要对tabcontrol按照美工出的图进行美化 对tabpage页进行标题设置,首先对整个tabcontrol的DrawMode设置为OwnerDrawFixed,由于需要对标题宽度有要求,设置si ...