如何创建作为动态函数作为参数接收的函数?

发布于 2025-01-26 08:39:10 字数 1445 浏览 2 评论 0原文

我正在用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;
}

*请注意,功能参数只是一个示例。另外,我将在此对话框中将文本和内容作为参数。

示例场景:

  1. 如果我将getCurrentDate函数发送到此对话框,则单击操作按钮时,它将获得当前日期。
  2. 如果我将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:

  1. If I send GetCurrentDate function to this dialog box, when the action button is clicked it will get the current date.
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

您的好友蓝忘机已上羡 2025-02-02 08:39:10

正如@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 use Action instead.

The Clicked event on Xamarin.Forms.Button uses an event handler with the signature void OnClicked(object? sender, EventArgs e). Therefore, your signature would use a Action<object?, EventArgs> parameter.

A couple things to note:

  • If you don't use nullable reference types in your project, omit the question marks
  • You need to check the parameter for null before assigning it to the Clicked event
  • Invoking an Action does not return anything. If you want to "get the current date", your action must write the current date in a field or control
  • If your function needs other parameters, you need to pass a delegate (or wrap your function call in a function with the sender and e parameters: ShowDialogBox(context, (sender, e) => MyFunction("other data"))
合约呢 2025-02-02 08:39:10

对象方向可以帮助您:您可以将功能抽象成类,然后得出任何特定的功能类以实现您的特定操作。
类似以下内容:

abstract class ActionClassBase
{
    public abstract void Action();

    private string data = "";
    public string Data { get { return data; } set { data = value; } }
}

class GetCurrentDateAction : ActionClassBase
{
    public override void Action()
    {
        Data = DateTime.Now.ToString();
    }
}

class OpenBlueToothAction : ActionClassBase
{
    public override void Action()
    {
        Data = "BlueTooth opened";
    }
}


public static void ShowDialogBox(Context context, ActionClassBase action)
{
    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 += (sender, ev) => action.Action();
}

进一步使用此方法,添加新功能非常容易:只需派生一个新类并覆盖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:

abstract class ActionClassBase
{
    public abstract void Action();

    private string data = "";
    public string Data { get { return data; } set { data = value; } }
}

class GetCurrentDateAction : ActionClassBase
{
    public override void Action()
    {
        Data = DateTime.Now.ToString();
    }
}

class OpenBlueToothAction : ActionClassBase
{
    public override void Action()
    {
        Data = "BlueTooth opened";
    }
}


public static void ShowDialogBox(Context context, ActionClassBase action)
{
    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 += (sender, ev) => action.Action();
}

Further with this approach it's very easy to add new functions: just derive a new class and override the Action() method

小苏打饼 2025-02-02 08:39:10

您可以在ActionClassBase中创建一个对话框,并在action()中调用dimply()方法。例如

abstract class ActionClassBase
{
  public  Dialog popupDialog;
  public abstract void Action();

  private string data = "";
  public string Data { get { return data; } set { data = value; } }
  }

class GetCurrentDateAction : ActionClassBase
{
  public override void Action()
  {
    Data = DateTime.Now.ToString();
    base.popupDialog.Dismiss();
  }
}

class OpenBlueToothAction : ActionClassBase
{
  public override void Action()
  {
    Data = "BlueTooth opened";
    base.popupDialog.Dismiss();
  }
}

,然后将对话框传递到showdialogbox(上下文上下文,函数动态函数)中的ActionClassBase。例如:

Dialog popupDialog = new Dialog(context);
popupDialog.SetContentView(Resource.Layout.dialog_dynamic);
popupDialog.Window.SetSoftInputMode(SoftInput.AdjustResize);
popupDialog.Window.SetBackgroundDrawableResource(Android.Resource.Color.Transparent);
dynamicFunction.popupDialog = popupDialog;

You can create a Dialog in the ActionClassBase and call the Dismiss() method in the Action(). Such as

abstract class ActionClassBase
{
  public  Dialog popupDialog;
  public abstract void Action();

  private string data = "";
  public string Data { get { return data; } set { data = value; } }
  }

class GetCurrentDateAction : ActionClassBase
{
  public override void Action()
  {
    Data = DateTime.Now.ToString();
    base.popupDialog.Dismiss();
  }
}

class OpenBlueToothAction : ActionClassBase
{
  public override void Action()
  {
    Data = "BlueTooth opened";
    base.popupDialog.Dismiss();
  }
}

And then, pass the dialog to the ActionClassBase in the ShowDialogBox(Context context, Function dynamicFunction). Such as:

Dialog popupDialog = new Dialog(context);
popupDialog.SetContentView(Resource.Layout.dialog_dynamic);
popupDialog.Window.SetSoftInputMode(SoftInput.AdjustResize);
popupDialog.Window.SetBackgroundDrawableResource(Android.Resource.Color.Transparent);
dynamicFunction.popupDialog = popupDialog;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文