博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WPF中任意Object的XAML代码格式化输出
阅读量:5940 次
发布时间:2019-06-19

本文共 4488 字,大约阅读时间需要 14 分钟。

原文:

有时候,我们需要将WPF中的控件自身的XAML代码输出成文本,那么,我们可以使用System.Windows.Markup.XamlWriter.Save()方法来完成此任务。关于XamlWriter.Save()的示例,我曾经在“在WPF中,如何得到任何Object对象的XAML代码?”()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;
        }
    }
}
运行效果图:
输出格式化的XAML代码
点击显示文字为XamlCode的按钮后,输出的代码为:
<Canvas Name="canvasContent" Width="630" Height="400" xmlns=" 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="">
    <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>

很明显,后者可读性强得多。

你可能感兴趣的文章
我的友情链接
查看>>
我的友情链接
查看>>
Spring学习资料之 依赖注入(一)
查看>>
安装win7提示安装程序无法创建新的系统分区和定位现有系统分区
查看>>
快递查询接口的调用与解析案例
查看>>
我的友情链接
查看>>
服务器性能优化配置建议
查看>>
oracle sql语句实现累加、累减、累乘、累除
查看>>
SCNetworkReachabilityRef监测网络状态
查看>>
3D地图的定时高亮和点击事件(基于echarts)
查看>>
接口由40秒到200ms优化记录
查看>>
java 视频播放 多人及时弹幕技术 代码生成器 websocket springmvc mybatis SSM
查看>>
Activiti6.0,spring5,SSM,工作流引擎,OA
查看>>
第十三章:SpringCloud Config Client的配置
查看>>
使用 GPUImage 实现一个简单相机
查看>>
CoinWhiteBook:区块链在慈善事业中的应用
查看>>
Mac上基于Github搭建Hexo博客
查看>>
阿里云服务器ECS开放8080端口
查看>>
前端常用排序详解
查看>>
Spring中实现监听的方法
查看>>