android - Xamarin.Android : CS0103 The name 'SetSupportActionBar' does not exist in the current context

标签 android xamarin xamarin.android main-activity

CS0103 当前上下文中不存在名称“SetSupportActionBar”

我很干净并且构建了很多次但是发生了这个错误我也会更改我的 list xml 主题但是有一个错误如何解决这个问题

我的 android 目标平台 api 27 和最低 18

我的mainactivity.cs只出现错误如何解决这个问题

我会从 stackoverflow 找到一些答案,但它不能解决我的问题

主 Activity .cs

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Support.V7.App;
using Android.Support.V4.Widget;
using V7Toolbar = Android.Support.V7.Widget.Toolbar;
using Android.Support.Design.Widget;

namespace NavigationDrawerLayout
{
    [Activity(Label = "NavigationDrawerLayout", Theme = "@style/Theme.DesignDemo", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : AppCompatActivity
    {

        DrawerLayout drawerLayout;
        NavigationView navigationView;
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            drawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);


            // Create ActionBarDrawerToggle button and add it to the toolbar
            var toolbar = FindViewById<V7Toolbar>(Resource.Id.toolbar);
            SetSupportActionBar(toolbar);


            var drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, Resource.String.drawer_open, Resource.String.drawer_close);
            drawerLayout.SetDrawerListener(drawerToggle);
            drawerToggle.SyncState();

            navigationView = FindViewById<NavigationView>(Resource.Id.nav_view);
            setupDrawerContent(navigationView);
        }
        void setupDrawerContent(NavigationView navigationView)
        {
            navigationView.NavigationItemSelected += (sender, e) => {
                e.MenuItem.SetChecked(true);
                drawerLayout.CloseDrawers();
            };
        }

        public override bool OnCreateOptionsMenu(IMenu menu)
        {

            navigationView.InflateMenu(Resource.Menu.nav_menu);
            return true;

        }

        }
}

工具栏.axml

 <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="4dp"
android:background="?attr/colorPrimary" />
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
`

样式.xml

<?xml version="1.0" encoding="utf-8" ?>
<resources>
  <style name="Theme.DesignDemo" parent="Base.Theme.DesignDemo"></style>
  <style name="Base.Theme.DesignDemo" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">#2196F3</item>
    <item name="colorPrimaryDark">#1976D2</item>
    <item name="android:windowBackground">@color/window_background</item>
  </style>
</resources>

最佳答案

将类 Activity 更改为 AppCompatActivity

然后重新启动 Visual Studio。

关于android - Xamarin.Android : CS0103 The name 'SetSupportActionBar' does not exist in the current context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50479080/

相关文章:

c# - 从 Xamarin 调用 PutAsJsonAsync 的异常

android - 更改微调器文本颜色

android - 无法创建调试或签名的APK

android - 线程中的数学函数返回错误值,使用 android ndk

布局渲染完成后Android回调?

Xamarin 表单 : How to make button appear at the bottom of a page

windows - 通用 Windows 平台上的 Mono.data.sqlite

xamarin - 在 Xamarin.Android 中,需要通过使用 DependencyService 从 Xamarin.Forms 调用来使用 Activity

c# - 从服务器 xamarin.android 收到的证书无效

android - findViewById 中出现奇怪的 ClassCastException