C# 如何在windows phone 7的bing地图中从ViewModel设置经度和纬度
我想给出ViewModel中的经度和纬度 现在我使用的是:-C# 如何在windows phone 7的bing地图中从ViewModel设置经度和纬度,c#,xaml,windows-phone-7,bing-maps,C#,Xaml,Windows Phone 7,Bing Maps,我想给出ViewModel中的经度和纬度 现在我使用的是:- private void button1_Click(object sender, RoutedEventArgs e) { Pushpin p = new Pushpin(); p.Background = new SolidColorBrush(Colors.Yellow); p.Foreground = new SolidColorBrush(Colors.Black);
private void button1_Click(object sender, RoutedEventArgs e)
{
Pushpin p = new Pushpin();
p.Background = new SolidColorBrush(Colors.Yellow);
p.Foreground = new SolidColorBrush(Colors.Black);
p.Location = new GeoCoordinate(double.Parse(longitude.Text), double.Parse(latitude.Text));//Longitude and Latitude
p.Content = "I'm here";//To show the place where it is located
map1.Children.Add(p);
map1.SetView(new GeoCoordinate(double.Parse(longitude.Text), double.Parse(latitude.Text), 200), 9);
}
我的Xaml是:-
<Grid x:Name="MapPageUIContainer" Grid.Row="1" Margin="2,0,2,0">
<my:Map CopyrightVisibility="Collapsed" LogoVisibility="Collapsed" CredentialsProvider="" Mode="AerialWithLabels" Height="543" HorizontalAlignment="Left" Name="map1" VerticalAlignment="Top" Width="480" Margin="2,100,0,0" />
<Border BorderBrush="Silver" BorderThickness="1" Height="100" HorizontalAlignment="Left" Margin="0,0,0,0" Name="border1" VerticalAlignment="Top" Width="476" Background="#FFA3A371">
<TextBlock Text="Map Example" HorizontalAlignment="Center" FontSize="32" FontWeight="Bold" VerticalAlignment="Center" />
</Border>
<TextBox Height="72" HorizontalAlignment="Left" Margin="6,627,0,0" Name="longitude" Text="" VerticalAlignment="Top" Width="200" />
<TextBox Height="72" HorizontalAlignment="Left" Margin="260,627,0,0" Name="latitude" Text="" VerticalAlignment="Top" Width="200" />
<Button Content="Set" HorizontalAlignment="Left" Margin="190,690,0,0" Name="button1" VerticalAlignment="Top" Click="button1_Click" />
</Grid>
但我不知道如何设置map1.Children.Add()和map1.SetView,以及如何在Map中绑定这些值
嗨,克莱门斯。我已经试过你的指示了。但它显示出错误
我也尝试过这个:-
public MapPageViewModel()
{
PushpinItems = new ObservableCollection<PushpinItem>();
PushpinItem pp = new PushpinItem();
setButton = new ReactiveAsyncCommand();
setButton.Subscribe(x => {
pp.Location = new GeoCoordinate(double.Parse(longitude), double.Parse(latitude));
pp.Text = "I'm here";
PushpinItems.Add(pp);
});
}
publicMapPageViewModel()
{
PushpinItems=新的ObservableCollection();
PushpinItem pp=新的PushpinItem();
setButton=new ReactiveAsyncCommand();
setButton.Subscribe(x=>{
pp.Location=新地理坐标(double.Parse(经度),double.Parse(纬度));
pp.Text=“我在这里”;
PushpinItems.Add(pp);
});
}
但是这里发生了运行时错误。请让我知道我犯了什么错误。在正确的MVVM方法中,您通常会为图钉使用带有
项模板的MapItemsControl
,并将其绑定到视图模型中图钉数据项的可观察集合
:
public class PushpinItem
{
public GeoCoordinate Location { get; set; }
public string Text { get; set; }
}
public class MapPageViewModel : ReactiveObject
{
public ObservableCollection<PushpinItem> PushpinItems { get; set; }
...
public MapPageViewModel()
{
PushpinItems = new ObservableCollection<PushpinItem>();
setButton = new ReactiveAsyncCommand();
setButton.Subscribe(x =>
{
PushpinItems.Add(new PushpinItem
{
Location = new GeoCoordinate(...),
Text = ...
});
});
}
}
公共类PushpinItem
{
公共地理坐标位置{get;set;}
公共字符串文本{get;set;}
}
公共类MapPageViewModel:反应对象
{
公共ObservableCollection PushpinItems{get;set;}
...
公共MapPageViewModel()
{
PushpinItems=新的ObservableCollection();
setButton=new ReactiveAsyncCommand();
setButton.Subscribe(x=>
{
PushpinItems。添加(新的PushpinItem)
{
位置=新的地理坐标(…),
文本=。。。
});
});
}
}
XAML:
@克莱门斯和这个链接对我很有帮助。。!!谢谢他们两个 嗨,克莱门斯。我已经听从你的指示了。但它向我显示了错误消息。随信附上屏幕截图。请让我知道我犯了什么错误?对不起,我的错。必须有逗号而不是分号。请注意,这只是编写代码的一种可能方式。您也可以先创建PushpinItem对象,然后设置位置和文本属性,最后将其添加到PushpinItems列表中。出于某些原因,StackOverflow不允许我编辑自己的答案。我稍后再试。我已经用逗号试过了。但在运行时引发了错误。你能把编辑过的代码发给我的邮件吗?如果可能的话,请寄给我。vijaydhascr@gmail.comJust使用更新问题中显示的另一种方法,在订阅处理程序中创建新的PushpinItem对象。在第二种方法中,还必须在订阅处理程序中创建新的PushpinItem对象。
public class PushpinItem
{
public GeoCoordinate Location { get; set; }
public string Text { get; set; }
}
public class MapPageViewModel : ReactiveObject
{
public ObservableCollection<PushpinItem> PushpinItems { get; set; }
...
public MapPageViewModel()
{
PushpinItems = new ObservableCollection<PushpinItem>();
setButton = new ReactiveAsyncCommand();
setButton.Subscribe(x =>
{
PushpinItems.Add(new PushpinItem
{
Location = new GeoCoordinate(...),
Text = ...
});
});
}
}
<map:Map ...>
<map:MapItemsControl ItemsSource="{Binding PushpinItems}">
<map:MapItemsControl.ItemTemplate>
<DataTemplate>
<map:Pushpin Location="{Binding Location}" Content="{Binding Text}"
Background="Yellow" Foreground="Black"/>
</DataTemplate>
</map:MapItemsControl.ItemTemplate>
</map:MapItemsControl>
</map:Map>