【问题标题】:How to update custom rendered maps?如何更新自定义渲染的地图?
【发布时间】:2015-08-10 18:01:52
【问题描述】:

我在我的共享项目中创建了一个 CustomMap,从 Xamarin.Forms.Maps.Map 扩展。

我还为 iOS 和 Android 创建了自定义渲染器。问题是:我正在尝试更新与目的地相关的用户位置,因此我需要使用来自 Google 的新折线更新该地图。但我不知道怎么做。

我曾想过在我的 CustomMap 类中创建一个方法以在我的渲染器中被覆盖,但渲染器只使用我的 CustomMap 类作为 ExportRenderer 类型,该类本身派生自 MapRenderer。

如何从我的 CustomMap 类控制此更新?从我的共享项目代码中我可以这样做CustomMap.Update(new Polylines)

项目结构:

  • 解决方案
    • 共享项目
      • CustomMap:Xamarin.Forms.Maps.Map
    • iOS 项目
      • CustomMapRenderer:Xamarin.Forms.Maps.iOS.MapRenderer
    • 安卓项目
      • CustomMapRenderer:Xamarin.Forms.Maps.Android.MapRenderer

【问题讨论】:

    标签: xamarin xamarin.forms


    【解决方案1】:

    在 CustomMap.cs 中:

    public static BindableProperty TestProperty = BindableProperty.Create(
        propertyName: "Test",
        returnType: typeof(string),
        declaringType: typeof(CustomMap),
        defaultValue: "test",
        defaultBindingMode: BindingMode.OneWay,
        propertyChanged: HandleTestChanged);
    
    public string Test
    {
        get { return (string)GetValue(TestProperty); }
        set { SetValue(TestProperty, value); }
    }
    
    private static void HandleTestChanged(BindableObject bindable, object oldValue, object newValue)
    {
        var customMap = (CustomMap)bindable;
        // do something with new value
    }
    

    然后在你的渲染器中:

    var customMap = (CustomMap)Element;
    var test = customMap.Test;
    

    【讨论】:

    • 感谢您的回复。我需要调用一个方法,例如 Update,然后该调用将在我的渲染器中触发对路线的重绘。我可以对 Bindable 属性使用相同的方法吗?谢谢!
    • 您可以为此使用 Bindable 属性,或者只创建一个使用事件处理程序的方法(在渲染器构造函数中订阅它们)。
    • 嗨,我一直在努力让它工作但没有太大成功。我在我的 CustomMap 类中创建了该属性,但是如果该属性已更改,我如何签入我的渲染器?谢谢。
    猜你喜欢
    • 1970-01-01
    • 2019-01-14
    • 2015-12-11
    • 2013-09-21
    • 2022-01-02
    • 2020-10-28
    • 1970-01-01
    • 2011-03-20
    • 1970-01-01
    相关资源
    最近更新 更多