为什么Xamarin地图控件没有在ListView中显示正确的位置?

时间:2016-09-05 15:52:47

标签: google-maps xamarin xamarin.forms

我正在使用Xamarin.Forms.Maps中的地图控件。如果我将它作为ContentPage的子项,则地图将显示正确的位置,但是当我在DataTemplate中使用它时,它将被卡在世界地图中心的默认位置(在赤道处)。

<?xml version="1.0" encoding="utf-8" ?> 
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
             xmlns:local="clr-namespace:Observer" 
             xmlns:controls="clr-namespace:Observer.Controls;assembly=Observer" 
             xmlns:data="clr-namespace:Observer.Data;assembly=Observer" 
             x:Class="Observer.MainPage" Title="Reports"> 
    <AbsoluteLayout x:Name="AbsoluteLayout" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"> 
    </AbsoluteLayout> 
    <ListView x:Name="ReportListView" RowHeight="150"> 
        <ListView.ItemTemplate> 
            <DataTemplate> 
                <ViewCell> 
                    <Grid> 
                        <Grid.ColumnDefinitions> 
                            <ColumnDefinition Width="*"/> 
                            <ColumnDefinition Width="150"/> 
                        </Grid.ColumnDefinitions> 
                        <StackLayout Grid.Column="0" Padding="8" Spacing="2"> 
                            <Label Text="{Binding Path=Client.Reference}"/> 
                            <Label Text="{Binding Path=Client.FullName}"/> 
                            <Label Text="{Binding Path=Address1}"/> 
                            <Label Text="{Binding Path=Address2}"/> 
                            <Label Text="{Binding Path=City}"/> 
                        </StackLayout> 
                        <!-- this doesn't. --> 
                        <controls:MapEx Grid.Column="1" Coordinates="(40, -3)"> 
                            <controls:MapEx.Circle> 
                                <controls:CustomCircle Radius="1000"/> 
                            </controls:MapEx.Circle> 
                        </controls:MapEx> 
                    </Grid> 
                </ViewCell> 
            </DataTemplate> 
        </ListView.ItemTemplate> 
    </ListView> 
    <!-- this works. 
    <controls:MapEx Grid.Column="1" Coordinates="(40, -3)"/> 
    --> 
</ContentPage> 
public class MapEx : Map 
{ 
    public static readonly BindableProperty CoordinatesProperty = BindableProperty.Create(nameof(Coordinates), typeof(string), typeof(MapEx), "(55.9533, 3.1883)"); 
    public MapEx() 
    { 
    } 
    public CustomCircle Circle { get; set; } 
    public string Coordinates 
    { 
        get 
        { 
            return (string)GetValue(CoordinatesProperty); 
        } 
        set 
        { 
            SetValue(CoordinatesProperty, value); 
            OnPropertyChanged(); 
        } 
    } 
    protected override void OnPropertyChanged(string propertyName = null) 
    { 
        base.OnPropertyChanged(propertyName); 
        if (propertyName == nameof(Coordinates)) 
        { 
            var pos = new Position(54, -3); // placeholder 
            MoveToRegion(MapSpan.FromCenterAndRadius(pos, Distance.FromMiles(5))); 
        } 
    } 
} 

1 个答案:

答案 0 :(得分:0)

为AbsoluteLayout标记内的ListView标记加宽,以某种方式修复了地图。