2012-10-19 55 views
3

我對使用Bing地圖和自定義圖釘的.NET 4應用程序有問題。在縮放或移動地圖時,性能非常差。我正在使用一個ObservableCollection,它與Bing地圖有數據綁定。每次地圖更改時,都會檢查地圖部分中的哪些圖釘。該集合將被重新填充,最後一個NotifyCollectionChangedEvent被解僱,這使地圖繪製圖釘。Bing地圖在使用自定義圖釘時需要很多性能

<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> 

爲什麼我的自定義圖釘需要這麼多的性能嗎?

回答

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> 

此模板使用對於必應地圖這樣所有圖釘:

<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>