如何在ListView内绑定Xamarin.forms.Maps的位置值? [英] How to bind the position value of Xamarin.forms.Maps inside the ListView?

查看:164
本文介绍了如何在ListView内绑定Xamarin.forms.Maps的位置值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Xamarin.forms.Maps 在我的项目中显示地图.地图位于 ListView 内部,我需要将坐标位置绑定到地图.

I am using Xamarin.forms.Maps for showing the map in my project. The map is inside the ListView and I need to bind the coordinate positions to the map.

模型类别:

public class History
{
    public double Latitude { get; set; }
    public double Longitude { get; set; }
}

数据添加部分:

historyList.Add(new History() { Latitude= -28.854930 ,Longitude= 151.166023 }); 
historyList.Add(new History() { Latitude = -28.853671, Longitude = 151.165712 }); 
historyList.Add(new History() { Latitude = -28.853934, Longitude = 151.167118 }); 
historyList.Add(new History() { Latitude = -28.855178, Longitude = 151.167946 }); 
historylistview.ItemsSource = historyList;

XAML

<ListView x:Name="historylistview">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <ViewCell.View>
                        <StackLayout
                             HorizontalOptions="Center">
                            <maps:Map 
                                x:Name="maps" 
                                VerticalOptions="Center" 
                                HorizontalOptions="Center">
                                <x:Arguments>
                                    <maps:MapSpan>
                                        <x:Arguments>
                                            <maps:Position>
                                                <x:Arguments>
                                                    <x:Double>{Binding Latitude}</x:Double>
                                                    <x:Double>{Binding Longitude}</x:Double>
                                                </x:Arguments>
                                            </maps:Position>
                                            <x:Double>0.01</x:Double>
                                            <x:Double>0.01</x:Double>
                                        </x:Arguments>
                                    </maps:MapSpan>
                                </x:Arguments>
                            </maps:Map>
                        </StackLayout>
                </ViewCell.View>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView> 

地图显示在每一行中,但位置值错误.我已经给出了德克萨斯州的位置,但是地图显示在Nijerya的某个地方.

The map is showing in each row but the position value are wrong. I have given location from Texas but the map is showing somewhere in Nijerya.

是否可以对 x:Arguments 类型进行绑定?

Is binding is possible to do for the type x:Arguments?

推荐答案

MapSpan Position 不可的可绑定属性,因此如果您使用数据绑定,将永远无法使用.

MapSpan and Position in Map are not bindable property , so it will never work if you use data binding .

如果要显示职位列表


<ListView x:Name="historylistview">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <ViewCell.View>
                        <StackLayout
                             HorizontalOptions="Center">
                             <maps:Map x:Name="map"
                  ItemsSource="{Binding positionList}">
            <maps:Map.ItemTemplate>
                <DataTemplate>
                    <maps:Pin Position="{Binding .}"/>
                </DataTemplate>
            </maps:Map.ItemTemplate>
        </maps:Map>
                        </StackLayout>
                </ViewCell.View>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView> 


historyList.Add(new History() { positionList = new ObservableCollection<Position>() { new Position(36.9628066, -122.0194722) },xxx="title here" });
historyList.Add(new History() { positionList = new ObservableCollection<Position>() { new Position(36.9628066, -122.0194722) }, xxx = "title here" });
//...
historylistview.ItemsSource = historyList;

public class History
    {
        public ObservableCollection<Position> positionList  { get; set; }
        
        //other property
        public string xxx { get; set; }

    }

检查文档了解更多详情

这篇关于如何在ListView内绑定Xamarin.forms.Maps的位置值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆