lineNumbersCanvas.Width 在 WindowState=Maximized 时导致异常
当我设置 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这也是我在使用 SyntaxHighlightBox 时遇到的一个错误。
我通过简单地将 Loaded 处理程序正在执行的所有操作移动到方法 OnApplyTemplate() 的重写来修复它。
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().