如何更新自定义渲染地图?
How to update custom rendered maps?
我在从 Xamarin.Forms.Maps.Map
.
扩展的共享项目中创建了一个 CustomMap
我还为 iOS 和 Android 创建了自定义渲染器。问题是:我正在尝试更新与目的地相关的用户位置,因此我需要使用来自 Google 的新折线更新该地图。但是我不知道怎么办。
我考虑过在我的 CustomMap class 中创建一个方法以在我的渲染器中被覆盖,但是渲染器只使用我的 CustomMap class 作为 ExportRenderer 类型, class 本身派生自 MapRenderer。
如何从我的 CustomMap class 控制此更新?通过我的共享项目代码,我可以这样做 CustomMap.Update(new Polylines)
项目结构:
- 解决方案
- 共享项目
- 自定义地图:Xamarin.Forms.Maps.Map
- iOS 项目
- 自定义地图渲染器:Xamarin.Forms.Maps.iOS.MapRenderer
- Android 项目
- 自定义地图渲染器:Xamarin.Forms.Maps.Android.MapRenderer
在 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;
我在从 Xamarin.Forms.Maps.Map
.
我还为 iOS 和 Android 创建了自定义渲染器。问题是:我正在尝试更新与目的地相关的用户位置,因此我需要使用来自 Google 的新折线更新该地图。但是我不知道怎么办。
我考虑过在我的 CustomMap class 中创建一个方法以在我的渲染器中被覆盖,但是渲染器只使用我的 CustomMap class 作为 ExportRenderer 类型, class 本身派生自 MapRenderer。
如何从我的 CustomMap class 控制此更新?通过我的共享项目代码,我可以这样做 CustomMap.Update(new Polylines)
项目结构:
- 解决方案
- 共享项目
- 自定义地图:Xamarin.Forms.Maps.Map
- iOS 项目
- 自定义地图渲染器:Xamarin.Forms.Maps.iOS.MapRenderer
- Android 项目
- 自定义地图渲染器:Xamarin.Forms.Maps.Android.MapRenderer
- 共享项目
在 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;