为什么覆盖 WPF Window.OnRender 方法无法绘制到屏幕? [英] Why does overriding the WPF Window.OnRender method fail to draw to the screen?

查看:24
本文介绍了为什么覆盖 WPF Window.OnRender 方法无法绘制到屏幕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到在覆盖 System.Windows.Window OnRender 方法时无法绘制到屏幕上.我相信这是有充分理由的,但找不到解释.

I noticed that when overriding the System.Windows.Window OnRender method fails to draw to the screen. I'm sure there's a good reason for this but was unable to find the explanation.

以下代码演示了这一点:

The following code demonstrates this:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        Pen pen = new Pen();

        public Window1()
        {
            InitializeComponent();
            pen.Brush = Brushes.Black;
        }

        protected override void OnRender(DrawingContext dc)
        {
            base.OnRender(dc);
            dc.DrawRectangle(Brushes.Black, pen, new Rect(0, 0, 600, 400));
            dc.DrawEllipse(Brushes.Green, pen, new Point(300, 300), 50, 50);
        }
    }
}

替换 FrameworkElement 中的绘图逻辑(然后将元素设置为窗口的内容)工作正常.

replacing the drawing logic in a FrameworkElement (then setting the element as the content of a window) work fine.

谢谢,

丹尼

推荐答案

Chales Petzold 给出了答案 此处.

The answer is given by Chales Petzold here.

他的回答是不知道为什么",但是评论正确地表明将窗口的背景设置为透明可以修复该错误.

His answer is 'don't know why', however a comment correctly suggests that setting the window's Background to transparent fixes the bug.

这篇关于为什么覆盖 WPF Window.OnRender 方法无法绘制到屏幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆