对XAML进行编辑的辅助类(XamlHelper)
// XamlHelper.cs
// --------------------------------------------
// 对XAML进行编辑操作的辅助类:
// 对选中的XAML进行操作; 对XAML代码进行对齐整理; 对XAML标记进行着色显示等
// --------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Collections;
using System.Windows;
using System.IO;
using System.Xml;
using System.Windows.Markup;
namespace BrawDraw.Com.Xaml.Utility
{
/// <summary>
/// Provides help functions for processing XAML.
/// </summary>
public class XamlHelper
{
/// <summary>
/// Get XAML from TextRange.Xml property
/// </summary>
/// <param name="range">TextRange</param>
/// <returns>return a string serialized from the TextRange</returns>
public static string GetTextRangeXaml(TextRange range)
{
MemoryStream mstream;
if (range == null)
{
throw new ArgumentNullException("range");
}
mstream = new MemoryStream();
range.Save(mstream, DataFormats.Xaml);
//must move the stream pointer to the beginning since range.save() will move it to the end.
mstream.Seek(0, SeekOrigin.Begin);
//Create a stream reader to read the xaml.
StreamReader stringReader = new StreamReader(mstream);
return stringReader.ReadToEnd();
}
/// <summary>
/// Set XML to TextRange.Xml property.
/// </summary>
/// <param name="range">TextRange</param>
/// <param name="xaml">XAML to be set</param>
public static void SetTextRangeXaml(TextRange range, string xaml)
{
MemoryStream mstream;
if (null == xaml)
{
throw new ArgumentNullException("xaml");
}
if (range == null)
{
throw new ArgumentNullException("range");
}
mstream = new MemoryStream();
StreamWriter sWriter = new StreamWriter(mstream);
mstream.Seek(0, SeekOrigin.Begin); //this line may not be needed.
sWriter.Write(xaml);
sWriter.Flush();
//move the stream pointer to the beginning.
mstream.Seek(0, SeekOrigin.Begin);
range.Load(mstream, DataFormats.Xaml);
}
/// <summary>
/// Parse a string to WPF object.
/// </summary>
/// <param name="str">string to be parsed</param>
/// <returns>return an object</returns>
public static object ParseXaml(string str)
{
MemoryStream ms = new MemoryStream(str.Length);
StreamWriter sw = new StreamWriter(ms);
sw.Write(str);
sw.Flush();
ms.Seek(0, SeekOrigin.Begin);
ParserContext pc = new ParserContext();
pc.BaseUri = new Uri(System.Environment.CurrentDirectory + "/");
return XamlReader.Load(ms, pc);
}
public static string IndentXaml(string xaml)
{
//open the string as an XML node
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xaml);
XmlNodeReader nodeReader = new XmlNodeReader(xmlDoc);
//write it back onto a stringWriter
System.IO.StringWriter stringWriter = new System.IO.StringWriter();
System.Xml.XmlTextWriter xmlWriter = new System.Xml.XmlTextWriter(stringWriter);
xmlWriter.Formatting = System.Xml.Formatting.Indented;
xmlWriter.Indentation = 4;
xmlWriter.IndentChar = ' ';
xmlWriter.WriteNode(nodeReader, false);
string result = stringWriter.ToString();
xmlWriter.Close();
return result;
}
public static string RemoveIndentation(string xaml)
{
if (xaml.Contains("/r/n "))
{
return RemoveIndentation(xaml.Replace("/r/n ", "/r/n"));
}
else
{
return xaml.Replace("/r/n", "");
}
}
public static string ColoringXaml(string xaml)
{
string[] strs;
string value = "";
string s1, s2;
s1 = "<Section xml:space=/"preserve/" xmlns=/"http://schemas.microsoft.com/winfx/2006/xaml/presentation/"><Paragraph>";
s2 = "</Paragraph></Section>";
strs = xaml.Split(new char[] { '<' });
for (int i = 1; i < strs.Length; i++)
{
value += ProcessEachTag(strs[i]);
}
return s1 + value + s2;
}
static string ProcessEachTag(string str)
{
string front = "<Run Foreground=/"Blue/"><</Run>";
string end = "<Run Foreground=/"Blue/">></Run>";
string frontWithSlash = "<Run Foreground=/"Blue/"></</Run>";
string endWithSlash = "<Run Foreground=/"Blue/"> /></Run>";//a space is added.
string tagNameStart = "<Run FontWeight=/"Bold/">";
string propertynameStart = "<Run Foreground=/"Red/">";
string propertyValueStart = "/"<Run Foreground=/"Blue/">";
string endRun = "</Run>";
string returnValue;
string[] strs;
int i = 0;
if (str.StartsWith("/"))
{ //if the tag is an end tag, remove the "/"
returnValue = frontWithSlash;
str = str.Substring(1).TrimStart();
}
else
{
returnValue = front;
}
strs = str.Split(new char[] { '>' });
str = strs[0];
i = (str.EndsWith("/")) ? 1 : 0;
str = str.Substring(0, str.Length - i).Trim();
if (str.Contains("="))//the tag has a property
{
//set tagName
returnValue += tagNameStart + str.Substring(0, str.IndexOf(" ")) + endRun + " ";
str = str.Substring(str.IndexOf(" ")).Trim();
}
else //no property
{
returnValue += tagNameStart + str.Trim() + endRun + " ";
//nothing left to parse
str = "";
}
//Take care of properties:
while (str.Length > 0)
{
returnValue += propertynameStart + str.Substring(0, str.IndexOf("=")) + endRun + "=";
str = str.Substring(str.IndexOf("/"") + 1).Trim();
returnValue += propertyValueStart + str.Substring(0, str.IndexOf("/"")) + endRun + "/" ";
str = str.Substring(str.IndexOf("/"") + 1).Trim();
}
if (returnValue.EndsWith(" "))
{
returnValue = returnValue.Substring(0, returnValue.Length - 1);
}
returnValue += (i == 1) ? endWithSlash : end;
//Add the content after the ">"
returnValue += strs[1];
return returnValue;
}
}
}
对XAML进行编辑的辅助类(XamlHelper)的更多相关文章
- wpf xaml文件编辑出现中文乱码
突然有一天,发现在xaml文件编辑窗里打汉字出来了乱码...抓狂 结果发现是番茄助手搞得鬼.只能在编辑xaml文件是暂时关闭番茄助手 visual assist
- WPF入门教程系列十九——ListView示例(一)
经过前面的学习,今天我做一个比较综合的WPF程序示例,主要包括以下功能: 1) 查询功能.从数据库(本地数据库(local)/Test中的S_City表中读取城市信息数据,然后展示到WPF的Windo ...
- WPF 杂谈——入门介绍
对于WPF的技术笔者是又爱又恨.现在WPF的市场并不是很锦气.如果以WPF来吃饭的话,只怕会饿死在街头.同时现在向面WEB开发更是如火冲天.所以如果是新生的话,最好不要以WPF为主.做为选择性来学习一 ...
- Blend 2015 教程 (五) 自定义状态
本篇再补充一块内容,就是自定义状态的介绍. 自定义状态用于封装用户控件在各种状态之间切换时的外观变化及其动画效果,方便调用.比如有个用户控件用于实现类似舞台幕布打开和关闭切换的效果,可以创建幕布关闭和 ...
- UWP 播放媒体控件
最近我的uwp需要有一个有声朗读的功能,like this 点击声音按钮就可以有声朗读了.这里主要是用了媒体播放的控件. 一般我们把需求分为两种: 一种是不需要呈现播放器的样子,只需要用户点击一下别的 ...
- WPF实用指南一:在WPF窗体的边框中添加搜索框和按钮
原文:WPF实用指南一:在WPF窗体的边框中添加搜索框和按钮 在边框中加入一些元素,在应用程序的界面设计中,已经开始流行起来.特别是在浏览器(Crome,IE,Firefox,Opera)中都有应用. ...
- [MAUI] 在.NET MAUI中结合Vue实现混合开发
在MAUI微软的官方方案是使用Blazor开发,但是当前市场大多数的Web项目使用Vue,React等技术构建,如果我们没法绕过已经积累的技术,用Blazor重写整个项目并不现实. Vue是当前流 ...
- VS编程,编辑WPF过程中,点击设计器中界面某一控件,在XAML中高亮突出显示相应的控件代码的设置方法。
原文:VS编程,编辑WPF过程中,点击设计器中界面某一控件,在XAML中高亮突出显示相应的控件代码的设置方法. 版权声明:我不生产代码,我只是代码的搬运工. https://blog.csdn.net ...
- vs.net2017在编辑的wpf的xaml文件引用本程序集下的类提示“找不到”
local对应就是当前exe程序下的类,会提示“...命令空间...找不到...” 因为我调整过生成的,于是尝试调回来anyCPU 问题解决. 看了一下vs.net2017的所在目录"C:\ ...
随机推荐
- OpenGL_ES-纹理
OpenGL_ES2.0 -纹理 一:纹理基础: 1: 纹素的概念: 一个二维纹理在OpenGLES2.0中是非经常见的,二维纹理就是一个二维数组,每一个数据元素称为纹素,详细格式例如以下: GL_R ...
- 飘逸的python - property及实现lazy property
@property有什么用呢?表面看来,就是将一个方法用属性的方式来訪问. 上代码,代码最清晰了. class Circle(object): def __init__(self, radius): ...
- Setup iOS Development Environment.
Setup iOS Development Environment Install XCode and check-out source code from SVN XCode Please find ...
- Gora官方范例 分类: C_OHTERS 2015-01-29 16:14 632人阅读 评论(0) 收藏
参考官方文档:http://gora.apache.org/current/tutorial.html 项目代码见:https://code.csdn.net/jediael_lu/mygoradem ...
- Opencv分水岭算法——watershed自动图像分割用法
分水岭算法是一种图像区域分割法,在分割的过程中,它会把跟临近像素间的相似性作为重要的参考依据,从而将在空间位置上相近并且灰度值相近的像素点互相连接起来构成一个封闭的轮廓,封闭性是分水岭算法的一个重要特 ...
- ConcurrentLinkedQueue的实现原理分析
1. 引言 在并发编程中我们有时候需要使用线程安全的队列.如果我们要实现一个线程安全的队列有两种实现方式一种是使用阻塞算法,另一种是使用非阻塞算法.使用阻塞算法的队列可以用一个锁(入队和出队用同 ...
- VMware虚拟机12安装linux系统详细教程
亲测有效,附图: 工具/原料 VM ware workstation12虚拟机(百度下载) 深度linux镜像ios系统文件 链接:https://pan.baidu.com/s/1RY1Plgru4 ...
- [Java开发之路](15)注解
1. 简单介绍 注解(也被称为元数据),为我们在代码中加入信息提供了一种形式化的方法. 注解在一定程度上是把元数据与源码文件结合在一起,而不是保存在外部文档中这一大趋势之下所催生的. 它能够提供用来完 ...
- POJ1659Frogs' Neighborhood(lavel定理)
Frogs' Neighborhood Time Limit: 5000MS Memory Limit: 10000K Total Submissions: 7260 Accepted: 31 ...
- CSDN编程挑战——《交替字符串》
交替字符串 题目详情: 假设字符串str3可以由str1和str2中的字符按顺序交替形成,那么称str3为str1和str2的交替字符串.比如str1="abc",str2=&qu ...