我需要在我的应用程序的主要活动中添加一个抽屉。我看过一些教程,并添加了所有必要的代码,抽屉可以打开和关闭。但是,我无法选择任何项目。当我单击其中的任何一个时,抽屉都将关闭,并且本应执行的代码永远不会运行。
这些是我的MainActivity.cs的第一行:
public class MainActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
SetContentView(Resource.Layout.activity_main);
Android.Support.V7.Widget.Toolbar toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
DrawerLayout drawer = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, Resource.String.navigation_drawer_open, Resource.String.navigation_drawer_close);
drawer.AddDrawerListener(toggle);
toggle.SyncState();
NavigationView navigationView = FindViewById<NavigationView>(Resource.Id.nav_view);
navigationView.NavigationItemSelected += NavigationView_NavigationViewSelected;
以及NavigationView_NavigationViewSelected事件:
void NavigationView_NavigationViewSelected(object sender, NavigationView.NavigationItemSelectedEventArgs e)
{
switch (e.MenuItem.ItemId)
{
case (Resource.Id.nav_search):
Android.Util.Log.WriteLine(Android.Util.LogPriority.Info, "SmartLyrics", "MainActivity.cs: Test for Search button on drawer");
break;
case (Resource.Id.nav_saved):
Android.Util.Log.WriteLine(Android.Util.LogPriority.Info, "SmartLyrics", "MainActivity.cs: Test for Saved button on drawer");
break;
case (Resource.Id.nav_settings):
Android.Util.Log.WriteLine(Android.Util.LogPriority.Info, "SmartLyrics", "MainActivity.cs: Test for Settings button on drawer");
break;
case (Resource.Id.nav_about):
Android.Util.Log.WriteLine(Android.Util.LogPriority.Info, "SmartLyrics", "MainActivity.cs: Test for About button on drawer");
break;
}
}
当我单击选项时,LogCat在此事件内不显示任何消息。我在这里缺少任何代码吗?
答案 0 :(得分:0)
您的MainActivity
类未实现NavigationView.OnNavigationItemSelectedListener
。
您的MainActivity
类必须实现NavigationView.OnNavigationItemSelectedListener
答案 1 :(得分:0)
尝试像这样更改您的xaml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:minWidth="25dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:id="@+id/toolbar" />
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="@layout/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer"
/>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>