无法设置图钉属性(WP8)

时间:2014-03-05 12:28:51

标签: c# windows-phone-8

我有Pushpin的地图:

<maps:Map ZoomLevel="18" Height="575" x:Name="map1" Width="415" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,0,-10,0">
    <toolkit1:MapExtensions.Children>
        <toolkit1:Pushpin x:Name="push1" GeoCoordinate="" Content=""  Background="Gray" ContentTemplate="{StaticResource Template_Content}">
        </toolkit1:Pushpin>
    </toolkit1:MapExtensions.Children>
</maps:Map>

我尝试从代码中设置属性:

map1.Center = currentObject.Coords;
push1.GeoCoordinate = currentObject.Coords;
push1.Content = currentObject.Name;

所以Map有效,但Pushpin会返回NullReference Exception。是什么原因以及如何解决?

1 个答案:

答案 0 :(得分:2)

map1.Center = currentObject.Coords;
Pushpin pushpin = (Pushpin)this.FindName("push1");
pushpin .GeoCoordinate = currentObject.Coords;
pushpin .Content = currentObject.Name;