Why MapFragment is not recognized by visual studio?

195 views Asked by At

I have a map inside a scrollview and when we try to scroll around the map, scroll view comes up and scrolls itself but not the map. I'm trying to fix this issue with a code that I found on google but visual studio is showing some errors:

Error CS0246: The type or namespace name 'MapFragment' could not be found 
(are you missing a using directive or an assembly reference?) (CS0246) (MasterDetailPageNavigation)

and

Error CS0115: 'TouchableMapFragment.OnCreateView(LayoutInflater, ViewGroup, Bundle)': no suitable method found to override (CS0115) (MasterDetailPageNavigation)
using System;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;
//using Xamarin.Forms.Maps;

namespace MasterDetailPageNavigation
{
    public class TouchableMapFragment : MapFragment
    {
        public event EventHandler TouchDown;
        public event EventHandler TouchUp;

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            var root = base.OnCreateView(inflater, container, savedInstanceState);
            var wrapper = new TouchableWrapper(Activity);
            wrapper.SetBackgroundColor(Resources.GetColor(Android.Resource.Color.Transparent));
            ((ViewGroup)root).AddView(wrapper,
              new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent));

            wrapper.TouchUp = () =>
            {
                if (TouchUp != null)
                    TouchUp(this, EventArgs.Empty);
            };
            wrapper.TouchDown = () =>
            {
                if (TouchDown != null)
                    TouchDown(this, EventArgs.Empty);
            };

            return root;
        }

        class TouchableWrapper : FrameLayout
        {
            public Action TouchDown;
            public Action TouchUp;

            #region ctors
            protected TouchableWrapper(IntPtr javaReference, JniHandleOwnership transfer)
              : base(javaReference, transfer) { }
            public TouchableWrapper(Context context)
              : this(context, null) { }
            public TouchableWrapper(Context context, IAttributeSet attrs)
              : this(context, attrs, 0) { }
            public TouchableWrapper(Context context, IAttributeSet attrs, int defStyle)
              : base(context, attrs, defStyle) { }
            #endregion

            public override bool DispatchTouchEvent(MotionEvent e)
            {
                switch (e.Action)
                {
                    case MotionEventActions.Down:
                        if (TouchDown != null)
                            TouchDown();
                        break;
                    case MotionEventActions.Cancel:
                    case MotionEventActions.Up:
                        if (TouchUp != null)
                            TouchUp();
                        break;
                }

                return base.DispatchTouchEvent(e);
            }
        }
    }
}

Also this code has a .AXML file that I will include below but I never had used something like this before, I'm working with XAML from Xamarin, where I have to put this axml?

the xaml I talked about:

<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/scroll">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="match_parent">
        <fragment
            android:id="@+id/map"
            android:layout_width="600dp"
            android:layout_height="match_parent"
            android:name="my.awesome.namespace.TouchableMapFragment" />
    </LinearLayout>
</HorizontalScrollView>

Sorry if this is a dummy question but I'm studding alone and it's being a little bit hard.

1

There are 1 answers

6
nevermore On

You should install the Xamarin.GooglePlayServices.Maps package from NuGet.

The Xamarin.GooglePlayServices.Maps package contains the Xamarin.Android bindings for the Google Play Services Maps API. To add the Google Play Services Map package, right-click the References folder of your project in the Solution Explorer and click Manage NuGet Packages...:

You can get the detailed steps about how to install this package from the document.

After installed the package, use the namespace Android.Gms.Maps, the MapFragment is a class in the namespace Android.Gms.Maps:

using Android.Gms.Maps;