原文: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代码格式化输出的更多相关文章

  1. WPF中,怎样将XAML代码加载为相应的对象?

    原文:WPF中,怎样将XAML代码加载为相应的对象? 在前面"在WPF中,如何得到任何Object对象的XAML代码?"一文中,我介绍了使用System.Windows.Marku ...

  2. WPF中动态加载XAML中的控件

    原文:WPF中动态加载XAML中的控件 using System; using System.Collections.Generic; using System.Linq; using System. ...

  3. WPF中CAD control的XAML实现

    原文:WPF中CAD control的XAML实现     下面这个XAML文件是cad control里面最重要的一部分,使用Grid包含Viewport,Viewport中包括Camera和mod ...

  4. 【WPF】CAD工程图纸转WPF可直接使用的xaml代码技巧

    前言:随着工业化的进一步发展,制造业.工业自动化等多领域,都可能用到上位监控系统.而WPF在上位监控系统方面,应该算是当下最流行的前端框架之一了.而随着监控体系的不断完善与更新迭代,监控画面会变得越来 ...

  5. python中的循环和编码,运算符, 格式化输出

    1.while循环 现在让我们来看看python中的while循环  格式为 while 条件 循环体 (break) (continue) 中断循环的关键字有break和continue, brea ...

  6. 亲测可用!!!golang如何在idea中保存时自动进行代码格式化

    亲测可用,golang在idea中的代码自动格式化 1.ctrl+alt+s打开设置界面,选择[Plugins] -> [Install JetBrains plugin...] -> 搜 ...

  7. WPF中关于对前台Xaml中Triggers的一些重要思考。

    今天在做一个小Demo的时候碰到了一个比较奇怪的问题,就是其中一个Trigger始终无法执行,<Trigger Property="Popup.IsOpen" Value=& ...

  8. XAML代码格式化神器扩展:XAML Styler,从安装到放弃

    背景 平时,我们写XAML的时候,写着写着就多了,乱了,听说这个神器扩展可以一键格式化. XAML Styler -- VS格式化扩展 XAML Styler From MarketPlace 安装 ...

  9. 将一行很长的js代码格式化输出方便查看

    之前的一行js代码,有2万多字符,打开这个网址,粘贴到左边空白框,点下面格式化: 参考下面文章: 数千行的js代码变成了一行,如何复原,该换行的换行,该对齐的对齐_开发工具_小邯韩的博客-CSDN博客 ...

随机推荐

  1. [Java开发之路](15)注解

    1. 简单介绍 注解(也被称为元数据),为我们在代码中加入信息提供了一种形式化的方法. 注解在一定程度上是把元数据与源码文件结合在一起,而不是保存在外部文档中这一大趋势之下所催生的. 它能够提供用来完 ...

  2. [Javascript] Create scrollable DOM elements with Greensock

    In this lesson, we will look at Greensock's Draggable API. We will implement a scrollable <div> ...

  3. Django之分页显示文章

    1.项目:http://www.cnblogs.com/jasonhaven/p/7493422.html 2.任务描述:页面分页显示文章 3.源代码 后台: from django.core.pag ...

  4. 文件流结尾的EOF详解

    我学习C语言的时候,遇到的一个问题就是EOF. 它是end of file的缩写,表示"文字流"(stream)的结尾.这里的"文字流",可以是文件(file) ...

  5. Linux安装.Net core 环境并运行项目

    原文:Linux安装.Net core 环境并运行项目 一 安装环境 1.  从微软官网下载 Linux版本的.NetCoreSdk 2.0 安装包 打开终端: 第一步: sudo yum insta ...

  6. SQL中where语句不能使用直接跟在select后列的别名

    由于select语句的执行顺序为: 1. from语句 2. where语句(结合条件) 3. start with语句 4. connect by语句 5. where语句 6. group by语 ...

  7. 二叉苹果树 - 二叉树树型DP

    传送门 中文题面: 题目描述 有一棵苹果树,如果树枝有分叉,一定是分 2 叉(就是说没有只有 1 个儿子的结点,这棵树共有N 个结点(叶子点或者树枝分叉点),编号为1-N,树根编号一定是1. 我们用一 ...

  8. Oracle数据库零散知识02

    15,函数的创建,要求必须有返回值,必须在语句中调用,需要多个返回值时,使用out参数类型,在user_procedures表中查询属性,在user_source表中查询源代码,创建示例: CREAT ...

  9. Entity Framework知识小总结

    什么是Entity Framework EF是微软主推的数据存取技术,在实际开发中,现在通常使用EF来构建应用程序的数据存取层,它是一个开源的“对象/关系映射(ORM:Object Relationa ...

  10. 各种图示的介绍及绘制(boxplot、stem)

    1. 箱线图(boxplot) 也叫作箱形图: 一种用作显示一组数据分散情况资料的统计图.因形状如箱子而得名.在各种领域也经常被使用,常见于品质管理. 主要包含六个数据节点,将一组数据从大到小排列,分 ...