写这篇主要是为了验证任何元素自身都具备绘图功能.
在默认Window中重写OnRender方法
protected override void OnRender(DrawingContext drawingContext) { drawingContext.DrawRectangle(Brushes.White, null, new Rect(0, 0, this.ActualWidth, ActualHeight)); drawingContext.DrawRectangle(Brushes.Blue, null, new Rect(0, 0, 300, 300)); drawingContext.DrawRectangle(Brushes.Green, null, new Rect(0, 0, 200, 200)); drawingContext.DrawRectangle(Brushes.Red, null, new Rect(0, 0, 100, 100)); base.OnRender(drawingContext); }
结果还是空白一片.
窗体的默认视觉树
解决方法1:去掉默认视觉树
protected override Visual GetVisualChild(int index) { return null; }
结果恢复正常
但此方法破坏了窗体默认视觉树,影响了其内部功能
解决方法2:将默认北京颜色设置为空
this.Background = null;
搞了半天原来是被遮住了
记录一下技巧