首页 > 其他分享 >WPF中任意Object的XAML代码格式化输出

WPF中任意Object的XAML代码格式化输出

时间:2023-02-28 11:06:20浏览次数:39  
标签:200 Windows lineRotate XAML Object System using WPF Round


有时候,我们需要将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;
}
}
}


运行效果图:

WPF中任意Object的XAML代码格式化输出_string



点击显示文字为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 versinotallow="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>

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

标签:200,Windows,lineRotate,XAML,Object,System,using,WPF,Round
From: https://blog.51cto.com/JohnsonJu/6090429

相关文章

  • WPF中制作立体效果的文字或LOGO图形
    较久之前,我曾写过一篇:“​​WPF绘制党徽(立体效果,Cool)​​ ”的博文。有感兴趣的朋友来EMAIL问是怎么制作的?本文解决此类问题。有时,为了美观的需要,我们可能需要在应用程......
  • WPF中制作立体效果的文字或LOGO图形(续)
    上篇“WPF中制作立体效果的文字或LOGO图形”中讲述了立体LOGO图形的制作方法。从程序开发人员的角度来讲,这样的做法是不能令人满意的。首先,费时费力效率不高;其次,完全没有通......
  • 利用XSLT及C#.net将SVG转换为XAML
    在网上找到了一个名为SVG-Convert-Driver-XAML-0.02的开源代码,解压缩之后发现,它并不是使用.net代码写的,所以并不适合我的需要。不过,里面有一个文件却是非常有用的,那就是......
  • WPF/Silverlight中图形的平移,缩放,旋转,倾斜变换演示
    为方便描述,这里仅以正方形来做演示,其他图形从略。运行时效果图:XAML代码://Transform.XAML<CanvasWidth="700"Height="700"xmlns="​​http://schemas.microsoft.com/......
  • WPF,SilverLight中直线的样式示例
    XAML代码://LineStyle.xaml<ViewboxWidth="600"Height="500"xmlns="​​​http://schemas.microsoft.com/winfx/2006/xaml/presentation​​​"xmlns:x="​​​http:......
  • WPF公章制作之2
    早前,我曾写过一篇:“在WPF中制作正圆形公章”。有空再次研究,使用C#将此WPF程序写了出来。运行效果图:关键C#代码://OfficialSeal.csusingSystem;usingSystem.Windows;usin......
  • WPF中的Frozen(冻结)与线程及其他相关问题
    System.Windows.Freezable类(在WindowsBase.dll中)定义一个对象,该对象具有可修改状态和只读(冻结)状态。派生自Freezable的类提供详细的更改通知,可以是不可变的,并且可以进行......
  • 关于.Net中的计时器及WPF中最适合的计时器问题
    .Net中,至少可以找出5个计时器类型:(1)System.Threading.Timer(2)System.Timers.Timer(3)System.Windows.Forms.Timer(4)System.Web.UI.Timer(5)System.Windows.Threading.Dis......
  • WPF 精修篇 拖拽 DragDrop
    WPF实现拖拽效果<Grid><Grid.ColumnDefinitions><ColumnDefinitionWidth="197*"/><ColumnDefinitionWidth="209*"/><Colum......
  • WPF 精修篇 依赖属性
    依赖属性使用场景1.希望可在样式中设置属性。2.希望属性支持数据绑定。3.希望可使用动态资源引用设置属性。4.希望从元素树中的父元素自动继承属性值。5.希望属性可进......