Xamarin Android - 从Fragment开始FragmentActivity

时间:2014-10-29 13:35:42

标签: android android-fragments xamarin android-fragmentactivity

我有一个按钮点击事件的片段,单击此按钮我需要启动FragmentActivity,但无论我尝试做什么,我似乎都会遇到错误。

这是片段中的按钮点击事件:

myExhibitGallery.Click += delegate {

    //StartActivity(typeof(MyExhibitHistoryActivity));

    Intent intent = new Intent(MyExhibitHistoryActivity);
    StartActivity(intent);

正如您所看到的,我尝试使用两种不同的方法(intent和StartActivity),但两者都抛出错误。

这是我想要开始的FragmentActivity:

[Activity (Label = "My Exhibit History")]           
    public class MyExhibitHistoryActivity : FragmentActivity
    {
        ViewPager _viewPager;

        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);

            SetContentView (Resource.Layout.MyExhibitHistory);

            _viewPager = FindViewById<ViewPager> (Resource.Id.viewPager);
        }
    }

    public class ViewFragmentAdapter: FragmentPagerAdapter
    {
        public ViewFragmentAdapter (Android.Support.V4.App.FragmentManager fm) : base (fm)
        {
        }

        public override int Count {
            get { return 5; }
            }



        public override Android.Support.V4.App.Fragment GetItem (int position)
        {
            return new viewerFragment();
        }
    }


    public class viewerFragment: Android.Support.V4.App.Fragment
    {
        public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            var view = inflater.Inflate (Resource.Layout.myExhibitHistoryItem, container, false);

            return view;
        }
    }

有人能告诉我启动FragmentActivity的正确方法吗?

1 个答案:

答案 0 :(得分:2)

在片段中添加一个使用:

using Android.Content;

添加一个处理事件的函数,如下所示:

void StartMyExhibitHistoryActivity (object sender, EventArgs e)
{ 
    var myExhibitHistoryActivity = new Intent (this.Activity, typeof(MyExhibitHistoryActivity));
    StartActivity (MyExhibitHistoryActivity);
}

最后添加Click事件:

myExhibitGallery.Click += StartMyExhibitHistoryActivity;