原文: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. bootstrap如何把表单select input button弄在一行

    bootstrap很多折叠样式css都已经写好,可以直接用,很方便.但是,如果遇到一些bootstrap文档里面没有的例子,估计很多初学者都懵了,然后会折腾很久也未见得有效.今天主要讲如何把selec ...

  2. Centos6.5 网络配置

    网络配置 本教程配置说明 以下为本教程安装时的配置,用户依据实际情况进行调整 * 在root用户权限下安装下完毕 * IP地址设置为 10.10.108.160 * 本机DNS设置为 8.8.8.8 ...

  3. [React] Break up components into smaller pieces using Functional Components

    We are going to ensure our app is structured in a clear way using functional components. Then, we ar ...

  4. 【u026】房间最短路问题

    描述 在一个长宽均为10,入口出口分别为(0,5).(10,5)的房间里,有几堵墙,每堵墙上有两个缺口,求入口到出口的最短路经. 格式 输入格式 第一排为n(n<=20),墙的数目. 接下来n排 ...

  5. iOS开发系列之三 - UITextField 使用方法小结

    // 初始化输入框并设置位置和大小 UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 100, 30 ...

  6. 高速掌握Lua 5.3 —— 字符串库 (2)

    Q:模式匹配字符串的相关函数? A: --[[ string.find(s, pattern [, init [, plain]]) 在字符串"s"中查找第一个与匹配模式" ...

  7. php正则及常用正则函数怎么用

    php正则及常用正则函数怎么用 一.总结 一句话总结: 能够使用正则的函数:preg_match();preg_match_all();preg_replace();preg_grep();preg_ ...

  8. JAVA SortedMap接口

    SortedMap接口主要提供有序的Map实现. Map的主要实现有HashMap,TreeMap,HashTable,LinkedHashMap. TreeMap实现了SortedMap接口,保证了 ...

  9. windows 7 64位下配置mysql64位免安装版

    1.官方网站下载mysql-noinstall-5.1.51-winx64.zip 2.解压到E:\Program Files\MySQL.(路径自己指定) 3.在E:\Program Files\M ...

  10. PCI GXL学习之二次开发篇

    作者:朱金灿 来源:http://blog.csdn.net/clever101 gxl的二次开发分初级和高级之分.初级是gxl提供了几百个模块供你编排成不同的作业.高级就是你可以编写你的算法模块,然 ...