[英]How can I add a Bing Maps Component to my C# Winforms app?
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 用戶控件。
創建 Windows Forms 應用程序。
安裝Microsoft.Maps.MapControl.WPF NuGet package。
將新的WPF UserControl添加到您的項目中:
右鍵單擊項目→選擇添加新項目→添加新用戶控件(WPF) (它位於 WPF 類別下)
將 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 密鑰。
構建您的項目。
將 ElementHost 控件的實例拖放到表單上。 您可以在工具箱的 WPF 互操作性組下找到它。
在智能操作面板(位於 ElementHost 控件的右上角)中,將托管內容設置為UserControl1
並將其停靠到父容器。
運行您的應用程序。
go,map 將出現在您的 Windows Z6450242531912981C36683CAE88A 中:
更多信息:
您可能需要查看以下鏈接以獲取更多信息:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.