簡體   English   中英

如何將 Bing 地圖組件添加到我的 C# Winforms 應用程序?

[英]How can I add a Bing Maps Component to my C# Winforms app?

我已經在我的 Winforms 應用程序中下載、安裝並引用了 Bing 地圖控件:

在此處輸入圖像描述

...我在 Form1.cs 中添加了以下內容:

using Microsoft.Maps.MapControl;

...但我的工具箱中仍然沒有 Bing 地圖控件。

我意識到這是一個 WPF 控件,我正在使用 Winforms,但我的理解是 WPF 控件可以在 Winforms 應用程序中使用。

我的問題是:如何使 Bing Maps 控件在工具箱中可見或在代碼中創建 Bing Maps 控件?

可能沒關系,但是map控件的Runtime版本是4.0.30319,version = 1.0.0.0

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顯示您的 WPF 用戶控件。

示例 - WPF Windows Forms 中的 Bing 地圖控件

  1. 創建 Windows Forms 應用程序。

  2. 安裝Microsoft.Maps.MapControl.WPF NuGet package。

  3. 將新的WPF UserControl添加到您的項目中:

    右鍵單擊項目→選擇添加新項目→添加新用戶控件(WPF) (它位於 WPF 類別下)

  4. 將 Map 添加到 WPF 用戶控件。 基本上,您需要通過將以下屬性添加到用戶控件來將 map 程序集添加到控件:

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

    並將 map 元素添加到控件中:

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

    這是您的用戶控件的完整代碼。 (確保使用正確的命名空間):

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

    • 注意:您需要一個必應地圖密鑰。 如果您沒有 Bing 地圖帳戶,請創建一個 Bing 地圖帳戶,然后從中獲取 Bing 地圖密鑰 出於測試目的,您可以忽略 map 密鑰。

  5. 構建您的項目。

  6. 將 ElementHost 控件的實例拖放到表單上。 您可以在工具箱的 WPF 互操作性組下找到它。

  7. 在智能操作面板(位於 ElementHost 控件的右上角)中,將托管內容設置為UserControl1並將其停靠到父容器。

  8. 運行您的應用程序。

go,map 將出現在您的 Windows Z6450242531912981C36683CAE88A 中:

在此處輸入圖像描述

更多信息:

您可能需要查看以下鏈接以獲取更多信息:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM