代码中的AvalonEdit更改语法突出显示

时间:2013-04-23 12:39:33

标签: c# wpf syntax-highlighting text-editor avalonedit

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

XAML:

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

C#:

public string syntaxHighlighting { get; set; }

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

但语法突出显示没有改变。我究竟做错了什么?对我的问题有更好的解决方案吗?

5 个答案:

答案 0 :(得分:30)

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

答案 1 :(得分:6)

你走了:

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();

修改:

由于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);

答案 2 :(得分:1)

只需使用TypeConverter

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

答案 3 :(得分:1)

如果要绑定到字符串,可以定义一个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);
    }
}

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

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

答案 4 :(得分:0)

syntaxHighlighting不是字符串!你的代码可能无法编译!请注意,XAML中的SyntaxHighlighting使用markup extensions,它实例化一个SyntaxHighlighting实例,而不是一个字符串。

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

转到sharpdevelop并下载源代码。