OnClickListener in c#

is there any way to convert this Java code:

`private void alert(final String title, final String message) {
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
alert.setTitle(title);
alert.setMessage(message);
alert.setCancelable(false);
alert.setNeutralButton(R.string.button_ok,
new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    try {
                        dialog.dismiss();
                    } catch (Exception e) {
                        if (CTC.DEBUG) {
                            Log.w(TAG, e);
                        }
                    }
                }

            });
    Util.wrapAlert(alert.   show());
}`

to be converted in xamarin. Everything is fine until i get to the OnClickListener block.

i have tryed alert.SetNeutralButton(Resource.String.button_ok, new OnPreferenceClickListener(this)), but ofcourse it does not work.

Thanks

Marius

Posts

  • Michael_ShapiroMichael_Shapiro USMember ✭✭✭

    Do you care to post the converted code ?

  • MariusTuguleaMariusTugulea USMember ✭✭

    Yes please, i do. I would like to have the C# Code instead of the Java one.

  • Michael_ShapiroMichael_Shapiro USMember ✭✭✭

    Show your c# code that you say doesn't work and we will figure it out :)

  • MariusTuguleaMariusTugulea USMember ✭✭

    @Michael_Shapiro,
    This is the C# conversion:

    private void alert(string title, string message)
            {
                AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.main_activity);
                alert.SetTitle(title);
                alert.SetMessage(message);
                alert.SetCancelable(false);
                alert.SetNeutralButton(Resource.String.button_ok, new OnPreferenceClickListener(this));
                Util.wrapAlert(alert.Show());
            }
    
  • Michael_ShapiroMichael_Shapiro USMember ✭✭✭

    What line does the converted code breaks on? I am not sure where you are calling it from, but if from your Activity then this line can have "this" reference: new AlertDialog.Builder(this);

    When you are setting the neutral button callback you are specifying a class. I do not know what that class is supposed to do, but I specify the callback procedure where I catch the click on the button:
    alert.SetNeutralButton(Resource.String.button_ok, MyButtonListener);

    I, then, have this listener procedure:

    private void MyButtonListener(object sender, DialogClickEventArgs e)
    {
        //do something
    }
    

    Not entirely sure what Util.wrapAlert does, but if you simply call alert.Show(); your code works just fine.

  • MariusTuguleaMariusTugulea USMember ✭✭

    I sorted it out eventually, thank you @Michael_Shapiro.

  • Michael_ShapiroMichael_Shapiro USMember ✭✭✭

    Whatever it was, I am glad you are ok now:)

Sign In or Register to comment.