【问题标题】:WPF Application with Bing Maps Control painfully slow on startup带有 Bing 地图控件的 WPF 应用程序在启动时非常缓慢
【发布时间】:2011-12-10 15:33:55
【问题描述】:

我目前正在开发一个支持映射的 WPF 应用程序。我使用 Bing Maps WPF 控件(来自这里:http://www.microsoft.com/download/en/details.aspx?id=27165)来帮助映射,但现在有一个很大的问题:

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

我的应用程序的映射部分很少需要,因此对于一个甚至不是每次都使用的功能启动缓慢会很糟糕,所以我最初将控件的可见性设置为“已折叠”,希望那么就不会提出任何请求,但这无济于事。

有没有办法在我想使用 Bing 地图控件而不是在应用程序启动时显式初始化它?

【问题讨论】:

    标签: wpf performance bing


    【解决方案1】:

    我最近开始使用 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 控件。相反,您可以延迟加载控件,以便在需要地图时通知主机控件,如果尚未加载,则在该点加载它。

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-08
      • 2016-07-18
      • 2010-12-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多