不能强制转换为android.support.v4.app.片段(Xamarin c#)
本文关键字:片段 app Xamarin v4 support 转换 android 不能 | 更新日期: 2023-09-27 18:09:53
我试着做DrawerLayout
但我面临的问题
Java.Lang.ClassCastException: tieit.droid.MyListFragment cannot be cast to android.support.v4.app.Fragment
这是我的代码在MainViewModel代码:
using MvvmCross.Core.ViewModels;
using System;
using System.Collections.Generic;
namespace TieiT.Core
{
public class MainViewModel : MvxViewModel
{
readonly Type[] _menuItemTypes = {
typeof(MyListViewModel),
typeof(MySettingsViewModel),
};
public IEnumerable<string> MenuItems { get; private set; } = new [] { "My List", "My Settings" };
public void ShowDefaultMenuItem()
{
NavigateTo (0);
}
public void NavigateTo (int position)
{
ShowViewModel (_menuItemTypes [position]);
}
}
public class MenuItem : Tuple<string, Type>
{
public MenuItem (string displayName, Type viewModelType)
: base (displayName, viewModelType)
{}
public string DisplayName
{
get { return Item1; }
}
public Type ViewModelType
{
get { return Item2; }
}
}
}
当我有错误时,它会高亮显示:
public void NavigateTo (int position)
{
ShowViewModel (_menuItemTypes [position]);
}
MyListFragment:
using Android.OS;
using Android.Runtime;
using Android.Views;
using MvvmCross.Droid.Shared.Attributes;
using MvvmCross.Droid.FullFragging.Fragments;
using TieiT.Core;
namespace TieiT.Droid
{
[MvxFragmentAttribute(typeof(MainViewModel), Resource.Id.frameLayout)]
[Register("tieit.droid.MyListFragment")]
public class MyListFragment : MvxFragment<MyListViewModel>
{
public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.Inflate(Resource.Layout.MyListView, container, false);
}
}
}
这里是我需要显示DrawerLayout
的活动:
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.Support.V7.AppCompat;
using TieiT.Droid;
using Android.Support.V7.App;
using Android.Support.V4.Widget;
using Toolbar = Android.Support.V7.Widget.Toolbar;
using Fragment = Android.Support.V4.App.Fragment;
using TieiT.Core;
namespace TieiT.Droid.Views
{
[Activity]
public class GeneralInfo : MvxCachingFragmentCompatActivity<MainViewModel>
{
ActionBarDrawerToggle _drawerToggle;
ListView _drawerListView;
DrawerLayout _drawerLayout;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.GeneralInfoPage);
var toolbar = FindViewById<Toolbar>(Resource.Id.toolbar);
SetSupportActionBar(toolbar);
SupportActionBar.SetDisplayHomeAsUpEnabled(false);
_drawerListView = FindViewById<ListView>(Resource.Id.drawerListView);
_drawerListView.ItemClick += (s, e) => ShowFragmentAt(e.Position);
_drawerListView.Adapter = new ArrayAdapter<string>(this, global::Android.Resource.Layout.SimpleListItem1, ViewModel.MenuItems.ToArray());
_drawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawerLayout);
_drawerToggle = new ActionBarDrawerToggle(this, _drawerLayout, Resource.String.OpenDrawerString, Resource.String.CloseDrawerString);
_drawerLayout.SetDrawerListener(_drawerToggle);
ShowFragmentAt(0);
}
void ShowFragmentAt(int position)
{
ViewModel.NavigateTo(position);
Title = ViewModel.MenuItems.ElementAt(position);
_drawerLayout.CloseDrawer(_drawerListView);
}
protected override void OnPostCreate(Bundle savedInstanceState)
{
_drawerToggle.SyncState();
base.OnPostCreate(savedInstanceState);
}
public override bool OnOptionsItemSelected(IMenuItem item)
{
if (_drawerToggle.OnOptionsItemSelected(item))
return true;
return base.OnOptionsItemSelected(item);
}
}
}
如何修复这个错误?
谢谢你的帮助。
更新
My Main Layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- content must go before menu because of z-order -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
layout="@layout/toolbar" />
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<!-- menu -->
<ListView
android:id="@+id/drawerListView"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:layout_width="240dp"
android:layout_height="match_parent"
android:background="?android:attr/windowBackground" />
</android.support.v4.widget.DrawerLayout>
下面是我在console中的内容:
[ERROR] FATAL UNHANDLED EXCEPTION: Java.Lang.ClassCastException: tieit.droid.MyListFragment cannot be cast to android.support.v4.app.Fragment
10-01 19:52:15.838 E/mono-rt ( 3039): --- End of managed Java.Lang.ClassCastException stack trace ---
10-01 19:52:15.838 E/mono-rt ( 3039): java.lang.ClassCastException: tieit.droid.MyListFragment cannot be cast to android.support.v4.app.Fragment
你安装了错误的包装,因为MvvmCross.Droid.FullFragging
只支持Fragment
,当你尝试转换它时你会得到错误。当使用Android.Support.V4.App.Fragment
时,您应该安装MvvmCross.Droid.Support.V7.Fragging
和'MvvmCross.Droid.V7.AppCompat'
。