当前上下文中不存在名称“getSupportFragmentManager”
我的代码:
using Android.Views;
using Android.OS;
using Android.Support.V4.App;
using com.refractored;
using Android.Support.V4.View;
namespace XamarinStore
{
public class HomeFragment : Android.App.Fragment
{
BadgeDrawable basketBadge;
int badgeCount;
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
RetainInstance = true;
SetHasOptionsMenu(true);
// Create your fragment here
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var HomePage = inflater.Inflate(Resource.Layout.HomeLayout, container, false);
var pager = HomePage.FindViewById<ViewPager>(Resource.Id.pager);
pager.Adapter = new MyPagerAdapter(SupportFragmentManager);
var tabs = HomePage.FindViewById<PagerSlidingTabStrip>(Resource.Id.tabs);
tabs.SetViewPager(pager);
return HomePage;
}
}
public class MyPagerAdapter : FragmentPagerAdapter{
private string[] Titles = {"Categories", "Home", "Top Paid", "Top Free", "Top Grossing", "Top New Paid",
"Top New Free", "Trending"};
public MyPagerAdapter(Android.Support.V4.App.FragmentManager fm) : base(fm)
{
}
public override Java.Lang.ICharSequence GetPageTitleFormatted (int position)
{
return new Java.Lang.String (Titles [position]);
}
#region implemented abstract members of PagerAdapter
public override int Count {
get {
return Titles.Length;
}
}
#endregion
#region implemented abstract members of FragmentPagerAdapter
public override Android.Support.V4.App.Fragment GetItem (int position)
{
return SuperAwesomeCardFragment.NewInstance (position);
}
#endregion
}
}
如何在片段中访问getSupportFragmentManager。
我正在使用xamarin跨平台开发工具..借助此来源的示例项目https://xamarin.com/c-sharp-shirt
在演示项目中,如果 BackStackEntryCount 计数等于零,则将屏幕切换到另一个片段..所以我用新的homefragment替换代码我决定在那里添加标签..所以我试图使用此组件“Material Pager Sliding Tab Strip”..在使用该组件时,它会因此错误而停止。 当前上下文中不存在名称'getSupportFragmentManager'
答案 0 :(得分:3)
首先我继承HomeFragment
到Android.Support.V4.App.Fragment
然后我使用ChildFragmentManager
代替SupportFragmentManager
pager.Adapter = new MyPagerAdapter(ChildFragmentManager);
答案 1 :(得分:1)
在Xamarin Android中,您应该能够使用FragmentManager
访问此内容,并且您需要继承Android.Support.V4.App.Fragment
像这样:
public class HomeFragment : Android.Support.V4.App.Fragment
{
BadgeDrawable basketBadge;
int badgeCount;
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
RetainInstance = true;
SetHasOptionsMenu(true);
var count = FragmentManager.BackStackEntryCount;
// Create your fragment here
}
Xamarin中的 getActivity
等效项为Activity
(例如,在继承自Android.Support.V4.App.Fragment
的类中使用时)
答案 2 :(得分:1)
你也可以这样做:
using Fragment = Android.Support.V4.App.Fragment;
using FragmentManager = Android.Support.V4.App.FragmentManager;
public class YourFragment : Fragment
{
//...
//To access to FragmentManager use base.FragmentManager
//Example:
base.FragmentManager.BeginTransaction()
.Replace(Resource.Id.content_frame, fragment)
.Commit();
//...
}
前两行(使用......)非常有用 您只能使用支持中的Fragment和FragmentManager 并且你已经清楚不要将它们与普通的Fragment和FragmentManager混合使用。
答案 3 :(得分:0)
我不熟悉 Xamarin ,但在原生Android开发中,您无法使用FragmentManager
或Fragment
方法从getFragmentManager
访问getSupportFragmentManager
。从 API 17 ,您可以调用getChildFragmentManager
方法获取FragmentManager
实例,该实例允许您管理来自其他片段的片段。
getChildFragmentManager().beginTransaction().add(...).commit();
getChildFragmentManager().executePendingTransactions();
答案 4 :(得分:0)
public class HomeFragment:FragmentActivity
使用FragmentActivity扩展您的课程将解决您的问题