c# - 将构造函数参数绑定(bind)到 Xamarin.Forms 的 GoogleMaps

标签 c# xamarin mvvm xamarin.forms mvvmcross

我使用 MVVMCross 与 GoogleMaps 开发简单的 Xamarin.Forms 应用程序。我的目标是在启动 map 时将 map 的位置置于用户当前位置的中心。不幸的是我不知道如何将这些值绑定(bind)到 GoogleMaps 的构造函数。现在它只是静态值,但我想从 CurrentLocation 对象(具有这些属性)传递纬度和经度值。

查看:

<ContentPage.Content>
    <maps:Map MapType="Street" IsShowingUser="True" HasZoomEnabled="True">
        <x:Arguments>
            <maps:MapSpan>
                <x:Arguments>
                    <maps:Position>
                        <x:Arguments>
                            <x:Double>56.368533</x:Double>
                            <x:Double>3.258646</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>
</ContentPage.Content>

这是我的 ViewModel 的一部分:

public class NearbyBollardsMapViewModel : MvxViewModel
{
    private Location _currentLocation;

    public Location CurrentLocation
    {
        get => _currentLocation;
        set
        {
            _currentLocation = value;
            RaisePropertyChanged(() => CurrentLocation);
        }
    }

    public NearbyBollardsMapViewModel(ILocationService locationService)
    {
        this._locationService = locationService;
        CurrentLocation = _locationService.GetCurrentUsersLocation().Result;
    }
}

最佳答案

您需要创建一个从 Maps 扩展的 PCL 类并向其添加 BindableProperty。

1º - 创建一个类并从 Xamarin.Forms.Maps.Map 扩展它

(我包含了可绑定(bind)属性的代码,您可以阅读更多内容Here)

public class BindableMap : Xamarin.Forms.Maps.Map
{
    public BindableMap()
    {

    }

    public MapSpan MapSpan
    {
        get { return (MapSpan)GetValue(MapSpanProperty); }
        set { SetValue(MapSpanProperty, value); }
    }

    public static readonly BindableProperty MapSpanProperty = BindableProperty.Create(
                                                     propertyName: "MapSpan",
                                                     returnType: typeof(MapSpan),
                                                     declaringType: typeof(BindableMap),
                                                     defaultValue: null,
                                                     defaultBindingMode: BindingMode.TwoWay,
                                                     validateValue: null,
                                                     propertyChanged: MapSpanPropertyChanged);

    private static void MapSpanPropertyChanged(BindableObject bindable, object oldValue, object newValue)
    {
        try
        {
            var thisInstance = bindable as BindableMap;
            var newMapSpan = newValue as MapSpan;

            thisInstance?.MoveToRegion(newMapSpan);
        }
        catch (Exception ex)
        {

        }
    }
    
}

2° - 添加将用于在 ViewModel 中绑定(bind) MapSpan 的属性

private MapSpan _mapSpanView;
public MapSpan MapSpanView
{
    get { return _mapSpanView; }
    set { SetProperty(ref _mapSpanView, value); }
}

...

Position position = new Position((double)Latitude, (double)Longitude);
MapSpanView = MapSpan.FromCenterAndRadius(
                            prospectPosition,
                            Distance.FromMeters(2500)
                        );

3° - 然后将其绑定(bind)在xaml中

xmlns:map="clr-namespace:YourNameSpace.Mobile.TheFolderWhereTheClassIs"

...

<StackLayout x:Name="mapHolder">
    <map:BindableMap
                    x:Name="map"
                    MapSpan="{Binding MapSpanView}"
                    MapType="Street" IsShowingUser="True" HasZoomEnabled="True"/>
</StackLayout>

关于c# - 将构造函数参数绑定(bind)到 Xamarin.Forms 的 GoogleMaps,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60515196/

相关文章:

c# - 删除列表中除第一项以外的所有项

c# - 了解以下异步等待调用之间的区别

c# - 抛出异常时隐藏源错误(代码行)

c# - 在 Mvc4/C# 中实现 Google Analytics?

listview - Xamarin Forms - 获取在 Listview 中选择的项目的位置

ios - 项目无法在 iPhone 上运行,而在模拟器上运行良好

xaml - Xamarin Forms - 我的 ContentPresenter 不会显示其内容

wpf - 将鼠标移出控制范围时禁用按钮

wpf - 使用 WPF 和 MVVM 处理异常

wpf mvvm 自定义控件 - 在控件上调用操作