3

我在使用带有自定义图钉的 Bing 地图的 .NET 4 应用程序时遇到问题。缩放或移动地图时性能非常差。我正在使用ObservableCollection与 Bing 地图有数据绑定的 a。每次地图更改时,都会检查地图部分中的图钉。集合将被重新填充,最后 aNotifyCollectionChangedEvent被触发,这使得地图绘制图钉。

<bingMap:MapItemsControl ItemsSource="{Binding Locations}">
    <bingMap:MapItemsControl.ItemTemplate>
        <DataTemplate>
            <bingMap:Pushpin Location="{Binding Coordinates}" Height="Auto" Width="Auto" PositionOrigin="BottomCenter"
                Template="{StaticResource PushpinControlTemplateLoc}">
            </bingMap:Pushpin>
        </DataTemplate>
    </bingMap:MapItemsControl.ItemTemplate>
</bingMap:MapItemsControl >

我用性能分析器做了一些研究:InitializeComponent()我的自定义图钉类的方法平均需要 25% 到 35% 的时间!

地图上通常显示 10 到 25 个自定义图钉。如果我减少数据绑定的数量,它会变得更快一点,但仍然不够快。

我已经测试了将所有画笔声明为冻结的静态资源,但绘图仍然运行得很慢。

<SolidColorBrush x:Key="SolidColorBrushUnknownMsg" Color="Gray" ice:Freeze="True"  xmlns:ice="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options" />

我尝试使用<bingMap:Pushpin Location="{Binding Coordinates}"/>速度更快的 Microsoft 默认图钉。所以我的自定义图钉的使用或实现一定有问题。

这是我的其余代码:

自定义图钉类(仅自动生成的代码):

public partial class MyPushpin: System.Windows.Controls.Grid
{
    public MyPushpin()
    {
        InitializeComponent();
    }
}

自定义图钉 XAML 代码:

<Grid x:Class="Test.MyPushpin"
    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">

    <Grid.Resources>
        <Style BasedOn="{StaticResource DataTextBlock}" x:Key="test1" TargetType="TextBlock">
            <Setter Property="Background" Value="{StaticResource SolidColorBrushErrorMsg}"/>
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=TextLine1, Mode=OneWay}" Value="0">
                <Setter Property="Background" Value="{StaticResource BackgroundImage}"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Grid.Resources>

<Border CornerRadius="20" BorderBrush="White" Padding="7" Opacity="0.8" Width="120" Height="100" Background="{StaticResource BackgroundImage}">
    <Grid >
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>

        <TextBlock Grid.Row="0" Grid.Column="0" Text="test2"  Foreground="Black" />
        <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding TextLine2, StringFormat=N1,Mode=OneWay}" Style="{StaticResource DataTextBlock}" />
        <TextBlock Grid.Row="0" Grid.Column="2" Text="{Binding objCurrentPower.sUnit,Mode=OneWay}"  Foreground="Black" />

        <!--three more lines of textblocks with data bindings -->

    </Grid>
</Border>

为什么我的自定义图钉需要这么高的性能?

4

1 回答 1

2

问题已解决:与其使用单独的类和 xaml 并引用它们,不如ControlTemplate在实际包含 Bing 地图的类中使用自定义图钉:

<ControlTemplate x:Key="CutomPushpinTemplate" TargetType="bingMap:Pushpin">
    <Border CornerRadius="15" BorderBrush="White" Padding="7" Opacity="0.8" Width="120" Height="90" Background="{StaticResource Bgr_enercon}">
        <Grid >
            <Grid.RowDefinitions>
                <RowDefinition Height="16" />
                <RowDefinition Height="16" />
                <RowDefinition Height="16" />
                <RowDefinition Height="16" />
                <RowDefinition Height="16" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="45" />
                <ColumnDefinition Width="32" />
                <ColumnDefinition Width="23" />
            </Grid.ColumnDefinitions>

            <TextBlock Grid.Row="0" Grid.Column="0" Text="test2"  Foreground="Black" />
            <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding TextLine2, StringFormat=N1,Mode=OneWay}" Style="{StaticResource DataTextBlock}" />
            <TextBlock Grid.Row="0" Grid.Column="2" Text="{Binding objCurrentPower.sUnit,Mode=OneWay}"  Foreground="Black" />

            <!--three more lines of textblocks with data bindings -->

        </Grid>
    </Border>
</ControlTemplate>

此模板以这种方式用于 Bing 地图的所有图钉:

<bingMap:MapItemsControl ItemsSource="{Binding Locations}">
    <bingMap:MapItemsControl.ItemTemplate>
        <DataTemplate>
            <bingMap:Pushpin Location="{Binding objCoordinates}" Height="Auto" Width="Auto" PositionOrigin="BottomCenter" Template="{StaticResource CutomPushpinTemplate}">
            </bingMap:Pushpin>
        </DataTemplate>
    </bingMap:MapItemsControl.ItemTemplate>

这样做会快得多。性能分析器显示程序现在不会花费太多时间来实例化自定义图钉。

这是 xaml 代码,它显示了它之前是如何实现的(慢):

<ControlTemplate x:Key="PushpinControlTemplateLoc" TargetType="bingMap:Pushpin" >
    <Test.MyPushpin />
</ControlTemplate>
于 2012-10-22T12:39:56.730 回答