带BitmapCache的画布

带BitmapCache的画布

本文介绍了带BitmapCache的画布-删除子对象后渲染的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有一个包含以下元素的用户控件:

I have a user control that contains the following elements:

<UserControl
 ...>
  <ScrollViewer Name="LayoutScroll" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">

    <Grid Name="MainGrid">
      <Canvas x:Name="LayerObjects" Panel.ZIndex="2"
       Width="{Binding ActualWidth,ElementName=MainGrid}" Height="{Binding ActualHeight, ElementName=MainGrid}">
            <Canvas.CacheMode>
                <BitmapCache
                EnableClearType="False"
                SnapsToDevicePixels="False"
                RenderAtScale="2"/>
            </Canvas.CacheMode>
        </Canvas>
    </Grid>

  </ScrollViewer>
</UserControl>

我使用此用户控件将多个形状绘制到其中.绘图工作正常,删除工作也正常(我不知道我做了什么更改,只是无法弄清楚).每次我想从画布上删除一个元素时,我都会简单地做:

I use this user control to have multiple shapes drawn into it. The drawing is working perfectly and the removing was working as well (I dont know what have I changed, just cant figure it out).Every time I want to remove an element from this canvas I would just simply do:

LayerObject.Children.Remove(shape);

现在,当我执行此操作时,就不会从画布上从视觉上删除该形状.它只是呆在那里.当我移动窗口或放大画布时,形状消失了.因此,我的问题是,一旦元素被删除,是否有任何方法可以渲染"画布?

Now, when I do that the shape is not visually removed from the canvas. It just stays there. When I move the window or zoom into the canvas the shape dissapears.So, my question is, is there any way to "render" the canvas as soon as the element is removed ?

到目前为止,我尝试了什么(没有成功):

What have I tried so far (without success):

  • oCanvas.UpdateLayout();
  • oCanvas.InvalidateVisual();
  • 验证删除操作是否正在调度程序上运行.
  • 如果删除了CacheMode,但在处理多种形状时会失去性能.

推荐答案

我已解决此问题,方法是在更改Canvas子级之前先删除CacheMode,然后再将其重新启用.

I've resolved this by removing the CacheMode before changing the Canvas children and then put it back on.

CacheMode oTempCM = oCanvas.CacheMode;
oCanvas.CacheMode = null;
//do canvas operations ...
oCanvas.CacheMode = oTempCM;

谢谢@Clemens.

Thank you @Clemens.

这篇关于带BitmapCache的画布-删除子对象后渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 19:08