时间:2022年02月09日 | 作者 : aaronyang | 分类 : WPF | 浏览: 951次 | 评论 0 人
【支持NET6】
github地址:GitHub - icsharpcode/AvalonEdit: The WPF-based text editor component used in SharpDevelop
官方文档:AvalonEdit - Table of Content
它有个很好的项目GitHub - icsharpcode/WpfDesigner: The WPF Designer from SharpDevelop
GitHub - WPFDevelopersOrg/XamlViewer: XAML Viewer is a lightweight XAML editor.
新建wpf .net framework4.5的项目,nuget搜avalonedit
准备工作
<Window x:Class="avaedit.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:avaedit" mc:Ignorable="d" WindowStartupLocation="CenterScreen" xmlns:avalonEdit="http://icsharpcode.net/sharpdevelop/avalonedit" Title="" Height="700" Width="1200"> <Grid> </Grid> </Window>
<Window x:Class="avaedit.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:avaedit" mc:Ignorable="d" WindowStartupLocation="CenterScreen" xmlns:avalonEdit="http://icsharpcode.net/sharpdevelop/avalonedit" Title="" Height="700" Width="1200"> <Grid> <avalonEdit:TextEditor x:Name="textEditor" SyntaxHighlighting="C#"/> </Grid> </Window>
运行后,粘贴一段C#代码
看了下代码,原生自己写的
这里以xml文件做demo,内置了xml的高亮规则和折叠
<?xml version="1.0"?> <SyntaxDefinition name="Custom Highlighting" xmlns="http://icsharpcode.net/sharpdevelop/syntaxdefinition/2008"> <Color name="Comment" foreground="Green" /> <Color name="String" foreground="Blue" /> <!-- This is the main ruleset. --> <RuleSet> <Span color="Comment" begin="//" /> <Span color="Comment" multiline="true" begin="/\*" end="\*/" /> <Span color="String"> <Begin>"</Begin> <End>"</End> <RuleSet> <!-- nested span for escape sequences --> <Span begin="\\" end="." /> </RuleSet> </Span> <Keywords fontWeight="bold" foreground="Blue"> <Word>if</Word> <Word>else</Word> <!-- ... --> </Keywords> <Keywords fontWeight="bold" fontStyle="italic" foreground="Red"> <Word>AvalonEdit</Word> </Keywords> <!-- Digits --> <Rule foreground="DarkBlue"> \b0[xX][0-9a-fA-F]+ # hex number | \b ( \d+(\.[0-9]+)? #number with optional floating point | \.[0-9]+ #or just starting with floating point ) ([eE][+-]?[0-9]+)? # optional exponent </Rule> </RuleSet> </SyntaxDefinition>
后台单击折叠按钮
private static FoldingManager foldingManager = null; private static XmlFoldingStrategy foldingStrategy = new XmlFoldingStrategy(); private void zhedie_Click(object sender, RoutedEventArgs e) { //默认语法高亮规则 textEditor.SyntaxHighlighting = HighlightingManager.Instance.GetDefinitionByExtension(".xml"); //折叠 foldingManager = FoldingManager.Install(textEditor.TextArea); //textEditor.Text = ""; foldingStrategy.UpdateFoldings(foldingManager, textEditor.Document); }
运行后,粘贴一段xml
单击折叠后
折叠后,复制文本都是可以的
行号的显示颜色 LineNumbersForeground
引入xmlns:system="clr-namespace:System;assembly=mscorlib",xaml上设置数字
<avalonEdit:TextEditor LineNumbersForeground="Red" Grid.Row="1" x:Name="textEditor" SyntaxHighlighting="C#" ShowLineNumbers="True"> <avalonEdit:TextEditor.Options> <avalonEdit:TextEditorOptions ShowSpaces="True" > </avalonEdit:TextEditorOptions> </avalonEdit:TextEditor.Options> </avalonEdit:TextEditor>
通过Options指定设置,F12可以看到很多设置,比如ShowSpaces="True",显示虚线
删掉了一些xml节点,折叠节点不是实时更新 折叠的,修改折叠按钮后面的代码
private void zhedie_Click(object sender, RoutedEventArgs e) { //默认语法高亮规则 textEditor.SyntaxHighlighting = HighlightingManager.Instance.GetDefinitionByExtension(".xml"); //折叠 if (foldingManager != null) { FoldingManager.Uninstall(foldingManager); foldingManager.Clear(); } foldingManager = FoldingManager.Install(textEditor.TextArea); //textEditor.Text = ""; foldingStrategy.UpdateFoldings(foldingManager, textEditor.Document); }
下面几个设置没看到效果
<avalonEdit:TextEditorOptions ShowSpaces="True" WordWrapIndentation="14" InheritWordWrapIndentation="true"> <avalonEdit:TextEditorOptions.ColumnRulerPosition> <system:Int32>10</system:Int32> </avalonEdit:TextEditorOptions.ColumnRulerPosition> </avalonEdit:TextEditorOptions>
需要先自己继承 ICompletionData 接口创建提示数据
public class CompletionData : ICompletionData { public CompletionData(string text) { Text = text; } public ImageSource Image => null; public string Text { get; } public object Content => Text; public object Description =>this.Text +" 的描述"; public double Priority { get; } public void Complete(TextArea textArea, ISegment completionSegment, EventArgs insertionRequestEventArgs) { textArea.Document.Replace(completionSegment, Text); } }
输入点号,主要CompletionWindow这个窗体show就可以了,主要是自己要判断什么时候弹,弹什么提示
private CompletionWindow _completionWindow; private void TextAreaOnTextEntered(object sender, TextCompositionEventArgs e) { if (e.Text == ".") { _completionWindow = new CompletionWindow(textEditor.TextArea); var completionData = _completionWindow.CompletionList.CompletionData; completionData.Add(new CompletionData("Name")); completionData.Add(new CompletionData("Gender")); completionData.Add(new CompletionData("Class")); _completionWindow.Show(); _completionWindow.Closed += (o, args) => _completionWindow = null; } }
输入.
暂时不知道怎么拿到前面的 Student这个单词的值,毕竟要根据什么值,然后提示啥
=============2022 02 14 ============================== www.ayjs.net==================================
默认文本区域没有右键菜单的
推荐您阅读更多有关于“avalonedit,wpf,”的文章
抖音:wpfui 工作wpf
目前在合肥企迈科技公司上班,加我QQ私聊
2023年11月网站停运,将搬到CSDN上
AYUI8全源码 Github地址:前往获取
杨洋(AaronYang简称AY,安徽六安人)和AY交流
高中学历,2010年开始web开发,2015年1月17日开始学习WPF
声明:AYUI7个人与商用免费,源码可购买。部分DEMO不免费
查看捐赠AYUI7.X MVC教程 更新如下:
第一课 第二课 程序加密教程
额 本文暂时没人评论 来添加一个吧
发表评论