在VB winform应用程序中创建自定义图钉 [英] Create custom pushpin in VB winform appliction

查看:78
本文介绍了在VB winform应用程序中创建自定义图钉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将Bing Map实现添加到用VB编写的winforms应用程序中。



我尝试过:



使用WPF互操作性控件并不难。一旦我创建了WPF地图控件,我就能将它导入到我的项目中,并且工作正常。

我的问题是只有一种默认的图钉样式。我本来期待他们的全部......我很傻。

我的新要求是有第二个图钉样式,但我找到的所有示例都显示了如何在XAML中执行此操作WPF,或在网页上。

以下是在winforms应用程序中在WPF控件上插入图钉的当前代码



私有Sub AddPin(PinName as String,Lat as Double,Lon as Double)
Dim Pin as New Microsoft.Maps.MapControl.WPF.Pushpin
Pin.Location = New Microsoft.Maps.MapControl.WPF.Location (Lat,Lon)
Pin.ToolTip = PinName
Pin.Name = PinName
MyBingMapUC.Map.Center = Pin.Location
MyBingMapUC.Map.ZoomLevel = STATE_LEVEL'5
AddHandler Pin.MouseDown,AddressOf PushpinClick
BingMapUC.Map.Children.Add(Pin)
End Sub



 Private Sub PushpinClick(sender As Object,e As Windows.RoutedEventArgs)
e.handled = True
MsgBox(pin& DirectCast(sender,Microsoft.Maps.MapControl.WPF.Pushpin) ).Name&点击了!)
结束子





以上添加了默认图钉。我找不到任何方法来使用图钉类中的属性或方法来更改图钉。有一个图钉模板属性。如果这是某种XAML模板,则没有关于如何创建它的线索,或者将其合并到我的WinForms应用程序中。



我真的很感激它如果有人能指出我正确的方向...

解决方案

Google有一些很好的答案: wpf bing maps pushpin - Google搜索 [ ^ ]



喜欢这个: WPF上的Bing地图和自定义PushPin教程PixelSense - Dennis van der Stelt [ ^ ]


谢谢格雷姆 - 我确实遇到了那个 - 但决定慢慢地,刻意地贯穿它的每一步。在这样做的过程中,我发现了如何做我需要的东西 - 所以,虽然这更像是完全在WPF中工作,但我能够插入我需要的东西。所以对于其他想要在WinForms应用程序中的WPF控件上启用自定义图钉的人 - 这就是它的完成方式。

在你的WPF用户控件中,你需要添加一个名为 <的部分/i><usercontrol.resources> 如下

< UserControl.Resources> 
< ControlTemplate x:Key =yourtemplatenamehereTargetType =m:Pushpin>
....这里的模板构造函数
< / ControlTemplate>
< /UserControl.Resources>



将此秒放在< grid>之上部分。



现在这个例子在ControlTemplate中定义了一个相当难看的图钉,所以我对它进行了改进(通过大量的实验)。所以我的整个ControlTemplate看起来像这样

< UserControl.Resources> 
< ControlTemplate x:Key =MyNewPushpinTargetType =m:Pushpin>
< Grid x:Name =ContentGridHorizo​​ntalAlignment =CenterVerticalAlignment =Center>
< StackPanel>
<网格边距=0宽度=24高度=23>
< Rectangle Horizo​​ntalAlignment =LeftMargin =0,3.238,0,-2.146Width =23Height =18Fill =SteelBlueStroke =BlackRenderTransformOrigin =0.5,0.62 >
< Rectangle.RenderTransform>
< TransformGroup>
< ScaleTransform />
< SkewTransform AngleY =0AngleX = - 23/>
< RotateTransform Angle =123/>
< TranslateTransform />
< / TransformGroup>
< /Rectangle.RenderTransform>
< / Rectangle>
< Rectangle Fill ={TemplateBinding Background}Stroke =blackRadiusX =5RadiusY =5/>
< ContentPresenter Horizo​​ntalAlignment =CenterVerticalAlignment =CenterContent ={TemplateBinding Content}ContentTemplate ={TemplateBinding ContentTemplate}Margin =0TextBlock.FontFamily =Segoe UITextBlock.FontWeight = BoldTextBlock.Foreground =Black>
< / ContentPresenter>
< / Grid>
< / StackPanel>
< / Grid>
< / ControlTemplate>
< /UserControl.Resources>



所以现在的诀窍是弄清楚如何在winforms中访问这个模板......

好​​吧,UserControl通过使用 WPF互操作性控件托管在我的Winforms应用程序中。与添加对 WindowsFOrmsIntegration Microsoft.Maps.MapControl.WPF 的引用一样,它就像文本框,标签和面板一样,是工具箱中可用的控件之一。要获得那些你需要下载并安装从这里获得的WPF Map SDK ...

https://msdn.microsoft.com/en-us/library/hh750210.aspx?f=255&MSPPError=-2147217396

当您添加 WPF互操作性控件时,您有机会添加您构建的WPF控件。

您现在可以访问该控件中的模板关闭



 Dim NewPushpin As System.Windows.Controls.ControlTemplate = BingMapUC.FindResource(MyNewPushpin)





然后用代码实例化并展示你的图钉



 Dim Pin作为新图钉
Pin.Location =新位置(纬度,经度)
Pin.Content =a pin
Pin.Name =Pin_& Counter
Pin.Template = NewPushpin
Pin.Background = Windows.Media.Brushes.YellowGreen
MyBingMapUC.Map.Center = Pin.Location
MyBingMapUC.Map.ZoomLevel = STATE_VIEW
MyBingMapUC.Map.Children.Add(Pin)



在我的模板中,我公开了Pushpin背景,以便根据需要进行更改。这给了我一些我需要的灵活性。



如果你不知道如何将一个WPF用户控件组合在一个winform上,那么这里有一个不错的教程: https://blogs.msdn.microsoft.com/rbrundritt/2013 / 11/08 /使用冰 - 映射合的WinForms /


I needed to add a Bing Map implementation to a winforms application written in VB.

What I have tried:

It wasn't that hard using the WPF Interoperability control. Once I created the WPF map control I was able to import it into my project and it works fine.
My problem is that there is only one default pushpin style. I would have expected a panoply of them... Silly me.
My new requirement is to have a second pushpin style, but all of the examples I've located show how to do it in XAML in WPF, or on a web page.
Here is the current code for inserting a pushpin on the WPF control in a winforms application

Private Sub AddPin(PinName as String, Lat as Double, Lon as Double)
  Dim Pin as New Microsoft.Maps.MapControl.WPF.Pushpin
  Pin.Location = New Microsoft.Maps.MapControl.WPF.Location(Lat, Lon)
  Pin.ToolTip = PinName
  Pin.Name = PinName
  MyBingMapUC.Map.Center = Pin.Location
  MyBingMapUC.Map.ZoomLevel = STATE_LEVEL '5
  AddHandler Pin.MouseDown, AddressOf PushpinClick
  BingMapUC.Map.Children.Add(Pin)
End Sub


Private Sub PushpinClick(sender As Object, e As Windows.RoutedEventArgs)
  e.handled = True
  MsgBox("pin " & DirectCast(sender, Microsoft.Maps.MapControl.WPF.Pushpin).Name & " was clicked!")
End sub



The above adds the default pushpin. I cannot find any way to change the pushpin using the properties or methods within the pushpin class. There is a pushpin template property. If that is to be some sort of XAML template there are no clues as to how to create it, or incorporate it into my WinForms application.

I would really appreciate it if someone could point me in the right direction...

解决方案

Google has some great answers: wpf bing maps pushpin - Google Search[^]

Like this one: Bing Maps on WPF and custom PushPin tutorial for PixelSense - Dennis van der Stelt[^]


Thanks Graeme - I did run across that one - but decided to run through each step of it slowly and deliberately. In doing so I found out how to do what I needed - so, although this was still more about working entirely within WPF, I was able to "interpolate" what I needed. So for anyone else wanting to enable custom pushpins on a WPF control in a WinForms application - here's how it's done.
In your WPF usercontrol you will need to add a section called <usercontrol.resources> as follows

<UserControl.Resources>
   <ControlTemplate x:Key="yourtemplatenamehere" TargetType="m:Pushpin">
   .... the template constructors here
   </ControlTemplate>
</UserControl.Resources>


Put this second just above the <grid> section.

Now the example had a fairly ugly pushpin defined in the ControlTemplate, so I improved on it (with a lot of experimenting). So my entire ControlTemplate looks like this

<UserControl.Resources >
  <ControlTemplate x:Key="MyNewPushpin" TargetType="m:Pushpin">
    <Grid x:Name="ContentGrid" HorizontalAlignment="Center" VerticalAlignment="Center">
     <StackPanel>
       <Grid Margin="0" Width="24" Height="23">
        <Rectangle HorizontalAlignment="Left"  Margin="0,3.238,0,-2.146" Width="23" Height="18" Fill="SteelBlue" Stroke="Black"  RenderTransformOrigin="0.5,0.62">
         <Rectangle.RenderTransform>
          <TransformGroup>
            <ScaleTransform/>
            <SkewTransform AngleY="0" AngleX="-23"/>
            <RotateTransform Angle="123"/>
            <TranslateTransform/>
           </TransformGroup>
          </Rectangle.RenderTransform>
         </Rectangle>
         <Rectangle Fill="{TemplateBinding Background}" Stroke="black" RadiusX="5" RadiusY="5"/>
           <ContentPresenter HorizontalAlignment="Center"                                                                VerticalAlignment="Center"                                                                Content="{TemplateBinding Content}"                                                                ContentTemplate="{TemplateBinding ContentTemplate}"                                                                Margin="0" TextBlock.FontFamily="Segoe UI" TextBlock.FontWeight="Bold" TextBlock.Foreground="Black" >
           </ContentPresenter>
     </Grid>
    </StackPanel>
   </Grid>
 </ControlTemplate>
</UserControl.Resources>


So now the trick is to figure out how to get access to this template in winforms...
Well, the UserControl is hosted in my Winforms application through the use of the WPF Interoperability control. It, like your textboxes and labels and panels is one of the controls available in you toolbox once you have added a reference to WindowsFOrmsIntegration and the Microsoft.Maps.MapControl.WPF. To get those you need to download and install the WPF Map SDK that you get from here...
https://msdn.microsoft.com/en-us/library/hh750210.aspx?f=255&MSPPError=-2147217396
When you add the WPF Interoperability control you get a chance to add the WPF control you've built.
You can now access the template within that control as follows

Dim NewPushpin As System.Windows.Controls.ControlTemplate = BingMapUC.FindResource("MyNewPushpin")



Then instantiate and present your pushpin(s) in code

Dim Pin As New Pushpin
Pin.Location = New Location(Latitude, Longitude)
Pin.Content = "a pin"
Pin.Name = "Pin_" & Counter
Pin.Template = NewPushpin
Pin.Background = Windows.Media.Brushes.YellowGreen
MyBingMapUC.Map.Center = Pin.Location
MyBingMapUC.Map.ZoomLevel = STATE_VIEW
MyBingMapUC.Map.Children.Add(Pin)


In my template I exposed the Pushpin background so it can be changed as necessary. This gave me some flexibility I needed.

If you don't know how to put together a WPF usercontrol for use on a winform there's a decent tutorial here: https://blogs.msdn.microsoft.com/rbrundritt/2013/11/08/using-bing-maps-in-winforms/


这篇关于在VB winform应用程序中创建自定义图钉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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