Android:CS0103 ;名称';设置支持操作栏';在当前上下文中不存在
CS0103当前上下文中不存在名称“SetSupportActionBar” 我清理和构建了很多时间,但这个错误是发生的,我将改变我的清单xml主题也有一个错误,但如何才能解决这个问题 我的android目标平台api 27和最低18 my mainactivity.cs唯一的错误是如何解决此问题 我会从stackoverflow中找到一些答案,但它不能解决我的问题 MainActivity.csAndroid:CS0103 ;名称';设置支持操作栏';在当前上下文中不存在,android,xamarin,xamarin.android,main-activity,Android,Xamarin,Xamarin.android,Main Activity,CS0103当前上下文中不存在名称“SetSupportActionBar” 我清理和构建了很多时间,但这个错误是发生的,我将改变我的清单xml主题也有一个错误,但如何才能解决这个问题 我的android目标平台api 27和最低18 my mainactivity.cs唯一的错误是如何解决此问题 我会从stackoverflow中找到一些答案,但它不能解决我的问题 MainActivity.cs using System; using Android.App; using Android.Co
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;
}
}
}
使用系统;
使用Android.App;
使用Android.Content;
使用Android.Runtime;
使用Android.Views;
使用Android.Widget;
使用Android.OS;
使用Android.Support.V7.App;
使用Android.Support.V4.Widget;
使用V7Toolbar=Android.Support.V7.Widget.Toolbar;
使用Android.Support.Design.Widget;
命名空间导航抽屉布局
{
[Activity(Label=“NavigationDrawerLayout”,Theme=“@style/Theme.DesignDemo”,MainLauncher=true,Icon=“@drawable/Icon”)]
公共类MainActivity:AppCompativeActivity
{
抽屉布局抽屉布局;
导航视图导航视图;
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
//从“主”布局资源设置视图
SetContentView(Resource.Layout.Main);
抽屉布局=FindViewById(Resource.Id.drawer\u布局);
//创建ActionBarDrawerToggle按钮并将其添加到工具栏
var-toolbar=findviewbyd(Resource.Id.toolbar);
设置支持操作栏(工具栏);
var drawerToggle=new ActionBarDrawerToggle(这个,抽屉布局,工具栏,Resource.String.drawer\u打开,Resource.String.drawer\u关闭);
抽屉布局。设置抽屉链接器(抽屉切换);
drawerToggle.SyncState();
navigationView=findviewbyd(Resource.Id.nav_视图);
setupDrawerContent(导航视图);
}
无效设置抽屉内容(导航视图导航视图)
{
navigationView.NavigationItemSelected+=(发件人,e)=>{
e、 MenuItem.SetChecked(true);
抽屉布局;
};
}
公共覆盖布尔onCreateOptions菜单(IMenu菜单)
{
导航视图.充气菜单(资源.菜单.导航菜单);
返回true;
}
}
}
toolbar.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>
`
`
styles.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>
#2196F3
#1976D2
@颜色/窗口背景
尝试更新您的Android SDK和NuGet软件包(Xamarin.Android.Support.v7…)。
此外,您还可以尝试从Android项目中删除bin和obj文件夹将类活动更改为AppCompatActivity
然后重新启动Visual Studio。您可以安装哪个版本的Xamarin.Android.Support.v7.AppCompat?Xamarin.Android.Support.v7.AppCompat 23.3.0I会将您的Xamarin.Android.Support.*软件包更新到至少24.x,最新的是
27.0.2
尝试不工作SetSupportActionBar
方法应位于AppCompatActivity
类中,尝试删除bin和obj文件夹并重新启动VS以重新编译项目。