简体   繁体   English

Xamarin Android dialog.PositiveButtonClicked 不工作

[英]Xamarin Android dialog.PositiveButtonClicked not working

I would like to have some code run (use Intent to go to new Activity) when the user clicks the Ok button in a dialog (DialogFragment)当用户单击对话框中的“确定”按钮(DialogFragment)时,我想运行一些代码(使用 Intent to go to new Activity)

I see in the debugger it hits the line of code for.Show.我在调试器中看到它命中了 for.Show 的代码行。 But it seems to skip past the PositiveButtonClicked and just go to the lines of code after that.但它似乎跳过了 PositiveButtonClicked 和 go 之后的代码行。

Why is PositiveButtonClicked not working?为什么 PositiveButtonClicked 不起作用?

I've tried adding extra lines before and after, as well as inside.我试过在前后以及内部添加额外的行。 Ive double checked that the dialog has a positivebutton set to "Ok".I also have other dialogs with PositiveButtonClicked that work on this Activity, so im not sure why this one doesnt work我仔细检查了对话框是否有一个 positivebutton 设置为“Ok”。我还有其他与 PositiveButtonClicked 的对话框可以在这个 Activity 上工作,所以我不确定为什么这个不起作用

C#: C#:

private async Task ProcessExistingFamilyMembers(AccountAddFamilyMemberState state)
{
    
    cafdialog.Show(this.FragmentManager, "cafdialog");
    cafdialog.PositiveButtonClicked += (args1) =>
    { 
        Intent intent = new Intent(this, typeof(OrderSummaryActivity));
        intent.PutExtra<AccountSubscriptionInfo>("account_subscription_info", info);
        SubscriptionChangeInfo changeInfo = new SubscriptionChangeInfo() { Last4ofCC = aafmi.Last4ofCC, NextBillingDate = aafmi.NextBillingDate };
        intent.PutExtra<SubscriptionChangeInfo>("subscription_change_info", changeInfo);
        StartActivity(intent);
        Finish();
    };
}

You can create AlertDialogFragment class that inherits DialogFragment :您可以创建继承 DialogFragment 的DialogFragment

[Obsolete]
    public class AlertDialogFragment : DialogFragment
    {
        [Obsolete]
        public override Dialog OnCreateDialog(Bundle savedInstanceState)
        {
            var builder = new AlertDialog.Builder(Activity)
                .SetMessage("This is my dialog.")
                .SetPositiveButton("Ok", (sender, args) =>
                {
                    //use Intent to go to new Activity
                    var intent = new Intent(Context, typeof(Activity1));
                    StartActivity(intent);
                })
                .SetTitle("Custom Dialog");
            return builder.Create();
        }
    }

In MainActivity.cs, call ShowDialog() in OnCreate():在 MainActivity.cs 中,在 OnCreate() 中调用 ShowDialog():

...
[System.Obsolete]
#pragma warning disable CS0809 // Obsolete member overrides non-obsolete member
        protected override void OnCreate(Bundle savedInstanceState)
#pragma warning restore CS0809 // Obsolete member overrides non-obsolete member
        {
            ...
            ShowDialog();
        }

       [System.Obsolete]
        public void ShowDialog()
        {
            var transaction = FragmentManager.BeginTransaction();
            var dialogFragment = new AlertDialogFragment();
            dialogFragment.Show(transaction, "dialog_fragment");
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM