I'm using Xamarin's ActionSheet Alert function and following the instruction from official website. The sample given by website is shown as
actionSheetAlert.AddAction(UIAlertAction.Create("Item One",UIAlertActionStyle.Default, (action) => Console.WriteLine ("Item One pressed.")));
After (action) =>
, it only shows how we can add one function here, which is (action) => Console.WriteLine ("Item One pressed.")
What if I want to add more actions? Can I just use (action) => {......}
? Or can I use (action) => function1()
? Could you please show me more examples that I can do after (action) =>
?
That's sample code for UIActionSheet, it should can help you.
using System;
using System.Collections.Generic;
using UIKit;
namespace TestActionSheet
{
public class SimpleSheet
{
public delegate void SelectedHandler(string selectedValue);
public event SelectedHandler Selected;
private UIActionSheet actionSheet;
public SimpleSheet(List<string> optionList)
{
actionSheet = new UIActionSheet("SheetTitle");
foreach (string str in optionList)
{
actionSheet.Add(str);
}
actionSheet.AddButton("Cancel");
actionSheet.Clicked += (sender, e) =>
{
if (e.ButtonIndex < actionSheet.ButtonCount - 1)
{
if (null != Selected)
Selected(optionList[(int)e.ButtonIndex]);
}
};
}
public void Show(UIView view)
{
actionSheet.ShowInView(view);
}
}
}
And invoke those code like this:
SimpleSheet sSheet = new SimpleSheet(new System.Collections.Generic.List<string>() { "option1", "option2" });
sSheet.Selected += (selectedValue) => {
Console.WriteLine("SelectedValue = "+selectedValue);
};
sSheet.Show(this.View);
Hope it can help you.
In short answer, you can do it in both way and achieve the same outcome.
actionSheetAlert.AddAction(UIAlertAction.Create("Item One",UIAlertActionStyle.Default, (action) => {
Console.WriteLine ("Item One pressed.");
Console.WriteLine (Date.UtcNow);
}));
or
function messageOutput(){
Console.WriteLine ("Item One pressed.");
Console.WriteLine (Date.UtcNow);
}
actionSheetAlert.AddAction(UIAlertAction.Create("ItemOne",UIAlertActionStyle.Default, (action) => messageOutput);
In detail answer, you question is not very clear what you are going to achieve. If it is about optimizations of inline function, you can refer to this question . Especially, you have mentioned your are using Mono(Xamarin) which has some other consideration.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.