必应地图在使用自定义图钉时需要很高的性能。

3

我有一个使用Bing Maps自定义图钉的.NET 4应用程序的问题。缩放或移动地图时性能非常差。我正在使用ObservableCollection,它具有与Bing Maps的数据绑定。每当地图更改时,都会检查哪些图钉在地图部分中。集合将被重新填充,最后触发NotifyCollectionChangedEvent,使地图绘制图钉。

<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}"/>,速度更快。因此,我的用法或自定义图钉实现中一定有什么问题。
以下是我的其余代码:
自定义图钉类(仅为自动生成的代码):
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>

为什么我的自定义图钉需要如此强大的性能?
1个回答

2
问题解决:与其使用单独的类和xaml文件并引用它们,最好在包含Bing地图的类中使用ControlTemplate来自定义推针。请注意保留HTML标签。
<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>

今日免费次数已满, 请开通会员/明日再来
<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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接