【问题标题】:How can I reset ICSharpCode.AvalonEdit syntax highlighting?如何重置 ICSharpCode.AvalonEdit 语法突出显示?
【发布时间】:2013-01-13 05:23:19
【问题描述】:

我将 ICSharpCode AvalonEdit 源代码编辑 WPF 控件托管到我的 Windows 窗体 C# 应用程序中。我知道下面的代码会加载一个语法高亮定义:

ICSharpCode.AvalonEdit.TextEditor MyEditor = new ICSharpCode.AvalonEdit.TextEditor();
MyEditor.ShowLineNumbers = true;

Stream xshd_stream = File.OpenRead("C:\\Syntax Highlighting\\php.xsdh");
XmlTextReader xshd_reader = new XmlTextReader(xshd_stream);

// Apply the new syntax highlighting definition.
MyEditor.SyntaxHighlighting = ICSharpCode.AvalonEdit.Highlighting.Xshd.HighlightingLoader.Load(
    xshd_reader, ICSharpCode.AvalonEdit.Highlighting.HighlightingManager.Instance
);

xshd_reader.Close();
xshd_stream.Close();

但是,如果在我已经设置了语法高亮定义之后,我不想要任何语法高亮,我只想让它显示为纯文本怎么办?如何禁用 AvalonEdit 控件中的语法高亮显示?

【问题讨论】:

    标签: c# wpf winforms avalonedit icsharpcode


    【解决方案1】:

    你试过MyEditor.SyntaxHighlighting = null吗?
    这对我有用:

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
    
        <avalonEdit:TextEditor SyntaxHighlighting="C#" x:Name="TextEditor">
            public class Foo
            {
            }
        </avalonEdit:TextEditor>
    
        <Button Grid.Row="1" Content="Reset syntax highlighting" Click="Button_Click" />
    </Grid>
    

    代码隐藏:

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            TextEditor.SyntaxHighlighting = null; // highlighting disappears
        }
    

    【讨论】:

    • 这给了我一个Object reference not set to an instance of an object 错误。
    • @BrandonMiller:我自己检查过并更新了答案 - 设置 null 值工作正常(我使用过 WPF 应用程序)。您能否发布 NRE 的堆栈跟踪,确定这是 AvalonEdit 的问题?
    猜你喜欢
    • 1970-01-01
    • 2015-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多