c# - 如何在 Windows Phone 7 中的 bing map 中从 ViewModel 设置经度和纬度

标签 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);
        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>

在这里,我想从 View 模型中给出图钉、经度、纬度。请告诉我如何实现这一目标?

提前致谢..

我已经尝试过这样的..

public class MapPageViewModel : ReactiveObject
{

    public static string _longitude;
    public string longitude
    {
        get { return _longitude; }
        set { this.RaiseAndSetIfChanged(x => x.longitude, value); }
    }

    public static string _latitude;
    public string latitude
    {
        get { return _latitude; }
        set { this.RaiseAndSetIfChanged(x => x.latitude, value); }
    }

    public ReactiveAsyncCommand setButton { get; set; }

    public MapPageViewModel()
    {
        setButton = new ReactiveAsyncCommand();
        setButton.Subscribe(x => {

            Pushpin p = new Pushpin();
            p.Background = new SolidColorBrush(Colors.Yellow);
            p.Foreground = new SolidColorBrush(Colors.Black);
            p.Location = new GeoCoordinate(double.Parse(longitude), double.Parse(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), double.Parse(latitude), 200), 9);
        });
    }


}

但我不知道如何设置map1.Children.Add()和map1.SetView以及如何在Map中绑定(bind)这些值?

嗨克莱门斯。我已经尝试过你的指导。但它显示错误。

Screen shot of error message

我也尝试过这个:-

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);
        });
    }

但是这里发生运行时错误。请让我知道我哪里做错了。

最佳答案

在正确的 MVVM 方法中,您通常会拥有一个带有用于图钉的 ItemTemplateMapItemsControl,并将其绑定(bind)到图钉的 ObservableCollection View 模型中的数据项:

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 = ...
            });
        });
    }
}

XAML:

<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>

关于c# - 如何在 Windows Phone 7 中的 bing map 中从 ViewModel 设置经度和纬度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22610214/

相关文章:

asp.net-mvc - Internet Explorer Mobile 和 MVC 应用程序的问题

c# - 常量枚举中的循环定义

c# - 为什么build action : None ignored for a *.是cs文件?

wpf - XAML:DataTemplate 中的自定义绑定(bind)用于 GridViewColumn CellTemplate

c# - 在 UWP 中检测空闲用户

windows-phone-7 - 将内容从 wp7 下载到电脑?

c# - 某些设备的 UWP 中的 XAML 梯度问题

c# - 在 EF Core 中,如何检查是否需要迁移?

WPF:如何以不同的方向书写文本?

c# - 从项目文件夹动态加载图像 - Windows Phone 7