我试图通过处理程序内的发件人对象访问按钮。但是,我一直得到这个例外:
指定的演员表无效。
我知道这是一个简单的问题,但有人可以帮我看看我错过了什么吗?它也没有特别的标签,我只想知道点击了哪个按钮。
这是我得到例外的一行:
var tag = ((UIButton)sender).Tag;
protected void Handle_FinishedPickingMedia (object sender, UIImagePickerMediaPickedEventArgs e)
{
try{
//determine what was selected, video or image
bool isImage = false;
switch(e.Info [UIImagePickerController.MediaType].ToString()) {
case "public.image":
Console.WriteLine("Image selected");
isImage = true;
break;
}
// if it was an image, get the other image info
if(isImage) {
// get the original image
UIImage originalImage = e.Info[UIImagePickerController.OriginalImage] as UIImage;
if(originalImage != null) {
// do something with the image
new Thread(new System.Threading.ThreadStart(() => {
Thread.Sleep(350);
BeginInvokeOnMainThread (() => {
var tag = ((UIButton)sender).Tag;
//UIButton senderButton = (UIButton)sender;
switch(tag)
{
case 0:
// do something here
break;
case 1:
// do something here
break;
});
})).Start();
}
}
// dismiss the picker
imagePicker.DismissModalViewController (true);
}catch(Exception ex)
{
ShowAlert ("Failed !", "Unable to select image", "");
Console.WriteLine(ex.Message + ex.StackTrace);
}
}
答案 0 :(得分:0)
尝试这样的事情:
UIButton button = sender as UIButton;
var tag = button.tag;
你也可以为"按钮"添加一些检查。是空的,例如。发件人不是UIButton
答案 1 :(得分:0)
根据Jason的上述建议,UIImagePickerController是发送者,而不是启动选择器的按钮。这就是我解决问题的方法:
创建了一个全局标记变量
int tag { get; set; }
在每个按钮的点击处理程序中指定了标记值。例如:
button.TouchUpInside += (object sender, EventArgs e) => {
tag = 0;
// Do other stuff here
};
在switch语句中使用标记变量。