我正在尝试在我的xamarin安卓中创建一个选项卡式滑动导航系统 我面临的 名称"支持片段管理器"在当前上下文中不存在,错误我不知道如何清除它 我是 xamarin android 的新手,请帮助我
主要活动.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;
}
页面适配器1.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]);
}
}
}
片段1.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;
}
}
}
错误:名称"SupportFragmentManager"在当前上下文中不存在
看起来您正在尝试传入类SupportFragmentManager
而不是类的实例。 尝试实例化SupportFragmentManager
的实例,然后将其传入。
请检查是否通过nuget包安装Xamarin.Android.Support.v4。SupportFragmentManager 类型是 Android.Support.V4.App.FragmentManager。
如果您想获得样品,请查看:
https://learn.microsoft.com/en-us/xamarin/android/user-interface/controls/view-pager/viewpager-and-fragments