【问题标题】:lineNumbersCanvas.Width Causing Exception When WindowState=MaximizedlineNumbersCanvas.Width 在 WindowState=Maximized 时导致异常
【发布时间】:2011-06-20 09:08:14
【问题描述】:

当我设置我的 WindowState = Maximized 时出现一个奇怪的错误(如果我将它设置为正常然后全屏则工作正常!!)。调试给了我一个讨厌的异常,并希望在这里得到一些指示。

例外:

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

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

【问题讨论】:

  • lineNumbersCanvas.Width = GetFormattedTextWidth(string.Format("{0:0000}", totalLineCount)) + 5;
  • 这表明lineNumbersCanvas 为空。有没有在那行打断点看看?
  • 我设置了一个断点,但除了设置为 null 之外,我并没有看到任何信息。我不太明白为什么当 WindowState 设置为非最大化时它会起作用。

标签: c# wpf xaml windowstate


【解决方案1】:

这也是我在使用 SyntaxHighlightBox 时遇到的一个错误。 我通过简单地将 Loaded 处理程序正在执行的所有操作移动到方法 OnApplyTemplate() 的覆盖来修复它。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-27
    • 1970-01-01
    • 2013-06-30
    • 1970-01-01
    相关资源
    最近更新 更多