当前上下文中不存在名称“ SupportFragmentManager”

时间:2019-09-23 19:05:22

标签: c# xamarin.android visual-studio-2019

我正试图在我所面对的xamarin android中创建一个选项卡式Swipe导航系统,名称'SupportFragmentManager'在当前上下文中不存在,错误我不知道如何清除此信息我是xamarin android的新手,请帮助我

  

MainActivity.cs

protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.GalleryPage);
            // Create your application here

            txtView = FindViewById<TextView>(Resource.Id.txtView);
            _imageView = FindViewById<ImageView>(Resource.Id.imageView1);
            Button button = FindViewById<Button>(Resource.Id.MyButton);
            TabLayout tabLayout = (TabLayout)FindViewById(Resource.Id.tablayout_navigation);

            ViewPager viewPager = (ViewPager)FindViewById(Resource.Id.pager);
            SetupviewPager(viewPager);

            tabLayout.SetupWithViewPager(viewPager);

        button.Click += delegate
            {
                Intent = new Intent();
                Intent.SetType("image/*");
                Intent.SetAction(Intent.ActionGetContent);
                StartActivityForResult(Intent.CreateChooser(Intent, "Select Picture"), PickImageId);
            }; 
        }

        private void SetupviewPager(ViewPager viewPager)
        {
            viewPager.OffscreenPageLimit = 3;
            var adapter = new PageAdapter1(SupportFragmentManager);
            adapter.AddFragment(new Fragment1(), "Title1");
            adapter.AddFragment(new Fragment2(), "Title2");
            adapter.AddFragment(new Fragment3(), "Title3");

            viewPager.Adapter = adapter;
        }
  

PageAdapter1.cs

using System.Collections.Generic;
using Android.Support.V4.App;
using Java.Lang;
using Fragment = Android.Support.V4.App.Fragment;
using FragmentManager = Android.Support.V4.App.FragmentManager;



namespace OCR_Pro
{
   public class PageAdapter1: FragmentPagerAdapter
    {
        private readonly List<Fragment> _fragments;
        private readonly List<string> _fragmentnames;

        public PageAdapter1(FragmentManager fm) : base(fm)
        {
            _fragments = new List<Fragment>();
            _fragmentnames = new List<string>();
        }

        public override int Count
        {
            get { return _fragments.Count; }
        }
        public override Fragment GetItem(int position)
        {
            return _fragments[position];
        }

        public void AddFragment(Fragment fragment, string name)
        {
            if (fragment == null) return;
            _fragments.Add(fragment);
            _fragmentnames.Add(name);
        }

        public override ICharSequence GetPageTitleFormatted(int position)
        {
            return new Java.Lang.String(_fragmentnames[position]);
        }
    }
}
  

Fragment1.cs

using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;

namespace OCR_Pro
{
    public class Fragment1 : Android.Support.V4.App.Fragment
    {
        public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Create your fragment here
        }

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            // Use this to return your custom view for this Fragment
            // return inflater.Inflate(Resource.Layout.YourFragment, container, false);

            var v = inflater.Inflate(Resource.Layout.Fragment1, container, false);
            return v;
        }
    }
}

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

2 个答案:

答案 0 :(得分:1)

您似乎在尝试传递类SupportFragmentManager而不是类的实例。尝试实例化SupportFragmentManager的实例,然后将其传递。

答案 1 :(得分:0)

请检查是否通过nuget软件包安装Xamarin.Android.Support.v4。 SupportFragmentManager类型为Android.Support.V4.App.FragmentManager。

如果您想获取样品,请看一下:

docs