具有大量元素的WPF绘图性能 [英] WPF drawing performance with large numbers of elements

查看:172
本文介绍了具有大量元素的WPF绘图性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在WPF中创建自定义控件,以显示围棋游戏(有关外观,请参见此处)。我已经或多或少地完成了对节点的布局,但是我发现的一个问题是,当节点的数量大于约3时,它开始呈现/导航(在滚动查看器中)的速度明显变慢。 30.由于平均每局围棋游戏包含约200步(更不用说玩家可能会插拔的其他分支),所以在任何现实游戏中这都是一个很大的问题。

I'm trying to create a custom control in WPF to display the game tree for a game of go (see here for what it looks like). I've more-or-less got it working laying out the nodes, but one problem I've found is that it begins gets noticeably slow to render/navigate (it's in a scroll viewer) when the number of nodes gets larger than about 30. Since an average game of go consists of around 200 moves (not to mention other branches that the player might fork onto), this is going to be a fairly big problem in any realistic game.

目前,我正在为游戏节点使用单独的用户控件(每个控件都是带有阴影位图效果并带有一些文本注释的椭圆)和用于树中弧线的简单线条,所有这些都是

Currently, I'm using individual user controls for the game nodes (each being an ellipse with a shadow bitmap effect on it and some text annotation) and simple lines for the arcs in the tree, all of which are positioned absolutely in a canvas.

布局算法不是这样的问题,因为仅在创建新分支时才执行(否则可以添加节点)直接位于其父节点的下方,因此无需重新定位其他节点)。主要问题是,对此画布及其元素的任何形式的处理都非常缓慢,大概是由于其子代数过多所致。显然,随着树的宽度和复杂度的增加,它会变慢,因为存在更多的弧以及节点。

The layout algorithm isn't such a problem since this only has to be executed when a new branch is created (otherwise the node can be added directly beneath its parent, so no other nodes need to be relocated). The main problem is simply that any kind of manipulation of this canvas and its elements is very slow, presumably just due to the number of children it has. It obviously gets slower as the width and complexity of the tree increases, since there are more arcs as well as nodes.

我的问题:用这样的方式绘制大型/复杂结构的正确方法是:随着它的增长,渲染不会太慢?

My question: What's the proper way to about drawing a large/complex structure like this in such a way that it doesn't render too slowly as it grows?

编辑:这与我的其他问题

编辑:以下是我用于节点的用户控件的标记:

Here's the markup for the user controls I'm using for the nodes:

<UserControl x:Class="Go.UI.GameNodeMarker"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:Go.UI"
             xmlns:wpftools="clr-namespace:WpfTools.Extensions;assembly=WpfTools"
             x:Name="_This">
    <UserControl.Resources>
        <!-- Some brushes, resources, etc. are omitted -->
        <Style x:Key="StoneStyle" TargetType="{x:Type Ellipse}">
            <Setter Property="StrokeThickness" Value="0"/>
            <Setter Property="BitmapEffect" Value="{StaticResource StoneEffect}"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=_This, Path=StoneColour}"  Value="Black">
                    <Setter Property="Fill" Value="{StaticResource BlackStoneBrush}"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding ElementName=_This, Path=StoneColour}" Value="White">
                    <Setter Property="Fill" Value="{StaticResource WhiteStoneBrush}"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding ElementName=_This, Path=IsEmpty}" Value="True">
                    <Setter Property="Fill" Value="{StaticResource EmptyBrush}"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </UserControl.Resources>
    <Grid>
        <Ellipse Style="{StaticResource StoneStyle}"/>
        <TextBlock Text="{Binding ElementName=_This, Path=Text}"
                   HorizontalAlignment="Center"
                   VerticalAlignment="Center"
                   FontWeight="Medium"/>
    </Grid>
</UserControl>

这些动态地添加到画布上以绘制树。

These are being added dynamically to a canvas in order to draw the tree.

推荐答案

渲染控件实际上非常昂贵。如果在客户区域中进行自定义绘图(自己绘制项目而不是放置控件),您将看到性能显着提高。

Rendering controls is actually quite expensive. If you do custom drawing (draw the items yourself instead of placing controls) in the client area you will see your performance increase dramatically.

这是我做这类工作的一次又一次的经历。如果您需要十几个控件,请选择绘制所有者。

This has been my experience time and again when doing this type of work. If you need more than a dozen controls, go for owner drawn.

顺便说一句,不要让自定义绘制吓倒您。并没有比动态放置控件复杂得多。

By the way, don't let custom drawing intimidate you. It's not too much more complex than placing controls dynamically.

链接到自定义WPF绘图示例

这篇关于具有大量元素的WPF绘图性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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