wpf AvalonEdit 更改代码中的语法高亮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16169584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:39:33  来源:igfitidea点击:

AvalonEdit Change Syntax Highlighting in Code

c#wpfsyntax-highlightingtext-editoravalonedit

提问by Karl_Schuhmann

I want to change the Syntax Highlighting of AvalonEdit in my Code.

我想在我的代码中更改 AvalonEdit 的语法突出显示。

XAML:

XAML:

 <avalonEdit:TextEditor Name="textEditor" SyntaxHighlighting="{Binding syntaxHighlighting}" />

C#:

C#:

public string syntaxHighlighting { get; set; }

public MainWindow()
{
     InitializeComponent();
     syntaxHighlighting = "C#";
     DataContext = this;
}

But the Syntax Highlighting is not changed. What am I doing wrong? Is there a better solution for my problem?

但是语法高亮没有改变。我究竟做错了什么?我的问题有更好的解决方案吗?

采纳答案by Jeremy Thompson

Here you go:

干得好:

ICSharpCode.AvalonEdit.TextEditor textEditor = new ICSharpCode.AvalonEdit.TextEditor();
textEditor.ShowLineNumbers = true;
string dir = @"C:\Program Files\MyFolder\";
#if DEBUG
dir = @"C:\Dev\Sandbox\SharpDevelop-master\src\Libraries\AvalonEdit\ICSharpCode.AvalonEdit\Highlighting\Resources\";
#endif

Stream xshd_stream = File.OpenRead(dir + "CSharp-Mode.xshd");
XmlTextReader xshd_reader = new XmlTextReader(xshd_stream);
textEditor.SyntaxHighlighting = ICSharpCode.AvalonEdit.Highlighting.Xshd.HighlightingLoader.Load(xshd_reader, ICSharpCode.AvalonEdit.Highlighting.HighlightingManager.Instance);
xshd_reader.Close();
xshd_stream.Close();

Edit:

编辑:

Since ICSharp.TextEditor throws AccessViolations in WinForms, I use AvalonEdit in WinForms:

由于 ICSharp.TextEditor 在 WinForms 中抛出 AccessViolations,我在 WinForms 中使用 AvalonEdit:

ElementHost host = new ElementHost();
host.Size = new Size(200, 100);
host.Location = new Point(100, 100);
host.Child = textEditor;
this.Controls.Add(host);

回答by user2339814

ICSharpCode.AvalonEdit.Highlighting.HighlightingManager.Instance.GetDefinition("C#");

回答by Alexey

Just use TypeConverter

只需使用 TypeConverter

var typeConverter = new HighlightingDefinitionTypeConverter();
var xmlSyntaxHighlighter = (IHighlightingDefinition)typeConverter.ConvertFrom("XML");
var csSyntaxHighlighter = (IHighlightingDefinition)typeConverter.ConvertFrom("C#");
CSharpEditor.SyntaxHighlighting = csSyntaxHighlighter;
XmlEditor.SyntaxHighlighting = xmlSyntaxHighlighter;

回答by HotN

If you want to bind to a string, you can define an IValueConverter to wrap the built in HighlightingDefinitionTypeConverter:

如果要绑定到字符串,可以定义一个 IValueConverter 来包装内置的 HighlightingDefinitionTypeConverter:

using System;
using System.Globalization;
using System.Windows.Data;
using ICSharpCode.AvalonEdit.Highlighting;

public class HighlightingDefinitionConverter : IValueConverter
{
    private static readonly HighlightingDefinitionTypeConverter Converter = new HighlightingDefinitionTypeConverter();

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return Converter.ConvertFrom(value);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return Converter.ConvertToString(value);
    }
}

Then, just add the converter to your binding:

然后,只需将转换器添加到您的绑定中:

<avalonEdit:TextEditor Name="textEditor" SyntaxHighlighting="{Binding syntaxHighlighting, Converter={StaticResource HighlightingDefinitionConverter}}" />

回答by David

syntaxHighlighting is not a string! your code might not compile! Note that SyntaxHighlighting in XAML here uses markup extensions, which instantiates a instance of SyntaxHighlighting, not a string.

syntaxHighlighting 不是字符串!您的代码可能无法编译!请注意,此处 XAML 中的 SyntaxHighlighting 使用标记扩展,它实例化 SyntaxHighlighting 的实例,而不是字符串。

private void OnTabLoaded(object sender, RoutedEventArgs e)
{
   textEditor.SyntaxHighlighting = HighlightingLoader.Load(..., HighlightingManager.Instance);
   textEditor.SyntaxHighlighting.MainRuleSet=...
}

Go to sharpdevelopand download the source code.

sharpdevelop下载源代码。