设置Android.Support.V4.App.Fragments时出错

时间:2019-05-08 11:28:20

标签: c# android-fragments xamarin.android

我有问题。我正在尝试使用Android.SupportV4.App.Fragments设置片段。到目前为止,这是我的代码:

Android.Support.V4.App.Fragment fragment = null;
Android.Support.V4.App.Fragment PreviousFragment = null;

switch (CurrentFragmentNum)
{
    case 1:
        fragment = ShapeSelectionFragment;
        break;
    case 2:
        fragment = ShapeFormationFragment;
        break;
}

switch (PreviousFragmentNum)
{
    case 1:
        PreviousFragment = ShapeSelectionFragment;
        break;
    case 2:
        PreviousFragment = ShapeFormationFragment;
        break;
}


var trans = SupportFragmentManager.BeginTransaction();
trans.SetCustomAnimations(Resource.Animation.slide_in_left,
                Resource.Animation.slide_out_right, 0, 0);
trans.Replace(Resource.Id.fragmentContainer, fragment);
trans.Hide(PreviousFragment);
trans.Show(fragment);
trans.AddToBackStack(null);
trans.Commit();

现在,我们从该行的第一个错误开始:var trans = SupportFragmentManager.BeginTransaction();,并显示错误:

  

名称'SupportFragmentManager'在当前不存在   上下文

如何解决此错误?

1 个答案:

答案 0 :(得分:0)

您应该知道android.support.v4.app.Fragment与android.app.Fragment不同,继承的父类也不同

android.support.v4.app.Fragment

var trans = SupportFragmentManager.BeginTransaction();

,当前类必须扩展 FragmentActivity

android.app.Fragment

var trans = FragmentManager.BeginTransaction();

只需要扩展活动