Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin.Android Support Library

I'm trying to implement James Montemagno's navigation drawer (https://github.com/jamesmontemagno/Xamarin-Templates) and I'm running into issues. When I create a new project, I don't run into any issues, but when I attempt to add the support libraries to an existing project, I get the following error:

The type or namespace name 'Support' does not exist in the namespace 'my namespace' (are you missing an assembly reference)?

Here is my code (The problem code is the very last line. I haven't gone past this because I'd like to understand what's happening):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

using Android.Support.V7.App;
using Android.Support.V4.Widget;
using Android.Support.V4.View;

using MyApp.Parse;
using MyApp.Android.Core.Login;
using MyApp.Android.Core.Utils;

namespace MyApp.Android.Core.Main
{
    [Activity(Label = "MainActivity", Icon = "@drawable/Icon")]
    public class MainActivity : BaseActivity
    {
        private MyActionBarDrawerToggle mDrawerToggle;
        private string mDrawerTitle;
        private string mTitle;

        private DrawerLayout mDrawerLayout;
        private ListView mDrawerListView;
        private DrawerMenuAdapter mAdapter;

        protected override int LayoutResource
        {
            get
            {
                return Resource.Layout.Main;
            }
        }

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // setup drawer
            mTitle = mDrawerTitle = Title;
            mDrawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
            mDrawerListView = FindViewById<ListView>(Resource.Id.left_drawer);

            // create adapter for drawer
            mDrawerListView.Adapter = mAdapter = new DrawerMenuAdapter(this);

            // set click handler
            mDrawerListView.ItemClick += (sender, args) => ListItemClicked(args.Position);

            // set DrawerShadow
            mDrawerLayout.SetDrawerShadow(Resource.Drawable.drawer_shadow_dark, (int)GravityCompat.Start);

            // set DrawerToggle - the animation that happens with the indicator next to the actionbar
            mDrawerToggle = new MyActionBarDrawerToggle(this, mDrawerLayout, Toolbar, 
                Resource.String.drawer_open, 
                Resource.String.drawer_close);

            // display current fragment's title and update the options menu
            mDrawerToggle.DrawerClosed += (o, args) =>
            {
                this.SupportActionBar.Title = mTitle;
                this.SupportInvalidateOptionsMenu();
            };

            // display the drawer title and update the options menu
            mDrawerToggle.DrawerOpened += (o, args) =>
            {
                this.SupportActionBar.Title = mDrawerTitle;
                this.SupportInvalidateOptionsMenu();
            };

            // set the drawer listener
            mDrawerLayout.SetDrawerListener(mDrawerToggle);

            // if first time, click first item
            if (savedInstanceState == null)
                ListItemClicked(0);

            //
            Button logout = FindViewById<Button>(Resource.Id.logoutButton);

            logout.Click += (object sender, EventArgs e) =>
            {
                Toast.MakeText(this, APIHandler.Logout(), ToastLength.Long).Show();
                StartActivity(typeof(DispatchActivity));
            };
        }

        int oldPosition = -1;
        private void ListItemClicked(int position)
        {
            // this way we don't load twice
            if (position == oldPosition)
                return;

            oldPosition = position;

            Android.Support.V4.App.Fragment fragment = null;
        }
    }
}

I'm using Visual Studio and I've tried importing Xamarin.Android.Support.v4 and Xamarin.Android.Support.v7.AppCompat using both Reference and Components within my project. I can find Android.Support when I am 'using' at the beginning of a class, but I cannot locate Android.Support once I'm in the declaration of the class.

Does anyone know how to fix this? I'm new to C#, Xamarin, and Visual Studio, so I'm kind of at a loss.

I know I can fix this by just starting a new project and copying over most of my old code, but I'd like to know why I'm getting this error.

like image 555
Matt Avatar asked Jun 30 '15 01:06

Matt


2 Answers

It looks like there actually is a namespace collision with MyApp.Android and Android.Support.etc - to resolve the fragment issue, put this up with the usings:

using Fragment = Android.Support.V4.App.Fragment;

That should resolve the namespace correctly (or you could change your namespace from Myapp.Android, if it becomes too much trouble).

like image 87
codechinchilla Avatar answered Oct 04 '22 03:10

codechinchilla


I would suggesting importing with a using statement a bit less specific:

using SupportV7 = Android.Support.V7.App;
using SupportV4 = Android.Support.V4.App;

Or any names you'd like so you can say:

  SupportV7.Fragment
  SupportV4.Fragment

Just a preference so you know when you look at your code 3 months later.

like image 21
jStaff Avatar answered Oct 04 '22 04:10

jStaff