AvalonEdit验证语法并提示错误
<UserControl x:Class="WpfTestApp.Xml.XmlEditor"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:avalonedit="http://icsharpcode.net/sharpdevelop/avalonedit"
xmlns:WpfTestApp="clr-namespace:WpfTestApp.Xml"> <UserControl.CommandBindings>
<CommandBinding Command="WpfTestApp:XmlEditor.ValidateCommand" Executed="Validate"/>
</UserControl.CommandBindings> <avalonedit:TextEditor Name="textEditor" FontFamily="Consolas" SyntaxHighlighting="XML" FontSize="8pt">
<avalonedit:TextEditor.Options>
<avalonedit:TextEditorOptions ShowSpaces="True" ShowTabs="True"/>
</avalonedit:TextEditor.Options>
<avalonedit:TextEditor.ContextMenu>
<ContextMenu>
<MenuItem Command="Undo" />
<MenuItem Command="Redo" />
<Separator/>
<MenuItem Command="Cut" />
<MenuItem Command="Copy" />
<MenuItem Command="Paste" />
<Separator/>
<MenuItem Command="WpfTestApp:XmlEditor.ValidateCommand" />
</ContextMenu>
</avalonedit:TextEditor.ContextMenu>
</avalonedit:TextEditor>
</UserControl>
public partial class XmlEditor : UserControl
{
private static readonly ICommand validateCommand = new RoutedUICommand("Validate XML", "Validate", typeof(MainWindow),
new InputGestureCollection { new KeyGesture(Key.V, ModifierKeys.Control | ModifierKeys.Shift) }); private readonly TextMarkerService textMarkerService;
private ToolTip toolTip; public static ICommand ValidateCommand
{
get { return validateCommand; }
} public XmlEditor()
{
InitializeComponent(); textMarkerService = new TextMarkerService(textEditor);
TextView textView = textEditor.TextArea.TextView;
textView.BackgroundRenderers.Add(textMarkerService);
textView.LineTransformers.Add(textMarkerService);
textView.Services.AddService(typeof(TextMarkerService), textMarkerService); textView.MouseHover += MouseHover;
textView.MouseHoverStopped += TextEditorMouseHoverStopped;
textView.VisualLinesChanged += VisualLinesChanged;
} private void MouseHover(object sender, MouseEventArgs e)
{
var pos = textEditor.TextArea.TextView.GetPositionFloor(e.GetPosition(textEditor.TextArea.TextView) + textEditor.TextArea.TextView.ScrollOffset);
bool inDocument = pos.HasValue;
if (inDocument)
{
TextLocation logicalPosition = pos.Value.Location;
int offset = textEditor.Document.GetOffset(logicalPosition); var markersAtOffset = textMarkerService.GetMarkersAtOffset(offset);
TextMarkerService.TextMarker markerWithToolTip = markersAtOffset.FirstOrDefault(marker => marker.ToolTip != null); if (markerWithToolTip != null)
{
if (toolTip == null)
{
toolTip = new ToolTip();
toolTip.Closed += ToolTipClosed;
toolTip.PlacementTarget = this;
toolTip.Content = new TextBlock
{
Text = markerWithToolTip.ToolTip,
TextWrapping = TextWrapping.Wrap
};
toolTip.IsOpen = true;
e.Handled = true;
}
}
}
} void ToolTipClosed(object sender, RoutedEventArgs e)
{
toolTip = null;
} void TextEditorMouseHoverStopped(object sender, MouseEventArgs e)
{
if (toolTip != null)
{
toolTip.IsOpen = false;
e.Handled = true;
}
} private void VisualLinesChanged(object sender, EventArgs e)
{
if (toolTip != null)
{
toolTip.IsOpen = false;
}
} private void Validate(object sender, ExecutedRoutedEventArgs e)
{
IServiceProvider sp = textEditor;
var markerService = (TextMarkerService)sp.GetService(typeof(TextMarkerService));
markerService.Clear(); try
{
var document = new XmlDocument { XmlResolver = null };
document.LoadXml(textEditor.Document.Text);
}
catch (XmlException ex)
{
DisplayValidationError(ex.Message, ex.LinePosition, ex.LineNumber);
}
} private void DisplayValidationError(string message, int linePosition, int lineNumber)
{
if (lineNumber >= && lineNumber <= textEditor.Document.LineCount)
{
int offset = textEditor.Document.GetOffset(new TextLocation(lineNumber, linePosition));
int endOffset = TextUtilities.GetNextCaretPosition(textEditor.Document, offset, System.Windows.Documents.LogicalDirection.Forward, CaretPositioningMode.WordBorderOrSymbol);
if (endOffset < )
{
endOffset = textEditor.Document.TextLength;
}
int length = endOffset - offset; if (length < )
{
length = Math.Min(, textEditor.Document.TextLength - offset);
} textMarkerService.Create(offset, length, message);
}
}
}
public class TextMarkerService : IBackgroundRenderer, IVisualLineTransformer
{
private readonly TextEditor textEditor;
private readonly TextSegmentCollection<TextMarker> markers; public sealed class TextMarker : TextSegment
{
public TextMarker(int startOffset, int length)
{
StartOffset = startOffset;
Length = length;
} public Color? BackgroundColor { get; set; }
public Color MarkerColor { get; set; }
public string ToolTip { get; set; }
} public TextMarkerService(TextEditor textEditor)
{
this.textEditor = textEditor;
markers = new TextSegmentCollection<TextMarker>(textEditor.Document);
} public void Draw(TextView textView, DrawingContext drawingContext)
{
if (markers == null || !textView.VisualLinesValid)
{
return;
}
var visualLines = textView.VisualLines;
if (visualLines.Count == )
{
return;
}
int viewStart = visualLines.First().FirstDocumentLine.Offset;
int viewEnd = visualLines.Last().LastDocumentLine.EndOffset;
foreach (TextMarker marker in markers.FindOverlappingSegments(viewStart, viewEnd - viewStart))
{
if (marker.BackgroundColor != null)
{
var geoBuilder = new BackgroundGeometryBuilder {AlignToWholePixels = true, CornerRadius = };
geoBuilder.AddSegment(textView, marker);
Geometry geometry = geoBuilder.CreateGeometry();
if (geometry != null)
{
Color color = marker.BackgroundColor.Value;
var brush = new SolidColorBrush(color);
brush.Freeze();
drawingContext.DrawGeometry(brush, null, geometry);
}
}
foreach (Rect r in BackgroundGeometryBuilder.GetRectsForSegment(textView, marker))
{
Point startPoint = r.BottomLeft;
Point endPoint = r.BottomRight; var usedPen = new Pen(new SolidColorBrush(marker.MarkerColor), );
usedPen.Freeze();
const double offset = 2.5; int count = Math.Max((int) ((endPoint.X - startPoint.X)/offset) + , ); var geometry = new StreamGeometry(); using (StreamGeometryContext ctx = geometry.Open())
{
ctx.BeginFigure(startPoint, false, false);
ctx.PolyLineTo(CreatePoints(startPoint, endPoint, offset, count).ToArray(), true, false);
} geometry.Freeze(); drawingContext.DrawGeometry(Brushes.Transparent, usedPen, geometry);
break;
}
}
} public KnownLayer Layer
{
get { return KnownLayer.Selection; }
} public void Transform(ITextRunConstructionContext context, IList<VisualLineElement> elements)
{} private IEnumerable<Point> CreatePoints(Point start, Point end, double offset, int count)
{
for (int i = ; i < count; i++)
{
yield return new Point(start.X + (i*offset), start.Y - ((i + )% == ? offset : ));
}
} public void Clear()
{
foreach (TextMarker m in markers)
{
Remove(m);
}
} private void Remove(TextMarker marker)
{
if (markers.Remove(marker))
{
Redraw(marker);
}
} private void Redraw(ISegment segment)
{
textEditor.TextArea.TextView.Redraw(segment);
} public void Create(int offset, int length, string message)
{
var m = new TextMarker(offset, length);
markers.Add(m);
m.MarkerColor = Colors.Red;
m.ToolTip = message;
Redraw(m);
} public IEnumerable<TextMarker> GetMarkersAtOffset(int offset)
{
return markers == null ? Enumerable.Empty<TextMarker>() : markers.FindSegmentsContaining(offset);
}
}
AvalonEdit验证语法并提示错误的更多相关文章
- 【Azure API 管理】在APIM中使用客户端证书验证API的请求,但是一直提示错误"No client certificate received."
API 管理 (APIM) 是一种为现有后端服务创建一致且现代化的 API 网关的方法. 问题描述 在设置了APIM客户端证书,用户保护后端API,让请求更安全. 但是,最近发现使用客户端证书的API ...
- 今天遇到一件开心事,在eclipse编写的代码在命令窗口中编译后无法运行,提示 “错误: 找不到或无法加载主类”
java中带package和不带package的编译运行方式是不同的. 首先来了解一下package的概念:简单定义为,package是一个为了方便管理组织java文件的目录结构,并防止不同java文 ...
- jQuery validate运作流程以及重复提示错误问题
一,运作流程 jQuery validate要想运作,首先要加载相应的js <script type="text/javascript" src="/js/clas ...
- asp.net mvc3 数据验证(二)——错误信息的自定义及其本地化
原文:asp.net mvc3 数据验证(二)--错误信息的自定义及其本地化 一.自定义错误信息 在上一篇文章中所做的验证,在界面上提示的信息都是系统自带的,有些读起来比较生硬.比如: ...
- vue中npm run dev运行项目不能自动打开浏览器! 以及 webstorm跑vue项目jshint一直提示错误问题的解决方法!
vue中npm run dev运行项目不能自动打开浏览器!以及 webstorm跑vue项目jshint一直提示错误问题的解决方法! 1.上个项目结束就很久没有使用vue了,最近打算用vue搭建自己的 ...
- Python3安装turtle提示错误:Command "python setup.py egg_info" failed with error code 1
Python3安装turtle提示错误:Command "python setup.py egg_info" failed with error code 1 Python3.5安 ...
- Django-Form表单(验证、定制、错误信息、Select)
Django form 流程 1.创建类,继承form.Form 2.页面根据类的对象自动创建html标签 3.提交,request.POST 封装到类的对象里,obj=UserInf ...
- MYSQL导入CSV格式文件数据执行提示错误(ERROR 1290): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement.
MYSQL导入CSV格式文件数据执行提示错误(ERROR 1290): The MySQL server is running with the --secure-file-priv option s ...
- SVN“验证位置时发生错误”的解决办法
验证位置时发生错误:“org.tigris.subversion.javahl.ClientException...... 验证位置时发生错误:“org.tigris.subversion.javah ...
随机推荐
- (转)光照模型及cg实现
经典光照模型(illumination model) 物体表面光照颜色由入射光.物体材质,以及材质和光的交互规律共同决定. 由于环境光给予物体各个点的光照强度相同,且没有方向之分,所以在只有环境光的情 ...
- (转)透明光照模型与环境贴图之基础理论篇(折射率、色散、fresnel定律) .
摘抄“GPU Programming And Cg Language Primer 1rd Edition” 中文名“GPU编程与CG语言之阳春白雪下里巴人” 材质和光的交互除了反射现象,对于透明物 ...
- 修改Linux SSH连接端口和禁用IP,安装DDoS deflate
测试系统:centos7 修改连接端口 修改配置文件 vi /etc/ssh/sshd_config 去掉port 22的注释,添加新的端口配置 port your_port_num 自定义端口选择建 ...
- SAP CX Upscale Commerce : SAP全新推出的电商云平台
大家好,我是Andy Chen,是SAP成都研究院年轻的SAP CX Upscale Commerce (后面将会以Upscale简称)开发团队的一名产品经理.CX的全称是Customer Exper ...
- 026.2 网络编程 UDP聊天
实现,通过socket对象 ##############################################################需求建立UDP发送端:###思路:1.建立可以实 ...
- IM——技术方案
一. 即时通讯技术方案 1. 第三方SDK: 环信, 融云, 网易云信, 腾讯 中小型公司/初创型: 建议使用第三方. 好处: 快, 符合快速开发的需求, 自己和后台人员不需要做什么操作 缺点: 你的 ...
- UVa 10214 - Trees in a Wood.(欧拉函数)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 「GXOI / GZOI2019」旅行者
题目 我还是太傻了 考虑每一条边的贡献,对于一条有向边\((u,v,w)\),我们求出\(k\)个关键点中到\(u\)最近的距离\(dis_1\),以及\(v\)到\(k\)个关键点中最近的距离\(d ...
- 【bzoj 3622】已经没有什么好害怕的了
题目 看到这个数据范围就发现我们需要一个\(O(n^2)\)的做法了,那大概率是\(dp\)了 看到恰好\(k\)个我们就知道这基本是个容斥了 首先解方程发现我们需要使得\(a>b\)的恰好有\ ...
- [HNOI2003]操作系统
嘟嘟嘟 这道题就是一个模拟. 首先我们建一个优先队列,存所有等待的进程,当然第一关键字是优先级从大到小,第二关键字是到达时间从小到大.然后再建一个指针Tim,代表cpu运行的绝对时间. 然后分一下几种 ...