C# 碎片活动和碎片。。。在这种情况下我如何工作?

C# 碎片活动和碎片。。。在这种情况下我如何工作?,c#,android-fragments,xamarin.android,xamarin,android-fragmentactivity,C#,Android Fragments,Xamarin.android,Xamarin,Android Fragmentactivity,我真的想不出一个好的题目。很抱歉 我的问题是: 我有我的主要活动: using System; using Android.App; using Android.Content; using Android.Runtime; using Android.Views; using Android.Widget; using Android.OS; using Android.Support.V4.App; namespace AndroidApplication2 {

我真的想不出一个好的题目。很抱歉

我的问题是: 我有我的主要活动:

using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Support.V4.App;

    namespace AndroidApplication2
    {
        [Activity(Label = "AndroidApplication2", MainLauncher = true, Icon = "@drawable/icon")]
        public class Activity1 : FragmentActivity
        {
            protected override void OnCreate(Bundle bundle)
            {
                base.OnCreate(bundle);

                // Set our view from the "main" layout resource
                SetContentView(Resource.Layout.Main);          
                //Not sure if I need the code below
                FragmentTransaction ft = this.SupportFragmentManager.BeginTransaction();
                Activity2 fragment = new Activity2();
                ft.Add(Resource.Id.fragmentContainer, fragment);
                ft.Commit();
            }
        }
    }
主活动的XML文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <fragment
        class="AndroidApplication2.Activity2"
        android:id="@+id/fragmentContainer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/changeFragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Change Fragment" />
</LinearLayout>
fragment_layout.axml如下所示:

namespace AndroidApplication2
{
    [Activity(Label = "My Activity")]
    public class Activity2 : Fragment
    {
        public override View OnCreateView(LayoutInflater p0, ViewGroup p1, Bundle p2)
        {
            return p0.Inflate(Resource.Layout.fragment_layout, p1, false);
        }
    }
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/myTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is a TextView from the fragment" />
    <Button
        android:id="@+id/myButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is a Button from the fragment" />
</LinearLayout>
_btnAboutUs = (Button)FindViewById (Resource.Id.btnAboutUs);

这段代码是用C#编写的,使用的是Xamarin

现在,我只想访问fragment_布局中的按钮,并使用该按钮替换Activity1(我的主要活动)中的片段。我用它来避免为每一件简单的事情创建新的活动,比如显示某件事情的细节,但我也不能使用任何弹出窗口

我无法访问在我的主要FragmentActivity中加载的片段上的按钮,我可以用另一个片段替换它。救命啊!谢谢任何其他改进或建议也都欢迎。

“但我仍然不知道如何访问我添加的新片段的按钮。”

你可以像这样引用这个按钮:

namespace AndroidApplication2
{
    [Activity(Label = "My Activity")]
    public class Activity2 : Fragment
    {
        public override View OnCreateView(LayoutInflater p0, ViewGroup p1, Bundle p2)
        {
            return p0.Inflate(Resource.Layout.fragment_layout, p1, false);
        }
    }
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/myTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is a TextView from the fragment" />
    <Button
        android:id="@+id/myButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is a Button from the fragment" />
</LinearLayout>
_btnAboutUs = (Button)FindViewById (Resource.Id.btnAboutUs);
但也许在片段中使用按钮来替换按钮本身所在的片段并不是最好的方法。
为什么不将按钮放在Activity1中,这样您就可以使用它的fragmentManager来替换片段。片段没有fragmentManager,但活动有。

尝试通过首先打开视图来访问按钮,然后返回它。 例如:

View view = p0.Inflate(Resource.Layout.fragment_layout, p1, false);

// now we can access button, textview etc... in the layout

_btnAboutUs = (Button)view.FindViewById(Resource.Id.btnAboutUs);

return view;

好了,我的一个问题解决了。我无法替换静态添加的片段。但我仍然不知道如何访问我添加的新片段的按钮。请帮帮我。谢谢