I'm trying to set the lat/lng values for the item marked "<device:GeoCoordinate..."
dynamically. I can hard code this in the XAML no problem but when I attempt to set it on the fly I get a null pointer exception (inside the loaded handler shown below)
XAML
<maps:Map ZoomLevel="6" Mode="Aerial" Grid.Row="1" Grid.ColumnSpan="2">
<maps:Map.Center>
<device:GeoCoordinate x:Name="theLocation"/>
</maps:Map.Center>
<maps:MapLayer x:Name="TheMapLayer"/>
</maps:Map>
c#
void ViewBingMaps_Loaded(object sender, RoutedEventArgs e)
{
//get lat + lng for the device and set it dynamically
theLocation.Latitude = 41.5686949;
theLocation.Longitude = -93.7943157;
}
Is it possible to set this or am I misunderstanding the d开发者_StackOverflowevice:GeoCoordinate purpose?
Simply re-instantiate the GeoCoordinate
class in the code-behind and set the map center to it:
theLocation = new System.Device.Location.GeoCoordinate(10.0, 10.0);
map1.Center = theLocation;
Or alternatively, use a MVVM approach:
<maps:Map Center="{Binding Center}">
...
</maps:Map>
Which then binds to a GeoCoordinate
property named Center
.
public GeoCoordinate Center
{
get;
private set;
}
It can then be dynamically changed from your ViewModel (You do have a ViewModel, right?!)
Likewise you can bind the ZoomProperty (which tends to be rather handy), and pretty much everything else!
精彩评论