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

时间:2016-11-17 09:11:31

标签: google-maps xamarin xamarin.android xamarin.forms android-maps-v2

在我的Xamarin Forms Android项目中,我使用Map展示CustomMapRenderer。我在Map屏幕中弹出Info Window,此信息窗口的按钮名称为"Call"。我需要为此按钮执行"OnCLick"按钮单击操作。我用谷歌搜索了它,但不幸的是我没有遇到任何解决方案。最后我开始将Android Native code - Java 转换为Xamarin Forms Android - c#,对于此转换,我一直在使用此答案{{3 }。此转换后的代码在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);


        }

    }

Google Maps Android API v2 - Interactive InfoWindow (like in original android google maps)

更新

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

爪哇

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# - 请通过转换/使用(如何使用)上面的java代码帮助我解决问题

1 个答案:

答案 0 :(得分:2)

此窗口的解决方案太复杂了。请逐步查看聊天室解决方案。 http://chat.stackoverflow.com/rooms/128847/discussion-between-jamal-and-yuri-s