c# - Xamarin Android : How do i pass variables to a buttons event method in ExpandableListView?

标签 c# android xamarin xamarin.android

我有一个 ExpandableListView,其 subview 中有按钮。我想将事件处理程序与按钮的单击绑定(bind)。

这就是我想要实现的目标:

public void func1()
{
    // let say we have following arguments
    int arg1= 100;          
    SomeObject arg2 = new SomeObj("John", "1000");
    button.Click -= DeleteActionHandler;
    button.Click += DeleteActionHandler;     // this is point-1
}

DeleteActionHandler 如下:

void DeleteActionHandler(object s, EventArgs e)
{
   // some implementation
   // I want  arg1 and arg2 to be passed here.
}

问题是我想从point-1 传递一些参数到DeleteActionHandler 中。我可以在 EventArgs 中发送参数吗?或者有没有其他办法。

最佳答案

使用 xamarin studio 将变量传递给 xamarin.forms 中的按钮事件方法,请参阅下面的示例..

示例

public partial class StackOverflowTest : ContentPage
{
    public StackOverflowTest()
    {
        InitializeComponent();
        string strvalue = "Delete";
        BtnTest.Clicked += delegate (object sender2, EventArgs e2)
        {
            DeleteActionHandler(sender2, e2, strvalue);
        };
    }

    private void DeleteActionHandler(object sender, EventArgs e, string strval)
    {
        DisplayAlert("Greeting", $"{strval}!", "Ok");
    }

}

关于c# - Xamarin Android : How do i pass variables to a buttons event method in ExpandableListView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52535230/

相关文章:

c# - 在 Linq 中使用加入重新排序

c# - 我应该如何为争用最少的循环线程方案增加一个数字?

android - 套接字总是收到最后一个 udp 数据包

android - Google Playstore 使用相同的 key 发布不同的项目?

c# - Xamarin PCL 中缺少 Type.GetProperties()

c# - 用于简化 HQL 语法的正则表达式

c# - Mysql 多次addwithvalue : one returned value is enough

android - 新 Intent (Intent.ACTION_EDIT,ContactsContract.Profile.CONTENT_URI);不适用于某些设备,例如 Oppo、Galaxy J7-6.0.0

c# - Xamarin.Android 错误 : unauthorized access to “/system/lib/libsqlite.so”

c# - 更改默认超时