如何将Bing Maps组件添加到C#Winforms应用程序? [英] How can I add a Bing Maps Component to my C# Winforms app?

查看:47
本文介绍了如何将Bing Maps组件添加到C#Winforms应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Winforms应用程序中下载,安装并引用了Bing Maps控件:

...并且我在Form1.cs中添加了以下内容:

 使用Microsoft.Maps.MapControl; 

...但是我的工具箱中仍然没有Bing Maps控件.

我意识到这是一个WPF控件,并且我正在使用Winforms,但是我的理解是WPF控件可以在Winforms应用程序中使用.

我的问题是:如何在工具箱中显示Bing Maps控件或如何在代码中创建Bing Maps控件?

可能没关系,但是地图控件的运行时版本为4.0.30319,版本= 1.0.0.0

解决方案

WPF控件不会添加到Windows Forms工具箱中,而是需要在Windows Forms项目中创建WPF UserControl并为其添加WPF Map,然后在窗体上放置ElementHost Windows窗体控件的实例,并告诉主机显示WPF UserControl.

示例-Windows窗体中的WPF Bing Maps控件

  1. 创建Windows窗体应用程序.

  2. 安装

    更多信息:

    您可能希望查看以下链接以获取更多信息:

WPF controls won't be added to Windows Forms toolbox, instead you need to create a WPF UserControl inside your Windows Forms project and add a WPF Map to that, then drop an instance of ElementHost Windows Forms control on your Form and tell the host to show your WPF UserControl.

Example - WPF Bing Maps Control in Windows Forms

  1. Create a Windows Forms Application.

  2. Install Microsoft.Maps.MapControl.WPF NuGet package.

  3. Add a new WPF UserControl to your project:

    Right-click on project → choose Add New Item → Add a new User Control (WPF) (It's located under WPF category)

  4. Add the Map to your WPF user control. Basically you need to add the map assembly to the control by adding the following attribute to your user control:

    xmlns:m="clr-namespace:Microsoft.Maps.MapControl.WPF;assembly=Microsoft.Maps.MapControl.WPF"
    

    And adding the map element to the control:

    <Grid>
      <m:Map/>
    </Grid>
    

    Here is the full code for your user control. (Make sure you use the correct namespace):

    <UserControl x:Class="YOURNAMESPACE.UserControl1"
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       xmlns:m="clr-namespace:Microsoft.Maps.MapControl.WPF;assembly=Microsoft.Maps.MapControl.WPF">
       <Grid>
           <m:Map CredentialsProvider="YOUR MAP KEY" x:Name="myMap" />
       </Grid>
    </UserControl >
    

    • Note: You need a Bing Maps key. If you don't have a Bing maps account, create a Bing Maps account and then get a Bing Maps key from it. For test purpose, you can ignore the map key.

  5. Build your project.

  6. Drop an instance of ElementHost control on your form. You can find it under the WPF Interoperability group in toolbox.

  7. From the smart action panel (at top right of the ElementHost control), set the hosted content to UserControl1 and dock it to the parent container.

  8. Run your application.

There you go, The map will appear in your Windows Forms Application:

More information:

You may want to take a look at the following links for more information:

这篇关于如何将Bing Maps组件添加到C#Winforms应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆