如何创建作为动态函数作为参数接收的函数?
我正在用Xamarin.android
创建一个动态对话框,我希望对话框在单击“操作”按钮时接收一个功能。
这是我的代码:
public static void ShowDialogBox(Context context, Function dynamicFunction)
{
Dialog popupDialog = new Dialog(context);
popupDialog.SetContentView(Resource.Layout.dialog_dynamic);
popupDialog.Window.SetSoftInputMode(SoftInput.AdjustResize);
popupDialog.Window.SetBackgroundDrawableResource(Android.Resource.Color.Transparent);
popupDialog.Show();
TextView title = popupDialog.FindViewById<TextView>(Resource.Id.dialog_title);
TextView content = popupDialog.FindViewById<TextView>(Resource.Id.dialog_content);
ImageView icon = popupDialog.FindViewById<ImageView>(Resource.Id.dialog_icon);
Button actionButton = popupDialog.FindViewById<Button>(Resource.Id.dialog_action_button);
popupDialog.FindViewById<ImageButton>(Resource.Id.dialog_close).Click += delegate { popupDialog.Dismiss(); };
popupDialog.FindViewById<Button>(Resource.Id.dialog_cancel_button).Click += delegate { popupDialog.Dismiss(); };
actionButton.Click += dynamicFunction;
}
*请注意,功能参数只是一个示例。另外,我将在此对话框中将文本和内容作为参数。
示例场景:
- 如果我将getCurrentDate函数发送到此对话框,则单击操作按钮时,它将获得当前日期。
- 如果我将OpenBluetooth函数发送到此对话框,当单击操作按钮时,它将打开蓝牙,
可以将另一个函数发送到此Showdialogbox
函数并在单击操作按钮时运行它?我的目的是使我的代码清洁且易于使用。
I'm creating a dynamic dialog box in xamarin.android
and I want the dialog box to receive a function that it will execute upon clicking the action button.
Heres my code:
public static void ShowDialogBox(Context context, Function dynamicFunction)
{
Dialog popupDialog = new Dialog(context);
popupDialog.SetContentView(Resource.Layout.dialog_dynamic);
popupDialog.Window.SetSoftInputMode(SoftInput.AdjustResize);
popupDialog.Window.SetBackgroundDrawableResource(Android.Resource.Color.Transparent);
popupDialog.Show();
TextView title = popupDialog.FindViewById<TextView>(Resource.Id.dialog_title);
TextView content = popupDialog.FindViewById<TextView>(Resource.Id.dialog_content);
ImageView icon = popupDialog.FindViewById<ImageView>(Resource.Id.dialog_icon);
Button actionButton = popupDialog.FindViewById<Button>(Resource.Id.dialog_action_button);
popupDialog.FindViewById<ImageButton>(Resource.Id.dialog_close).Click += delegate { popupDialog.Dismiss(); };
popupDialog.FindViewById<Button>(Resource.Id.dialog_cancel_button).Click += delegate { popupDialog.Dismiss(); };
actionButton.Click += dynamicFunction;
}
*Take note that the Function parameter is just an example. Also I will make the text and content in this dialog box a parameter.
Example scenario:
- If I send GetCurrentDate function to this dialog box, when the action button is clicked it will get the current date.
- If I send OpenBluetooth function to this dialog box, when the action button is clicked it will open the Bluetooth
It is possible to send another function to this ShowDialogBox
function and run it when the action button is clicked? My purpose here it to make my code clean and easy to use.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如@nico已经评论的那样,您可以使用
func
或委托作为参数。我建议您改用操作
。on
xamarin.forms.button
使用带有签名void onclicked的事件处理程序(对象?发送者,EventArgs e)
。因此,您的签名将使用action&lt; object?,eventargs&gt;
参数。需要注意的几件事:
null
的参数,然后将其分配给单击
>事件操作
不会返回任何内容。如果要“获取当前日期”,您的操作必须在字段中写入当前日期,或者>
一起包装函数调用发件人
和e
参数:showdialogbox(context,(sender,e)=&gt; myFunction(“其他数据”))
>As @Nico already commented, you can use
Func
or a delegate as a parameter. I'd suggest you useAction
instead.The
Clicked
event onXamarin.Forms.Button
uses an event handler with the signaturevoid OnClicked(object? sender, EventArgs e)
. Therefore, your signature would use aAction<object?, EventArgs>
parameter.A couple things to note:
null
before assigning it to theClicked
eventAction
does not return anything. If you want to "get the current date", your action must write the current date in a field or controlsender
ande
parameters:ShowDialogBox(context, (sender, e) => MyFunction("other data"))
对象方向可以帮助您:您可以将功能抽象成类,然后得出任何特定的功能类以实现您的特定操作。
类似以下内容:
进一步使用此方法,添加新功能非常容易:只需派生一个新类并覆盖
action()
方法Object orientation can help you: you can abstract your function into a class and then derive any specilized function class to implement your specific action.
Something like the following:
Further with this approach it's very easy to add new functions: just derive a new class and override the
Action()
method您可以在ActionClassBase中创建一个对话框,并在
action()
中调用dimply()
方法。例如,然后将对话框传递到
showdialogbox(上下文上下文,函数动态函数)
中的ActionClassBase。例如:You can create a Dialog in the ActionClassBase and call the
Dismiss()
method in theAction()
. Such asAnd then, pass the dialog to the ActionClassBase in the
ShowDialogBox(Context context, Function dynamicFunction)
. Such as: