如何创建快速、轻量级的UIElement

本文关键字:轻量级 UIElement 何创建 创建 | 更新日期: 2023-09-27 18:00:49

我正在创建一个标记的路径,以便在我的C#/XAML(WPF(应用程序中的GMapControl上显示。该控件要求我创建一个UIElement作为标记覆盖在地图上。为此,我创建了一个非常简单的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>

然而,当我构建行,创建该控件的多达400个实例时,我的应用程序会冻结。由于我似乎只能在UI线程上创建UserControl(而不是在线程或BackgroundWorker中(,我该怎么做才能加快新Circle实例的创建?

有没有比UserControl更轻量级的UIElement?对此有任何指导意见,不胜感激。

如何创建快速、轻量级的UIElement

您可以创建一个最小派生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);
    }
}