Android Xamarin中的导航(c#)
本文关键字:导航 Xamarin Android | 更新日期: 2023-09-27 18:09:47
我尝试做导航视图和DrawerLayout
这里是我的axml:
<?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:fitsSystemWindows="true">
<!-- your content layout -->
<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/nav_menu"/>
</android.support.v4.widget.DrawerLayout>
下面是我的Activity:
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 MvvmCross.Droid.Views;
using Android.Support.V4.Widget;
using Android.Support.Design.Widget;
using Android.Content.PM;
namespace TieiT.Droid.Views
{
[Activity(Label = "GeneralInfo", ScreenOrientation = ScreenOrientation.Portrait)]
public class GeneralInfo : MvxActivity
{
DrawerLayout drawerLayout;
NavigationView navigationView;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.GeneralInfo);
// Create your application here
var toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
SetSupportActionBar(toolbar);
SupportActionBar.SetHomeAsUpIndicator(Resource.Drawable.ic_menu);
SupportActionBar.SetDisplayHomeAsUpEnabled(true);
drawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
navigationView = FindViewById<NavigationView>(Resource.Id.nav_view);
navigationView.NavigationItemSelected += (sender, e) =>
{
e.MenuItem.SetChecked(true);
//react to click here and swap fragments or navigate
drawerLayout.CloseDrawers();
};
}
public override bool OnOptionsItemSelected(IMenuItem item)
{
switch (item.ItemId)
{
case Android.Resource.Id.Home:
drawerLayout.OpenDrawer(Android.Support.V4.View.GravityCompat.Start);
return true;
}
return base.OnOptionsItemSelected(item);
}
}
}
有这些错误:
Error CS0103 The name 'SupportActionBar' does not exist in the current context TieiT.Droid F:'Conf'TieiT.Core'TieiT.Droid'Views'GeneralInfo.cs 31 Active
Error CS0103 The name 'SupportActionBar' does not exist in the current context TieiT.Droid F:'Conf'TieiT.Core'TieiT.Droid'Views'GeneralInfo.cs 32 Active
Error CS0103 The name 'SetSupportActionBar' does not exist in the current context TieiT.Droid F:'Conf'TieiT.Core'TieiT.Droid'Views'GeneralInfo.cs 30 Active
Error CS0117 'Resource.Id' does not contain a definition for 'drawer_layout' TieiT.Droid F:'Conf'TieiT.Core'TieiT.Droid'Views'GeneralInfo.cs 34 Active
也许有愚蠢的问题,但我是新手。如何修复这些错误?
谢谢你的回答和帮助。
您需要继承MvxAppCompatActivity
来访问支持库组件,如SupportActionBar
我建议阅读下面的帖子:http://martiancraft.com/blog/2015/06/android-support-library/