2011-02-03 12 views
0

WindowState = Maximizedを設定すると、奇妙なエラーが発生します。デバッグは私に厄介な例外を与え、ここにいくつかのポインタを得ることを望んでいました。lineNumbersCanvas.Width WindowStateが最大化されたときに例外が発生する

例外:

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

これは67行ですか? –

+0

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

+0

これは、 'lineNumbersCanvas'がnullであることを示唆しています。あなたはその行にブレークポイントを置いて見ましたか? –

答えて

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(); 
} 
関連する問題