具有android支持v4库的

时间:2016-08-31 15:42:39

标签: c# android xamarin alertdialog

我正在尝试创建自定义AlertDialog,但我有一个问题。

我的警告对话框类:

class AddNewDayAlert : DialogFragment
{
    public static AddNewDayAlert NewInstance(Bundle bundle)
    {
        AddNewDayAlert fragment = new AddNewDayAlert();
        fragment.Arguments = bundle;
        return fragment;
    }

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

        Button btn = view.FindViewById<Button>(Resource.Id.dialog_text);
        EditText txt = view.FindViewById<EditText>(Resource.Id.dialog_text);
        btn.Click += delegate
        {
            string day_name;
            if (txt.Text != "" && txt.Text != null)
            {
                day_name = txt.Text;
                Toast.MakeText(this.Activity, "Sėkmingai pridėta!!!", ToastLength.Long).Show();
                Dismiss();
            }

            else
                Toast.MakeText(this.Activity, "Pamiršote įvesti dienos pavadinimą!", ToastLength.Long).Show();
        };

        return view;
    }

}

问题在于:

        Android.Support.V4.App.FragmentTransaction ft = FragmentManager.BeginTransaction();
        Fragment prev = FragmentManager.FindFragmentByTag("dialog");
        if (prev != null)
        {
            ft.Remove(prev);
        }
        ft.AddToBackStack(null);

        AddNewDayAlert fragmentas = AddNewDayAlert.NewInstance(null);
        fragmentas.Show(ft, "dialog");
  

在最后一行中我收到错误,因为fragmentas.Show()正在使用Android.App.FragmentTransaction而我正在使用Android.Support.V4.App.FragmentTransaction

     

如果我尝试使用Android.App.FragmentTransaction,我会在第一行收到错误:   enter image description here

也许有办法转换?我该怎么办?

2 个答案:

答案 0 :(得分:0)

您有两个不同的FragmentTransaction类冲突。如果您决定使用Support v4 Fragments,那么您需要使用Support v4 FragmentTransaction

https://developer.android.com/reference/android/app/FragmentTransaction.html

https://developer.android.com/reference/android/support/v4/app/FragmentTransaction.html

如果沿着v4路线前进,您还需要确保使用AppCompatActivity(继承FragmentActivity并且引用Android.Support.V4.App.FragmentManager)。这样您就可以访问类型相同的SupportFragmentManager

EX:

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

        // Set our view from the "main" layout resource
        // SetContentView (Resource.Layout.Main);

        Android.Support.V4.App.FragmentTransaction ft = SupportFragmentManager.BeginTransaction();

    }
}

答案 1 :(得分:0)

您的AddNewDayAlert类还应继承自V4 DialogFragment

class AddNewDayAlert : Android.Support.V4.App.DialogFragment