我正在尝试使用界面处理对话框按钮。我在android中得到了这个解决方案 - java:
1)声明了一个接口:
public interface DialogPopUpListener {
void onConfirmButtonClick();
void onCancelButtonClick();
}
2)实施对话框:
public static void genericCreatePopUp(final Context context, final SimpleDialogPopUpListener listener) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Title");
builder.setMessage("Message");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// do nothing to ignore autoclose
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// do nothing to ignore autoclose
}
});
final AlertDialog dialog = builder.create();
dialog.show();
//Overriding the handler immediately after show POSITIVE
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onConfirmButtonClick("");
dialog.dismiss();
}
});
//Overriding the handler immediately after show NEGATIVE
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onCancelButtonClick();
dialog.dismiss();
}
});
}
3)调用功能显示对话框:
GenericPopUpDialogSimple.genericCreatePopUp(context, new SimpleDialogPopUpListener() {
@Override
public void onConfirmButtonClick(String editTxtInput) {
//handle positive button
}
@Override
public void onCancelButtonClick() {
//handle negative buton
}
});
现在我想在C#中迁移这个功能:
1)声明了相同的界面;
2)实施对话框创建:
public async void createDialog( SimpleDialogPopUpListener listener) {
var dialog = new MessageDialog("Message", "Title");
// Add positive command and set callback
dialog.Commands.Add(new UICommand(btnOkTxt, UICommandInvokedHandler => {
listener.onConfirmButtonClick();
}) { Id = 0 });
// Add negative command and set callback
dialog.Commands.Add(new UICommand(btnCancelTxt, UICommandInvokedHandler => {
listener.onCancelButtonClick();
}) { Id = 1 });
// Show the message dialog
await dialog.ShowAsync();
}
3)这是问题,如何调用此方法并覆盖接口方法?
答案 0 :(得分:0)
在C#中有点不同,因为它不支持内联类作为Java。一种解决方案是定义一个实现接口的类,并在调用方法时将该类用作参数。所以你有:
public class MyImplementor : DialogPopUpListener {
public void onConfirmButtonClick() {
Debug.WriteLine("T E S T --> ok");
}
public void onCancelButtonClick() {
Debug.WriteLine("T E S T --> cancel");
}
}
你打电话给你的方法:
createDialog(new MyImplementor());