简体   繁体   English

如何创建快速轻量的UIElement

[英]How to create a fast and lightweight UIElement

I am creating a path of Markers to be displayed on a GMapControl in my C#/XAML (WPF) application. 我正在创建要在我的C#/ XAML(WPF)应用程序中的GMapControl上显示的标记的路径。 The control requires I create a UIElement to be overlaid on the map as a marker. 该控件要求我创建一个UIElement作为标记叠加在地图上。 I have created a very simple UserControl for this purpose, as follows: 为此,我创建了一个非常简单的UserControl,如下所示:

<UserControl x:Class="Project.Resources.Circle"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Ellipse Width="5" Height="5" Stroke="Red" Fill="Red" />
    </Grid>
</UserControl>

However, when I build the line, creating anything up to 400 instances of this control, my application freezes. 但是,当我建立该行时,最多创建该控件的400个实例,我的应用程序将冻结。 Since I can only seem to create UserControl's on the UI thread (and not in a Thread or BackgroundWorker), what can I do to speed up the creation of new Circle instances? 由于我似乎只能在UI线程上创建UserControl(而不是在Thread或BackgroundWorker中),因此该怎么办才能加快创建新Circle实例的速度?

Is there a more lightweight UIElement than a UserControl? 是否有比UserControl更轻巧的 UIElement? Any guidance on this appreciated. 任何对此的指导表示赞赏。

You could create a minimal derived UIElement like this: 您可以创建一个最小的派生UIElement,如下所示:

public class Circle : UIElement
{
    protected override void OnRender(DrawingContext drawingContext)
    {
        const double radius = 2.5;
        drawingContext.DrawEllipse(Brushes.Red, null, new Point(), radius, radius);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM