使用Bing Maps Control的WPF应用程序在启动时会很慢

时间:2011-12-10 15:33:55

标签: wpf performance bing

我目前正在使用具有映射支持的WPF应用程序。我使用Bing Maps WPF Control(从这里:http://www.microsoft.com/download/en/details.aspx?id=27165)来帮助进行映射,但现在有一个非常大的问题:

应用程序需要相当长的时间才能启动,因为Bing Maps Control会检索所有初始数据以显示地图。

我的应用程序的映射部分很少需要,因此对于每次都没有使用的功能进行慢启动会非常糟糕所以我最初将控件的可见性设置为“Collapsed”,希望然后没有要求,但这没有帮助。

有什么方法可以在我想要使用它时显式初始化Bing Maps控件,而不是在应用程序启动时?

1 个答案:

答案 0 :(得分:0)

我最近开始使用Bing地图控件,也遇到了同样的问题。它并不优雅,但我设法通过在需要时手动创建Map控件来解决它。

在我的情况下,地图是我的应用程序的主要焦点,所以我需要几乎立即加载它。由于我希望应用程序本身能够立即完全呈现(因此用户知道发生了什么),因此我推迟创建Map控件,直到内容在MainWindow中呈现。您可以使用以下内容实现此目的:

<强> XAML

<Grid x:Name="MapPanel">

    <!-- Placeholder text while the map is loading -->
    <TextBlock HorizontalAlignment="Center"
               VerticalAlignment="Center"
               Text="Loading map..." />
</Grid>

<强>代码隐藏

protected override void OnContentRendered(EventArgs e)
{
    base.OnContentRendered(e);

    // Change the cursor to a waiting cursor so the user knows we are loading something
    var previousCursor = Cursor;
    Cursor = Cursors.Wait;

    // Load the application Id credentials required for the Bing map
    var provider = new ApplicationIdCredentialsProvider(Properties.Resources.BingMapsAPIKey);

    // Set up the Bing map control
    var map = new Map();
    map.Mode = new AerialMode(labels: true);
    map.CredentialsProvider = provider;
    map.HorizontalAlignment = HorizontalAlignment.Stretch;
    map.VerticalAlignment = VerticalAlignment.Stretch;

    // Render the map control over the top of the loading text in the map panel
    MapPanel.Children.Add(map);

    // Reset the application cursor
    Cursor = previousCursor;
}

对于您的方案,此时您不一定需要加载Map控件。相反,您可以延迟加载控件,以便在需要映射时基本上通知主机控件,如果尚未加载,则在该点加载它。

希望有所帮助。

相关问题