如何在 Xamarin Forms 中单击按钮 Android 自定义信息 window

how to do a button click in Xamarin Forms Android custom info window

在我的 Xamarin Forms Android 项目中,我使用 CustomMapRenderer 展示了 Map。我在地图屏幕中弹出一个 Info Window,此信息 window 的按钮名称为 "Call"。我需要为这个按钮做一个 "OnCLick" 按钮点击操作。我用谷歌搜索了它,但不幸的是我没有遇到任何解决方案。最后,我开始着手将 Android Native code -Java 转换为 Xamarin Forms Android - c#,为此转换我一直在使用这个答案 Google Maps Android API v2 - Interactive InfoWindow (like in original android google maps) 。此转换后的代码显示 A field initializer cannot reference the non static field, method or property OnInfoWindowElemTouchListener.onClickConfirmed(view, marker) 这个 错误 Java.Lang.Runnable 请帮助我解决问题。

OnInfoWindowElemTouchListener.cs

 using System.Threading.Tasks;
    using Android.App;
    using Android.Content;
    using Android.Gms.Maps.Model;
    using Android.Graphics.Drawables;
    using Android.OS;
    using Android.Views;
    using Android.Views.Accessibility;

    using Java.Lang;

    namespace Hotel.Droid
    {
        public abstract class OnInfoWindowElemTouchListener : Java.Lang.Object
        , View.IOnTouchListener
        {
            private View view;

            private Drawable bgDrawableNormal;
            private Drawable bgDrawablePressed;
            private Handler handler = new Handler();
            private Marker marker;
            private static bool endPressStatus = false;
            private bool pressed = false;


            public OnInfoWindowElemTouchListener(View view, Drawable bgDrawableNormal, Drawable bgDrawablePressed)
            {
                this.view = this.view;
                this.bgDrawableNormal = this.bgDrawableNormal;
                this.bgDrawablePressed = this.bgDrawablePressed;
            }
            public OnInfoWindowElemTouchListener()
            {

            }

            public void setMarker(Marker marker)
            {
                this.marker = this.marker;
            }



            public bool OnTouch(View vv, MotionEvent e)
            {
                if (0 <= e.GetX() && e.GetX() <= vv.Width && 0 <= e.GetY() && e.GetY() <= vv.Height)
                {
                    switch (e.ActionMasked)
                    {
                        case MotionEventActions.Down:
                            startPress();
                            break;

                        // We need to delay releasing of the view a little so it shows the
                        // pressed state on the screen
                        case MotionEventActions.Up:
                            //handler.PostDelayed(ConfirmClickRunnable, 150);
Task.Factory.StartNew(() => onClickConfirmed(view, marker));
Task.Delay(150);
                            break;

                        case MotionEventActions.Cancel:
                            endPress();
                            break;
                        default:
                            break;
                    }
                }
                else {
                    // If the touch goes outside of the view's area
                    // (like when moving finger out of the pressed button)
                    // just release the press
                    endPress();
                }

                return false;
            }


            private void startPress()
            {
                if (!pressed)
                {
                    pressed = true;
                    //handler.RemoveCallbacks(ConfirmClickRunnable);
                    view.SetBackgroundDrawable(bgDrawablePressed);
                    if ((marker != null))
                    {
                        marker.ShowInfoWindow();
                    }

                }

            }

            public bool endPress()
            {
                if (pressed)
                {
                    this.pressed = false;
                    handler.RemoveCallbacks(ConfirmClickRunnable);
                    view.SetBackgroundDrawable(bgDrawableNormal);


                    if ((marker != null))
                    {
                        marker.ShowInfoWindow();
                    }
                    endPressStatus = true;
                    return true;
                }
                else {
                    endPressStatus = false;
                    return false;
                }

            }

            private  Runnable ConfirmClickRunnable = new Java.Lang.Runnable(() =>
            {
               if (endPressStatus)
               {

                    onClickConfirmed(view, marker);
               }
            });




            /*private  class RunnableAnonymousInnerClassHelper : Java.Lang.Object, Java.Lang.IRunnable
            {
                private readonly Context outerInstance;     

                public RunnableAnonymousInnerClassHelper(Context outerInstance)
                {
                    this.outerInstance = outerInstance;         
                }

                public void Run()
                {
                    if (endPressStatus)
                    {
                        onClickConfirmed();
                    }
                }
            }*/


            protected abstract void onClickConfirmed(View v, Marker marker);


        }

    }

已更新

我已经实现了 Task.Factory.StartNew 而不是 Android Runnable,现在我停留在下面几行。我正在努力将下面的 Java 代码转换为 C#,因为它是由 Anonymous class 概念编写的。

Java

this.infoButtonListener = new OnInfoWindowElemTouchListener(infoButton,
                getResources().getDrawable(R.drawable.btn_default_normal_holo_light),
                getResources().getDrawable(R.drawable.btn_default_pressed_holo_light)) 
        {
            @Override
            protected void onClickConfirmed(View v, Marker marker) {
                // Here we can perform some action triggered after clicking the button
                Toast.makeText(MainActivity.this, marker.getTitle() + "'s button clicked!", Toast.LENGTH_SHORT).show();
            }
        }; 
        this.infoButton.setOnTouchListener(infoButtonListener);

infoButton 在代码中是呼叫按钮

C# - 请帮助我通过converting/using(如何使用)以上java代码

解决问题

这个window的解决方案太复杂了。请查看聊天室以了解分步解决方案。 https://chat.whosebug.com/rooms/128847/discussion-between-jamal-and-yuri-s