我尝试在我的MainWindow上绘制。我使用以下示例代码:
MainWindow.xaml.cs
namespace WpfApplication4
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
protected override void OnRender(DrawingContext drawingContext)
{
Trace.WriteLine("OnRender");
drawingContext.DrawRectangle(Brushes.Red, new Pen(Brushes.Black, 5), new Rect(20, 20, 250, 250));
base.OnRender(drawingContext);
}
}
}
MainWindow.xaml
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="400" Width="600">
</Window>
在输出窗口中,我看到消息"OnRender",但是没有绘制任何内容。
![MainWindow and Output](https://istack.dev59.com/6tG5Z.webp)
Background="Transparent"
。并且在你自己的绘图之前始终调用base.OnRender
,因为你想要在基类绘图(如果有)之上进行绘制。 - Clemens