2013-04-23 47 views
8

我想在我的代碼中更改AvalonEdit的語法高亮。AvalonEdit在代碼中更改語法高亮

XAML:

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

C#:

public string syntaxHighlighting { get; set; } 

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

但語法高亮也不會改變。我究竟做錯了什麼?我的問題有更好的解決方案嗎?

回答

5

在這裏你去:

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拋出AccessViolations中的WinForms,我用AvalonEdit中的WinForms:

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

它的工作原理,謝謝。 – 2013-05-06 05:50:23

0

syntaxHighlighting不是一個字符串!你的代碼可能無法編譯!請注意,此處XAML中的SyntaxHighlighting使用markup extensions,它實例化了SyntaxHighlighting的一個實例,而不是字符串。

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

轉到sharpdevelop並下載源代碼。

+0

哦,我明白了,你知道我能解決嗎?當我只有它的名字時,我可以更改語法嗎? – 2013-04-23 12:55:17

+0

你最好下載SharpDevelop源碼,並對源代碼進行研究,這將有很大的幫助。 – David 2013-04-23 12:58:54

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

'ICSharpCode.AvalonEdit.Highlighting.HighlightingManager.Instance.GetDefinitionByExtension(Path.GetExtension(FileName));' – 2016-03-11 14:05:55

1

只需使用TypeConverter

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

如果你要綁定到一個字符串,您可以定義的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}}" />