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

XAML:

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

C#:
public string syntaxHighlighting { get; set; }

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

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

最佳答案

干得好:

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

关于c# - 代码中的AvalonEdit更改语法突出显示,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16169584/

10-11 18:10