Forum General

Error (CS0246) : NavigationView doesn't exists in current context using Xamarin.Android

The NavigationView cannot be found because of missing using directive or assembly reference.

HomeActivity.cs code as follow:

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Support.V4.Widget;
using Android.Support.V7.App;
using Android.Support.Design;
using System.Collections.Generic;


namespace SQLTuts
{
    [Activity (Label = "SQL Tuts", MainLauncher = false, Icon = "@mipmap/icon")]
    public class HomeActivity : AppCompatActivity
    {
    DrawerLayout drawerLayout;

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

        // Create UI
        SetContentView(Resource.Layout.Main);
        drawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);

        // Init toolbar
        var toolbar = FindViewById<Toolbar>(Resource.Id.toolbar);
        SetSupportActionBar(toolbar);   

        // Attach item selected handler to navigation view
        var navigationView = FindViewById<NavigationView>(Resource.Id.nav_view);
        navigationView.NavigationItemSelected += NavigationView_NavigationItemSelected;

        // Create ActionBarDrawerToggle button and add it to the toolbar
        var drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, Resource.String.open_drawer, Resource.String.close_drawer);
        drawerLayout.SetDrawerListener(drawerToggle);
        drawerToggle.SyncState();
    }
    void NavigationView_NavigationItemSelected(object sender, NavigationView.NavigationItemSelectedEventArgs e)
    {
        switch (e.MenuItem.ItemId)
        {
        case (Resource.Id.nav_home):
            // React on 'Home' selection
            break;
        case (Resource.Id.nav_messages):
            // React on 'Messages' selection
            break;
        case (Resource.Id.nav_friends):
            // React on 'Friends' selection
            break;
        case (Resource.Id.nav_discussion):
            // React on 'Discussion' selection
            break;                
        }

        // Close drawer
        drawerLayout.CloseDrawers();
    }
}

Following are the![](http://i.stack.imgur.com/J0wMQ.png image) for above code error.

Main.axml code as follow

 <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/drawer_layout"
    android:fitsSystemWindows="true">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    </RelativeLayout>
    <android.support.design.widget.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:id="@+id/nav_view"
        app:menu="@menu/navmenu"
        app:headerLayout="@layout/header" />
</android.support.v4.widget.DrawerLayout>

Following are the components & packages installed in it.

I tried various namespace to be added in code, but didn't work.

Posts

  • DaveHuntDaveHunt USMember ✭✭✭✭✭

    NavigationView is in the Android.Support.Design.Widget namespace, but you don't have a using directive for that.

  • @DaveHunt . Thanks Man. I had made some changes in namespaces & hence the code run successfully.

    Here are the changes in namespace.

    using System;
    using Android.App;
    using Android.Content;
    using Android.Runtime;
    using Android.Views;
    using Android.Widget;
    using Android.OS;
    using Android.Support.V4.Widget;
    using Android.Support.V7.App;
    using Android.Support.V7.Widget;
    using Android.Support.Design.Widget;
    
Sign In or Register to comment.