c# - 在 map 上作为图钉的图像 - Windows Phone 8

标签 c# xaml windows-phone-8 windows-phone here-api

我正在尝试将图像添加到 Windows Phone 8 map ,以用作图钉

我的 XAML 中有以下代码

<phone:PhoneApplicationPage
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:maps="clr-namespace:Microsoft.Phone.Maps.Controls;assembly=Microsoft.Phone.Maps"
x:Class="Cartuxa.Contactos"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d"
shell:SystemTray.IsVisible="True">

<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot">
    <Grid.Background>
        <ImageBrush ImageSource="/Assets/Images/bkContacts@2x.png"/>
    </Grid.Background>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel Grid.Row="0" Margin="12,17,0,28"/>

    <Grid x:Name="ContentPanel" Grid.RowSpan="2" Margin="0,-6,0,6">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="127*"/>
            <ColumnDefinition Width="329*"/>
        </Grid.ColumnDefinitions>
        <Image HorizontalAlignment="Left" Margin="18.75,57.5,0,0" VerticalAlignment="Top" Source="/Assets/Images/logo@2x.png" Stretch="Fill" Grid.ColumnSpan="2" Width="221" Height="72"/>
        <TextBlock HorizontalAlignment="Left" Margin="30,152,0,0" TextWrapping="Wrap" Text="Contactos" VerticalAlignment="Top" Foreground="#FFCD3333" FontFamily="Helvetica" FontSize="36" Grid.ColumnSpan="2" Width="174"/>
        <Button HorizontalAlignment="Right" Margin="0,201,-13,0" VerticalAlignment="Top" Width="255" Background="White" Height="66" BorderThickness="0" Grid.Column="1" BorderBrush="#FFFF0909">
            <Grid Grid.ColumnSpan="2">
                <Image Name="POI_Left" Source="/Assets/Images/POI_Gray@2x.png" RenderTransformOrigin="0.4,0.274" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="-180,0,0,0" Stretch="None"/>

                <TextBlock HorizontalAlignment="Left"
               Margin="13,6,-22,0"
               TextWrapping="Wrap"
               Text="Paços dos Condes de Basto"
               VerticalAlignment="Top"
               FontFamily="Helvetica"
               Foreground="Black"
               FontSize="14" Width="182"/>
            </Grid>
        </Button>
        <maps:Map Name="contactosMaps" HorizontalAlignment="Left" Margin="0,271,0,0" VerticalAlignment="Top" RenderTransformOrigin="-1.737,-0.175" Grid.ColumnSpan="2" Height="290" Width="480"/>
        <Button  Name="Contactos_Btn1"
                 Click="Contactos_Btn1_Click"
                 HorizontalAlignment="Right"
                 Margin="-17,201,242,0"
                 VerticalAlignment="Top"
                 Width="255"
                 Background="White"
                 Height="65"
                 BorderThickness="0"
                 Grid.ColumnSpan="2">
            <Grid Name="pushRed" Grid.ColumnSpan="2">
                <Image x:Name="POI_Right" Source="/Assets/Images/POI_Red@2x.png" RenderTransformOrigin="0.4,0.274" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="-190,0,0,0" Stretch="None"/>
                <TextBlock HorizontalAlignment="Left" Margin="14,6,-14,0" TextWrapping="Wrap" Text="Fundação Eugénio De Almeida" VerticalAlignment="Top" FontFamily="Helvetica" Foreground="Black" FontSize="14"/>
            </Grid>
        </Button>
        <TextBlock HorizontalAlignment="Left" Margin="26,606,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Grid.ColumnSpan="2" Width="406" Height="22">
            <Run FontFamily="Helvetica" Text="Fundação Eugénio de Almeida"/>
            <LineBreak/>
            <Run/>
        </TextBlock>
        <TextBlock HorizontalAlignment="Left" Margin="30,655,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Grid.ColumnSpan="2" Width="402" Height="85" FontFamily="Helvetica">
            <Run Text="Páteo de São Miguel "/>
            <LineBreak/>
            <Run Text="Apartado 2001, 7001-901 Évora "/>
            <LineBreak/>
            <Run Text="Portugal"/>
            <LineBreak/>
            <Run/>
        </TextBlock>
        <TextBlock HorizontalAlignment="Left" Margin="30,745,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Grid.ColumnSpan="2" Width="406" Height="22" Text="Tel. (+351) 266 748 300 "/>
        <TextBlock HorizontalAlignment="Left" Margin="30,773,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Grid.ColumnSpan="2" Width="406" Height="22">
            <Run FontFamily="Helvetica" Text="Fax. (+ 351) 266 705 149"/>
            <Run FontFamily="Helvetica"/>
            <Run/>
        </TextBlock>

    </Grid>
</Grid>

我的 .cs 代码如下

public partial class Contactos : PhoneApplicationPage
{
    public Contactos()
    {
        InitializeComponent();
        Loaded +=Contactos_Loaded;

    }

    private void Contactos_Loaded(object sender, RoutedEventArgs e)
    {
        UpdateMap();
    }

    private void UpdateMap()
    {

        MapOverlay pin = new MapOverlay();
        pin.Content = pushRed;
        pin.GeoCoordinate = new GeoCoordinate(38.57325D, -7.90546);
        pin.PositionOrigin = new Point(0, 0.5);

        MapLayer pinLayer = new MapLayer();
        pinLayer.Add(pin);
        contactosMaps.Layers.Add(pinLayer);
        contactosMaps.SetView(new GeoCoordinate(38.57325D, -7.90546), 14D);
    }

    private void Contactos_Btn1_Click(object sender, RoutedEventArgs e)
    {

    }
}

当我尝试运行我的项目时,它加载了 map ,但片刻之后我得到了这个异常

“附加信息:值不在预期范围内。”

当我执行“contactosMaps.Layers.Add(pinLayer);”时

我不能将带有图像的网格作为图钉吗?

最佳答案

按照此处所述使用 Phone.Controls.Toolkit:-

http://wp.qmatteoq.com/maps-in-windows-phone-8-and-phone-toolkit-a-winning-team-part-1/

工具包可以在以下任一位置找到

http://phone.codeplex.com/

https://www.nuget.org/packages/WPtoolkit

然后您可以直接在 XAML 中添加图像,如下所示:-

<maps:Map Loaded="MapControl_Loaded" ZoomLevel="18" Name="MapControl">
   <toolkit:MapExtensions.Children>
       <toolkit:Pushpin x:Name="MyPushpin">
           <toolkit:Pushpin.Template>
               <ControlTemplate TargetType="toolkit:Pushpin">
                   <StackPanel>
                       <Image Source="/Images/MapScreen/MapScreenCurrentLocationPin.png" Stretch="Uniform" Width="50" Height="50" HorizontalAlignment="Center"/>
                    </StackPanel>
                </ControlTemplate>
            </toolkit:Pushpin.Template>
        </toolkit:Pushpin>
    </toolkit:MapExtensions.Children>
</maps:Map>

或者您可以按如下方式在 C# 中添加它:-

MapOverlay overlay = new MapOverlay
{
    GeoCoordinate = myMap.Center,
    Content = new Ellipse
    {
        Fill = new SolidColorBrush(Colors.Red),
        Width = 40,
        Height = 40
     }
};
MapLayer layer = new MapLayer();
layer.Add(overlay);

myMap.Layers.Add(layer);

您应该能够添加带有图像的网格,而不是如上所示的椭圆。

如果这对您有用,请告诉我。

关于c# - 在 map 上作为图钉的图像 - Windows Phone 8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21295870/

相关文章:

c# - 在 LINQ 查询中添加 Lambda 以替换 foreach 中的一行

c# - 相同的 XAML,但用户控件背后的代码不同

c# - 将十进制(用户字符串)转换为带有 0x 前缀的十六进制

c# - 在 RaisePropertyChangedEvent 之后 ListView 不更新

c# - 通过 XAML 将图像设置为按钮背景

c# - Windows Phone 8 HttpClient Get 方法返回奇怪的结果

c# - 无法在 WP 上调用必应翻译服务

c# - 使用 Caliburn.Micro 附加到多个事件

c# - 正在寻找 C# TDD 框架?

c# - 基于通用对象属性的 WPF UserControl