【问题标题】:Transitioning from the Obsolete FragementManager to Android.Support.V4.App从过时的 FragmentManager 过渡到 Android.Support.V4.App
【发布时间】:2020-03-11 22:17:17
【问题描述】:

我的应用中有多个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; 上,假设我还必须用 Android.Support.V4.App. 限定 FragmentManager,我更改了读取Android.Support.V4.App.FragmentTransaction transcation = Android.Support.V4.App.FragmentManager;的代码,这产生了错误'FragmentManager' is a type, which is not valid in the given context,我得到的另一个错误是Cannot convert type 'Android.App.Fragment' to 'MyAndroidApp.ChangePassword',它发生在ChangePassword _exportFragment = (ChangePassword)FragmentManager.FindFragmentByTag("Dialog");行上。我一直在寻找答案,但我尝试过的任何方法都没有解决这个问题。我错过了什么?

【问题讨论】:

  • 除其他问题外,您的大小写与正确的大小写不匹配(所有包名称都应为小写),FragmentManager 与 FragmentTransaction 不同,您应该真的转换为androidx 版本:developer.android.com/jetpack/androidx/releases/fragment。 support.v4 已过时且未维护。
  • 如果包名是指Android.Support.V4.App,那么对于 Visual Studio 2019 来说不是这样。也许我应该指出我的开发平台。

标签: c# android android-fragments android-dialogfragment


【解决方案1】:

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

        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();
                }
            };

【讨论】:

    猜你喜欢
    • 2015-05-07
    • 2013-02-24
    • 2016-08-26
    • 2019-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    相关资源
    最近更新 更多