YesNo Button单击DialogFragment上的处理程序[Mono for Android]

时间:2013-04-12 03:35:28

标签: android mono fragment android-alertdialog dialogfragment

我有一个ListFragment,上面有复选框......

public class ViewOrderLineFragment : ListFragment {
      public override bool OnOptionsItemSelected(IMenuItem item){
            switch (item.TitleFormatted.ToString()){
                case "Add":
                   //stuff here
                    break;
                case "Edit":
                        var dialog = new ConfirmationFragment(Activity);
                        dialog.Show(FragmentManager, null);
                    break;
            }

            return base.OnOptionsItemSelected(item);
        }
       public void DoPositiveClick(){=
           DeletedSelectedItems();=
        }

        public void DoNegativeClick()={
            // Do stuff here.
            //Log.Info("FragmentAlertDialog", "Negative click!");
        }

        void DeletedSelectedItems={
           //do stuff here
         }
}

当我选择一些项目并按下删除按钮时,它会提示用户“你确定吗?”

这是我的DialogFragment代码。

public class ConfirmationFragment : DialogFragment { 
Context _context;
        public ConfirmationFragment(Context context) {
            _context = context;   
        }
public override Dialog OnCreateDialog(Bundle savedState){
 return new AlertDialog.Builder(Activity)
                //.SetIcon(Resource.Drawable.alert_dialog_icon)
                //.SetTitle(title)
            .SetPositiveButton("Yes", (sender, e) =>
            {
                ((ViewOrderLineFragment)Activity).DoPositiveClick();
            })
            .SetNegativeButton("No", (sender, e) =>
            {
                ((ViewOrderLineFragment)Activity).DoNegativeClick();
            }).Create();
        }
}

错误: Cannot convert type 'Android.App.Activity' to 'OTCMobile.Screens.ViewOrderLineFragment'

在线((ViewOrderLineFragment)Activity).DoPositiveClick();

我按照此示例this link

任何其他解决方案?

1 个答案:

答案 0 :(得分:0)

我废弃了ConfirmationFragment ..

这对我有用。

public override bool OnOptionsItemSelected(IMenuItem item) {
        switch (item.TitleFormatted.ToString()) {
            case "Add":
               //some stuff
                break;
            case "Edit":
              var confirmDialog = new AlertDialog.Builder(this.Activity)
                        //.SetIcon(Resource.Drawable.alert_dialog_icon)
                        .SetTitle("Are you sure you want to delete all selected?")
                    .SetPositiveButton("Yes", (sender, e) =>
                    {
                        DoPositiveClick();
                    })
                    .SetNegativeButton("No", (sender, e) =>
                    {
                        DoNegativeClick();
                    }).Create();
                    confirmDialog.Show();

                }
                break;
        }
        return base.OnOptionsItemSelected(item);
    }