首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WindowState=Maximized时导致异常的lineNumbersCanvas.Width

WindowState=Maximized时导致异常的lineNumbersCanvas.Width
EN

Stack Overflow用户
提问于 2011-02-03 08:52:45
回答 1查看 215关注 0票数 0

当我设置我的WindowState = Maximized时,得到一个奇怪的错误(如果我将它设置为正常然后全屏,效果很好!!)。Debug给了我一个令人讨厌的异常,并希望在这里得到一些指示。

例外:

代码语言:javascript
复制
System.NullReferenceException was unhandled
Message=Object reference not set to an instance of an object.
Source=SyntaxHighlight
StackTrace:
at SyntaxHighlight.SyntaxHighlightBox.<.ctor>b__0(Object s, RoutedEventArgs e) in    
C:\Test\SyntaxHighlight\src\SyntaxHighlightBox.xaml.cs:line 67

SyntaxHighlighBox.xaml.cs

代码语言:javascript
复制
public SyntaxHighlightBox() {
    InitializeComponent();

    MaxLineCountInBlock = 100;
    LineHeight = FontSize * 1.3;
    totalLineCount = 1;
    blocks = new List<InnerTextBlock>();

    Loaded += (s, e) => {
        renderCanvas = (DrawingControl)Template.FindName("PART_RenderCanvas", this);
        lineNumbersCanvas = (DrawingControl)Template.FindName("PART_LineNumbersCanvas", this);
        scrollViewer = (ScrollViewer)Template.FindName("PART_ContentHost", this);

        lineNumbersCanvas.Width = GetFormattedTextWidth(string.Format("{0:0000}", totalLineCount)) + 5;

        scrollViewer.ScrollChanged += OnScrollChanged;

        InvalidateBlocks(0);
        InvalidateVisual();
    };

    SizeChanged += (s, e) => {
        if (e.HeightChanged == false)
            return;
        UpdateBlocks();
        InvalidateVisual();
    };
EN

回答 1

Stack Overflow用户

发布于 2015-02-16 18:45:24

这也是我在使用SyntaxHighlightBox时遇到的一个错误。我只需将加载的处理程序所做的所有操作移动到OnApplyTemplate()方法的覆盖中,即可修复它。

代码语言:javascript
复制
public SyntaxHighlightBox() {
    InitializeComponent();

    MaxLineCountInBlock = 100;
    LineHeight = FontSize * 1.3;
    totalLineCount = 1;
    blocks = new List<InnerTextBlock>();

    // The Loaded handler is not needed anymore.

    SizeChanged += (s, e) => {
        if (e.HeightChanged == false)
            return;
        UpdateBlocks();
        InvalidateVisual();
    };

    TextChanged += (s, e) => {
        UpdateTotalLineCount();
        InvalidateBlocks(e.Changes.First().Offset);
        InvalidateVisual();
    };
}

public override void OnApplyTemplate()
{
    base.OnApplyTemplate();

    // OnApplyTemplate() is called after Loaded, and this is where templated parts should be retrieved.

    renderCanvas = (DrawingControl)Template.FindName("PART_RenderCanvas", this);
    lineNumbersCanvas = (DrawingControl)Template.FindName("PART_LineNumbersCanvas", this);
    scrollViewer = (ScrollViewer)Template.FindName("PART_ContentHost", this);

    lineNumbersCanvas.Width = GetFormattedTextWidth(string.Format("{0:0000}", totalLineCount)) + 5;

    scrollViewer.ScrollChanged += OnScrollChanged;

    InvalidateBlocks(0);
    InvalidateVisual();
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4881664

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档