У меня в приложении несколько 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
, другая ошибка, которую я получаю, - Cannot convert type 'Android.App.Fragment' to 'MyAndroidApp.ChangePassword'
, которая происходит в строке ChangePassword _exportFragment = (ChangePassword)FragmentManager.FindFragmentByTag("Dialog");
, Я искал ответ на все вопросы, и ничего, что я пытался решить эту проблему. Чего мне не хватает?