【问题标题】:How to bind the position value of Xamarin.forms.Maps inside the ListView?如何在 ListView 中绑定 Xamarin.forms.Maps 的位置值?
【发布时间】:2021-03-31 02:09:30
【问题描述】:

我正在使用Xamarin.forms.Maps 在我的项目中显示地图。地图在ListView里面,我需要把坐标位置绑定到地图上。

模型类:

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 的某个地方。

x:Arguments类型可以绑定吗?

【问题讨论】:

    标签: xaml xamarin.forms data-binding xamarin.forms.maps


    【解决方案1】:
    Map中的

    MapSpanPosition可绑定属性,所以如果你使用数据绑定,它永远不会起作用。

    如果要显示职位列表

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

    查看docs了解更多详情

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-22
    • 2016-06-05
    • 2015-11-17
    • 1970-01-01
    • 1970-01-01
    • 2012-10-14
    相关资源
    最近更新 更多