Xamarin Forms自定义控件:在渲染器中实现方法 [英] Xamarin Forms custom control: implement a method in the renderer

查看:89
本文介绍了Xamarin Forms自定义控件:在渲染器中实现方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建标准XF Map对象的扩展版本:

I'm trying to create an extended version of the standard XF Map object:

public class RRMap: Map
{

    public void DoSomethingOnMap() {
        /* ... */
    }

}

我还创建了一个Android渲染器(iOS会在以后发布):

I also created an Android renderer (iOS will come later):

[assembly: ExportRenderer(typeof(RRMap), typeof(RRMapRendererAndroid))]
namespace MyApp.Droid.Renderers
{
    public class RRMapRendererAndroid : MapRenderer
    {
        public RRMapRendererAndroid(Context context) : base(context) { }

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

            if (e.NewElement != null)
            {
                Control.GetMapAsync(this);
            }
        }

        protected override MarkerOptions CreateMarker(Pin pin)
        {
            var marker = new MarkerOptions();
            marker.SetPosition(new LatLng(pin.Position.Latitude, pin.Position.Longitude));
            marker.SetTitle(pin.Label);
            marker.SetSnippet(pin.Address);
            marker.SetIcon(BitmapDescriptorFactory.DefaultMarker(210));
            return marker;
        }

    }
}

到目前为止,一切正常:绘制地图并使用自定义颜色创建图钉.

Everything is working fine so far: the map is rendered and pins are created with a custom color.

不幸的是,我被困在 DoSomethingOnMap 方法的实现上:它应该是共享代码中的一种方法,但是根据平台的不同,应该以不同的方式实现.

Unfortunately, I'm stuck on the implementation of DoSomethingOnMap method: it should be a method in the shared code, but it should be implemented in different ways, depending on the platform.

在其他情况下,我将使用DependencyService创建一个接口以进行实施,但是在这种情况下,我不知道如何进行操作.

In other circumstances, I would create an interface using DependencyService for implementation, but in this particular case I can't figure out how to proceed.

推荐答案

第一个解决方案是您可以使用

The first solution is you can use a messaging-center, this can communicate between shared project and iOS/Android project.

doSomethingOnMap 方法中发布一条消息,您订阅该消息的任何地方都会被触发.

Publish a message in the doSomethingOnMap method and anywhere you subscribed to the message will be triggered.

第二是在共享项目中创建一个事件并在渲染器中订阅该事件,我在下面编写了两种解决方案:

The second is create an event in your shared project and subscribe to that event in the renderer, I wrote both two solutions below:

在您的共享项目中:

public class CustomMap : Map
{
    public List<CustomPin> CustomPins { get; set; }

    public event EventHandler CallToNativeMethod;

    public void doSomething()
    {
        if (CallToNativeMethod != null)
            CallToNativeMethod(this, new EventArgs());
    }

    public void doSomething(CustomMap myMap) {


        MessagingCenter.Send<CustomMap>(this, "Hi");

    }
}

在渲染器中:

protected override void OnElementChanged(ElementChangedEventArgs<View> e)
    {
        base.OnElementChanged(e);

        if (e.OldElement != null)
        {

        }

        if (e.NewElement != null)
        {

            MessagingCenter.Subscribe<CustomMap>(this, "Hi", (sender) =>
            {
                // Do something whenever the "Hi" message is received
                Console.WriteLine("hi");
            });

            ((CustomMap)e.NewElement).CallToNativeMethod += (sender, arg) =>
            {
                Console.WriteLine("native method");
            };
        }
    }

在任何要调用此方法的地方:

At anywhere you want to call this method:

private void Button_Clicked(object sender, System.EventArgs e)
{
    customMap.doSomething();
    customMap.doSomething(customMap);
}

这篇关于Xamarin Forms自定义控件:在渲染器中实现方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆