从过时的FragementManager过渡到Android.Support.V4.App

问题描述 投票:0回答:1

我的应用程序中有几个DialogFragement,需要转换为Android.Support.V4.App.DialogFragment,因此我的应用程序与API 29(Q)兼容。我首先将“更改密码”对话框的片段代码转换为此:

class ChangePassword : Android.Support.V4.App.DialogFragment 
{
    public event DialogEventHandler Dismissed;
    public string selection = "";
    private int error = 0;
    public User MyUser;

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        base.OnCreateView(inflater, container, savedInstanceState);
        View view = inflater.Inflate(Resource.Layout.ChangePassword, container, false);
        Button ok = view.FindViewById<Button>(Resource.Id.button_ok);
        EditText currentPassword = view.FindViewById<EditText>(Resource.Id.currentPassword);
        EditText newPassword1 = view.FindViewById<EditText>(Resource.Id.newPassword1);
        EditText newPassword2 = view.FindViewById<EditText>(Resource.Id.newPassword2);
        ok.Click += (sender, args) =>
        {
            if (currentPassword.Text != "" && (newPassword1.Text.ToUpper () == newPassword2.Text.ToUpper()) && (newPassword1.Text != "" || newPassword2.Text != ""))
            {
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                OMLDataInterfaceWeb.OMLDataInterface datainterface = new OMLDataInterfaceWeb.OMLDataInterface();
                try
                {
                    datainterface.ChangeUserPassword(MyUser.Username, currentPassword.Text, newPassword1.Text);
                }
                catch (Exception e)
                {
                    error = 1;
                    Utils.showMessage(e.Message, "Error");
                }

                if (error == 0)
                {
                    selection = newPassword2.Text;
                    if (null != Dismissed)
                        Dismissed(this, new DialogEventArgs { Selection = selection });
                }
            }
            else
            {
                if (currentPassword.Text == "")
                {
                    // Android.Content.Context context = new AppContext() ;
                    Utils.showMessage("Enter the current password.","ERROR");
                }
                else
                {
                    if (newPassword1.Text == "")
                    {
                        Utils.showMessage("The first new password field is blank.", "ERROR");
                    } else if (newPassword2.Text == "")
                    {
                        Utils.showMessage("Please re-enter the new password.", "ERROR");

                    } else if (newPassword1.Text.ToUpper() != newPassword2.Text.ToUpper())
                    {
                        Utils.showMessage("The passwords do not match.", "ERROR");
                    }
                }
            }
        };

        return view;
    }
    public override void OnResume()
    {
        int width = 900; 
        int height = 900;
        Dialog.Window.SetLayout(width, height);
        base.OnResume();
    }
}

注意,我使用的是Android.Support.V4.App.DialogFragment,而不是Android.App.DialogFragment

我通过单击按钮从另一个活动中调用它,该代码是:

        btnChangePassword.Click += (sender, e) =>
        {
            Android.Support.V4.App.FragmentTransaction transcation = FragmentManager; //FragmentManager;    // FragmentManager.BeginTransaction();
            ChangePassword changePassword = new ChangePassword();
            changePassword.MyUser = MyUser;
            changePassword.Show(transcation, "Dialog");

            changePassword.Dismissed += (s, a) => {
                /* do something with e.Selection here */

                if (a.Selection.ToUpper() != "")
                {
                    ChangePassword _exportFragment = (ChangePassword)FragmentManager.FindFragmentByTag("Dialog");
                    if (_exportFragment != null)
                    {
                        _exportFragment.Dismiss();
                    }
                    changedPassword = true;
                   thePassword = a.Selection;
                }
                else
                {
                    Toast.MakeText(this, "Enter new password.", ToastLength.Long).Show();
                }
            };

        };

我在按钮点击代码中遇到两个错误,第一个是Cannot implicitly convert type 'Android.App.FragmentManager' to 'Android.Support.V4.App.FragmentTransaction,它发生在代码Android.Support.V4.App.FragmentTransaction transcation = FragmentManager;的这一行上,假设我还必须用FragmentManager来限定Android.Support.V4.App.,则我更改了代码要读取Android.Support.V4.App.FragmentTransaction transcation = Android.Support.V4.App.FragmentManager;,则会生成错误'FragmentManager' is a type, which is not valid in the given context,另一个错误Im得到的是Cannot convert type 'Android.App.Fragment' to 'MyAndroidApp.ChangePassword',该错误发生在行ChangePassword _exportFragment = (ChangePassword)FragmentManager.FindFragmentByTag("Dialog");上。我已经搜索了所有答案,但我尝试过的任何方法都无法解决。我想念什么?

c# android android-fragments android-dialogfragment
1个回答
0
投票

我能够弄清楚。新的按钮单击代码为:

        btnChangePassword.Click += (sender, e) =>
        {

            Android.Support.V4.App.FragmentTransaction transcation = SupportFragmentManager.BeginTransaction();
            ChangePassword changePassword = new ChangePassword();
            changePassword.MyUser = MyUser;
            changePassword.Show(transcation, "Dialog");

            changePassword.Dismissed += (s, a) => {
                /* do something with e.Selection here */

                if (a.Selection.ToUpper() != "")
                {
                    ChangePassword _exportFragment = (ChangePassword)SupportFragmentManager.FindFragmentByTag("Dialog");
                    if (_exportFragment != null)
                    {
                        _exportFragment.Dismiss();
                    }
                    changedPassword = true;
                   thePassword = a.Selection;
                }
                else
                {
                    Toast.MakeText(this, "Enter new password.", ToastLength.Long).Show();
                }
            };
© www.soinside.com 2019 - 2024. All rights reserved.