gpt4 book ai didi

wpf - DrawingVisual 未刷新

转载 作者:行者123 更新时间:2023-12-04 10:13:10 32 4
gpt4 key购买 nike

我创建自己的 FrameworkElement并覆盖 VisualChildrenCount{get;}GetVisualChild(int index)通过返回我自己的 DrawingVisual实例。

如果我在初始渲染后(例如在计时器处理程序中)使用 DrawingVisual.RenderOpen() 修改视觉内容的内容并绘制到上下文中,元素不会刷新。

这是最简单的示例:

using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Threading;

namespace VisualTest
{
public class TestControl : FrameworkElement
{
private readonly DrawingVisual _visual = new DrawingVisual();

public TestControl()
{
Draw(false);

var timer = new DispatcherTimer {Interval = new TimeSpan(0, 0, 2)};
timer.Tick += (sender, args) =>
{
Draw(true);
InvalidateVisual();
timer.Stop();
};
timer.Start();
}

protected override Visual GetVisualChild(int index)
{
return _visual;
}

protected override int VisualChildrenCount
{
get { return 1; }
}

private void Draw(bool second)
{
DrawingContext ctx = _visual.RenderOpen();
if (!second)
ctx.DrawRoundedRectangle(Brushes.Green, null, new Rect(0, 0, 200, 200), 20, 20);
else
ctx.DrawEllipse(Brushes.Red, null, new Point(100, 100), 100, 100);
ctx.Close();
}
}
}
InvalidateVisual()什么也没做。尽管如果您调整包含该元素的窗口的大小,它会更新。

关于如何正确刷新内容的任何想法?最好 没有 为我的元素引入新的依赖属性。

最佳答案

添加

this.AddVisualChild(_visual);
this.AddLogicalChild(_visual);

到 TestControl 类构造函数。

关于wpf - DrawingVisual 未刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1662987/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com