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博客 ...
随机推荐
- 走进windows编程的世界-----对话框、文本框、button
1 对话框的分类 2 对话框的基本使用方式 3 对话框资源 4 有模式对话框的使用 int DialogBox( HINSTANCE hInstance, LPCTSTR lpTemplate, ...
- 百度echart--Uncaught Error: Component series.wordCloud not exists. Load it first.
百度echart--Uncaught Error: Component series.wordCloud not exists. Load it first. 一.总结 一句话总结:关注报的错.可以通 ...
- innodb_data_file_path参数误修改解决方法
如果innodb_data_file_path参数被修改乱了,不知道原来值的大小,这样启动是会报错的.知道原来大小方法: 方法一:操作系统下ls -l看到原来大小,直接用ls -l显示的大小,复制即可 ...
- Scrapy系列教程(1)------命令行工具
默认的Scrapy项目结构 在開始对命令行工具以及子命令的探索前,让我们首先了解一下Scrapy的项目的文件夹结构. 尽管能够被改动,但全部的Scrapy项目默认有类似于下边的文件结构: scrapy ...
- 学习鸟哥的Linux私房菜笔记(14)——硬件配置与管理
一.设备文件 Linux沿袭了Unix的风格,将所有设备看成一个文件 设备文件分为两种: 块设备文件(b):比如硬盘.光驱 字符设备文件(c):比如串口.键盘 设备文件一般存放在/dev目录下 二.常 ...
- windows 7、Windows10 系统目录迁移,修改安装的默认路径:Users,Program Files,ProgramData
Tips:本文只针对Win7.windows 10系统有过测试,其他系统尚未测试:不过大家想尝试也可.这次实验是我做的全新 win7_32位系统来测试的,windows10当然64位的系统也可.至少现 ...
- Spring MVC的RequestContextHolder使用误区 good
JShop简介:jshop是一套使用Java语言开发的B2C网店系统,致力于为个人和中小企业提供免费.好用的网店系统. 项目主页:http://git.oschina.net/dinguangx/js ...
- 华为软件开发云(DevCloud):免费可商用的项目管理工具
在软件开发技术和理念层出不穷的今天,如何更快的适应变化的环境,更好的满足客户的需求,已经成为决定从小到大各种规模企业能否活下去的关键. 天下武功唯快不破,在当今大环境中更是如此,微服务,敏捷开发,新的 ...
- 华为如何实现基于Git的跨地域协同开发
跨地域开发的需求其实由来已久,在IT/互联网发展的早期就已存在,只不过限于当时网络环境的因素,无法在线上有效的完成协同工作,所以没法实际开展.而随着近十年网络的快速发展,跨地域协同开发线变得可能而且越 ...
- telnet 的使用(ping 与 telnet)
基本用法 >> telnet localhost 23 // 23 表示 telnet 服务的端口号,不写端口号也可以,telnet 默认绑定的端口号就是 23 // netstat -a ...