对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:\ ...
随机推荐
- linux中的rootfs/initrd/ramfs/initramfs
什么是ramfs?ramfs是空间规模动态变化的RAM文件系统.它非常简单,是用来实现Linux缓存机制(缓存page cache and dentry cache)的文件系统.通常情况下,Linux ...
- go get请求 json字符串转为结构体
package main import ( "io/ioutil" "fmt" "net/http" "encoding/json ...
- 学习jquery.pagewalkthroung.js插件记录点
1.53行:options = $.extend(true, {}, $.fn.pagewalkthrough.defaults, options); $.extend的作用是把第二个对象合并到第一个 ...
- angular模块详解
原文: https://www.jianshu.com/p/819421ff955a 大纲 1.angular应用是模块化的 2.对模块(Module)的认识 3.模块的分类:根模块和特性模块 4.N ...
- 黑马程序猿-assign、retain、release、nonatomic、atomic、strong、weak
都是用于修饰@property声明的变量 assign:用于非oc对象类型,表示直接赋值(默认值) retain:用于mrc中,用于类属性中有oc对象的情况,表示先推断赋值的对象是否和实例对象变量的值 ...
- mysql查询字段所在表
use information_schema;select * from columns where column_name='字段名' ;
- C#中使用split分割字符串的几种方法小结
1.用字符串分隔: using System.Text.RegularExpressions;string str="aaajsbbbjsccc";string[] sArray= ...
- Django之模板过滤器
Django 模板过滤器也是我们在以后基于 Django 网站开发过程中会经常遇到的,如显示格式的转换.判断处理等.以下是 Django 过滤器列表,希望对为大家的开发带来一些方便. 一.形式:小写 ...
- ArcEngine中最短路径的实现
原文 ArcEngine中最短路径的实现 最短路径分析属于ArcGIS的网络分析范畴.而ArcGIS的网络分析分为两类,分别是基于几何网络和网络数据集的网络分析.它们都可以实现最短路径功能.下面先介绍 ...
- 从Handler+Message+Looper源代码带你分析Android系统的消息处理机制
PS一句:不得不说CSDN同步做的非常烂.还得我花了近1个小时恢复这篇博客. 引言 [转载请注明出处:http://blog.csdn.net/feiduclear_up CSDN 废墟的树] 作为A ...