xamarin OnElementChanged(...)未获取属性的新值

lsmepo6l  于 2023-05-11  发布在  其他
关注(0)|答案(1)|浏览(160)

我有一张Map,显示了点之间的路线.这是工作,但现在我想通过一些颜色值从PCL。我有一个自定义的渲染器为Android和一个类之间的桥梁PCL和Android与绑定的属性。(对于Map,我使用PCL实现,并使用自定义渲染器对其进行扩展)
现在我有:
(android:CustomMapRenderer)

[assembly: ExportRenderer(typeof(CustomMap), typeof(MapOverlay.Droid.CustomMapRenderer))]
namespace MapOverlay.Droid
{
public class CustomMapRenderer : MapRenderer , ICustomMap
{
    List<Position> routeCoordinates;
    Int32 RouteColor;

    protected override void OnElementChanged(Xamarin.Forms.Platform.Android.ElementChangedEventArgs<Map> e)
    {
        base.OnElementChanged(e);

        if (e.OldElement != null)
        {
            // Unsubscribe
        }

        if (e.NewElement != null)
        {
            var formsMap = (CustomMap)e.NewElement;
            routeCoordinates = formsMap.RouteCoordinates;
            Control.GetMapAsync(this);
        }
    }

    protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);

        if (this.Element == null || this.Control == null)
            return;
       
        if (e.PropertyName == "VisibleRegion")
        {
            var polylineOptions = new PolylineOptions();
            polylineOptions.InvokeColor(0x66FF0000);

            foreach (var position in routeCoordinates)
            {
                polylineOptions.Add(new LatLng(position.Latitude, position.Longitude));
            }

            NativeMap.AddPolyline(polylineOptions);
        }
    }

    public void working()
    {
        Log.Debug("xxxx", "WORKING");
    }
  }
}

现在类充当从PCL到Android的桥梁
(PCL:CustomMap)

namespace SomeNamespace
{

public class CustomMap : Map
{
    public static readonly BindableProperty RouteCoordinatesProperty =
    BindableProperty.Create<CustomMap, List<Position>>(p => p.RouteCoordinates, new List<Position>());
    
    public static readonly BindableProperty RouteColorProperty =
    BindableProperty.Create<CustomMap, int>(p => p.RouteColor, 0x66FF0000);

    //Property used to add points to the map. Then polyline utility will draw a line beteween thoses points
    public List<Position> RouteCoordinates
    {
        get { return (List<Position>)base.GetValue(RouteCoordinatesProperty); }
        set {
            base.SetValue(RouteCoordinatesProperty, value);
        }
    }

    public Int32 RouteColor
    {
        get { return (Int32)base.GetValue(RouteColorProperty); }
        set { base.SetValue(RouteColorProperty, value); }
    }

    public CustomMap()
    {
        RouteCoordinates = new List<Position>();
    }

   }
}

所以你可以看到,我添加了一个Int32 RouteColor变量,它应该在CustomMapRenderer中使用,以改变显示的路线的颜色。
但是当我在PCL代码中这样更改这个值时:

mCustomMap.RouteColor = 0x22AA0000;
    • 它触发OnElementPropertyChanged响应,但不触发OnElementChanged事件。**所以我无法获取更改后的值。我只知道它确实改变了(使用OnElementPropertyChanged)。

如果有人知道如何绕过这种现象...欢迎提出任何建议。先谢谢你了!

jmo0nnb3

jmo0nnb31#

Element表示forms元素,在本例中应为CustomMap。您可以使用它来检索属性值。

例如:

protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
    base.OnElementPropertyChanged(sender, e);

    if (this.Element == null || this.Control == null)
        return;
    ....
    //if RouteColor is the property that changed
    if (e.PropertyName == nameof(CustomMap.RouteColor))
    {
        //get new value
        var newColor = (Element as CustomMap)?.RouteColor;

        //and, update native control
        ....

相关问题