0

在我的Xamarin Forms Android项目中,我使用CustomMapRenderer显示Map。我在地图屏幕上弹出一个Info Window,这个信息窗口有一个叫做"Call"的按钮名称。我需要对此按钮执行一次"OnCLick"按钮单击操作。我使用了Google,但不幸的是我没有遇到任何解决方案。最后我开始着手将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请帮我解决问题。如何在Xamarin中点击按钮表单Android自定义信息窗口

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); 


     } 

    } 

enter image description here

更新

我已经实现了Task.Factory.StartNew代替Android Runnable,现在我stucking在以下线。我很努力地将Java代码转换为C#,因为它是由Anonymous类概念编写的。

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

来解决问题
+0

代码过于复杂(至少对我来说),给你一个答案,但如果你共享你的样本项目,我可以尝试看看 –

+0

分享您'电子邮件id'我会送 – Jamal

+0

你可以将它张贴在Dropbox或github上,如果你需要帮助 –

回答

1

该窗口的解决方案过于复杂。请参阅聊天室一步一步的解决方案。 http://chat.stackoverflow.com/rooms/128847/discussion-between-jamal-and-yuri-s

+0

我已经在'Google Drive'中上传了示例工作项目代码,请你检查并帮助完成这个问题?这里https://drive.google.com/file/d/0Bwrd--0WzaM2Ry1jdzI3alZSeDQ/view?usp=sharing你可以得到代码。 **注意:**清理并右键单击'CustomRenderer.Droid'然后构建。 – Jamal

+0

您现在遇到的问题是什么? –

+0

我没有任何'语法错误'在我的代码中,我可以建立没有错误的代码。我无法“执行操作(调用方法)”,同时点击“调用”按钮。 – Jamal

相关问题