Python wechat ordering applet course video
https://edu.csdn.net/course/detail/36074
Python practical quantitative transaction financial management system
https://edu.csdn.net/course/detail/35475
AvalonEdit Is a WPF based text editor component. It is made by Daniel Grunwald by SharpDevelop Written. Starting with version 5.0, AvalonEdit is released under a MIT license.
By using AvalonEdit, partners can easily integrate code editors into their programs. AvalonEdit in Luyao toolbox It is widely used in.
Install AvalonEdit
First, install AvalonEdit through NuGet at: https://www.nuget.org/packages/AvalonEdit .
Then paste the following code into the XAML file to create the simplest AvalonEdit control:
<avalonEdit:TextEditor xmlns:avalonEdit="http://icsharpcode.net/sharpdevelop/avalonedit" Name="TextEditor" SyntaxHighlighting="C#" FontFamily="Consolas" FontSize="10pt" LineNumbersForeground="Black" ShowLineNumbers="True"> avalonEdit:TextEditor>
Xmlns: AvalonEdit in the second line=“ http://icsharpcode.net/sharpdevelop/AvalonEdit "Is the namespace of AvalonEdit, which is hard coded.
AvalonEdit has built-in various syntax highlighting rules. Syntax highlighting = "C #" means to highlight C #. If you need to highlight the XML code, just change c# to XML: syntax highlighting = "XML".
ShowLineNumbers="True" in the ninth line represents the line number of each line, which is very useful when displaying long text in a single line. The default is False.
Use the JetBrand Mono font to display the code
The JetBrand Mono font is very suitable for displaying code, and the font file is very small (only about 200K) and can be directly embedded in the application.
First, download the JetBrand Mono font file jetbrainsmono Copy TTF to the Resources folder of the project and set the "generate operation" of the file to "resource".
Then set the FontFamily attribute of TextEditor:
pack://application:,,, / {assembly name}; component/Resources/#JetBrains Mono
Where, {assembly name} needs to be replaced with your own application information.
MVVM binding for AvalonEdit
We can get or set the content in the code editor through the Text property of the TextEditor, but this property is not a dependent property, so we can't bind it directly to the ViewModel.
A Behavior for AvalonEdit can help solve this problem:
using ICSharpCode.AvalonEdit; using Microsoft.Xaml.Behaviors; using System; using System.Windows; public sealed class AvalonEditBehaviour : Behavior<TextEditor> { public static readonly DependencyProperty CodeTextProperty = DependencyProperty.Register("CodeText", typeof(string), typeof(AvalonEditBehaviour), new FrameworkPropertyMetadata(default(string), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, PropertyChangedCallback)); public string CodeText { get { return (string)GetValue(CodeTextProperty); } set { SetValue(CodeTextProperty, value); } } protected override void OnAttached() { base.OnAttached(); if (AssociatedObject != null) AssociatedObject.TextChanged += AssociatedObjectOnTextChanged; } protected override void OnDetaching() { base.OnDetaching(); if (AssociatedObject != null) AssociatedObject.TextChanged -= AssociatedObjectOnTextChanged; } private void AssociatedObjectOnTextChanged(object sender, EventArgs eventArgs) { if (sender is TextEditor textEditor) { if (textEditor.Document != null) CodeText = textEditor.Document.Text; } } private static void PropertyChangedCallback( DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs) { var behavior = dependencyObject as AvalonEditBehaviour; if (behavior.AssociatedObject != null) { var editor = behavior.AssociatedObject; if (editor.Document != null) { var caretOffset = editor.CaretOffset; editor.Document.Text = dependencyPropertyChangedEventArgs.NewValue.ToString(); if (caretOffset <= editor.Document.Text.Length) editor.CaretOffset = caretOffset; } } } }
Suppose that the above Code exists in the Behaviors namespace, and the ViewModel has a notification property named Code. The XAML Code is as follows:
<avalonEdit:TextEditor xmlns:avalonEdit="http://icsharpcode.net/sharpdevelop/avalonedit"> <i:Interaction.Behaviors xmlns:i="http://schemas.microsoft.com/xaml/behaviors"> <bh:AvalonEditBehaviour xmlns:bh="clr-namespace:Behaviors" CodeText="{Binding Code}"/> i:Interaction.Behaviors> avalonEdit:TextEditor>
Open the quick search box
When using ILSpy to view the assembly source code, you can open a quick search box with the shortcut key "CTRL + F", the text to be searched will be highlighted, and navigation of search results is supported:
ILSpy's quick search function
In fact, ILSpy also uses AvalonEdit control. It only needs one line of code to open the search panel:
ICSharpCode.AvalonEdit.Search.SearchPanel.Install(TextEditor);
The above code is called after the InitializeComponent method in the postcode constructor of XAML.
Code folding function
Code folding is a function of some text editors, source code editors and ides. It allows users to selectively hide and display – "collapse" – various parts of the current editing file as part of routine editing operations. This allows the user to manage a large amount of text while viewing only text subparts that are particularly relevant at any given time.
Code folding function of Luyao toolbox
For XML language code folding, you need to use FlodingManager and XmlFoldingStrategy.
<avalonedit:TextEditor SyntaxHighlighting="XML" TextChanged="CodeEditor\_TextChanged" ShowLineNumbers="True" WordWrap="True" x:Name="CodeEditor"> <avalonedit:TextEditor.ContextMenu> <ContextMenu> <MenuItem Header="collapse all" x:Name="CloseMenuItem" Click="CloseMenuItem\_Click">MenuItem> <MenuItem Header="Expand all" x:Name="OpenMenuItem" Click="OpenMenuItem\_Click">MenuItem> ContextMenu> avalonedit:TextEditor.ContextMenu> avalonedit:TextEditor>
The corresponding post code is as follows:
using ICSharpCode.AvalonEdit.Folding; using System; using System.Windows; using System.Windows.Controls; public MyUserControl() //Constructor { InitializeComponent(); foldingManager = FoldingManager.Install(CodeEditor.TextArea); } FoldingManager foldingManager = null; XmlFoldingStrategy foldingStrategy = new XmlFoldingStrategy(); private void CodeEditor\_TextChanged(object sender, EventArgs e) { if (foldingManager == null) return; foldingStrategy.UpdateFoldings(foldingManager, CodeEditor.Document); } private void CloseMenuItem\_Click(object sender, RoutedEventArgs e) { if (foldingManager == null) return; var isFrist = true; foreach (var item in foldingManager.AllFoldings) { if (isFrist) { isFrist = false; continue; } item.IsFolded = true; } } private void OpenMenuItem\_Click(object sender, RoutedEventArgs e) { if (foldingManager == null) return; foreach (var item in foldingManager.AllFoldings) { item.IsFolded = false; } }
end
The above is the whole content of this film. All the codes shown in this article can be found in Luyao toolbox Find application in.