lineNumbersCanvas.Width 在 WindowState=Maximized 时导致异常

发布于 2024-10-15 10:37:44 字数 1360 浏览 2 评论 0原文

当我设置 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();
    };

Getting a strange error, when I set my WindowState = Maximized (works fine If I set it to Normal and then full screen!!). Debug gives me a nasty exception and was hoping to get some pointers here.

Exception:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

辞取 2024-10-22 10:37:44

这也是我在使用 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();
}

This is a bug I encountered with SyntaxHighlightBox as well.
I fixed it by simply moving all that the Loaded handler is doing to an override of method 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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文