我使用Xamarin的ActionSheet Alert功能并遵循官方网站的指示。网站提供的样本显示为
actionSheetAlert.AddAction(UIAlertAction.Create("Item One",UIAlertActionStyle.Default, (action) => Console.WriteLine ("Item One pressed.")));
在(action) =>
之后,它只显示我们如何在这里添加一个函数,即(action) => Console.WriteLine ("Item One pressed.")
如果我想添加更多动作怎么办?我可以使用(action) => {......}
吗?或者我可以使用(action) => function1()
吗?你能否告诉我更多可以在(action) =>
之后做的例子?
答案 0 :(得分:0)
简而言之,你可以两种方式做到并取得同样的结果。
actionSheetAlert.AddAction(UIAlertAction.Create("Item One",UIAlertActionStyle.Default, (action) => {
Console.WriteLine ("Item One pressed.");
Console.WriteLine (Date.UtcNow);
}));
或
function messageOutput(){
Console.WriteLine ("Item One pressed.");
Console.WriteLine (Date.UtcNow);
}
actionSheetAlert.AddAction(UIAlertAction.Create("ItemOne",UIAlertActionStyle.Default, (action) => messageOutput);
详细回答,你的问题不是很清楚你要实现的目标。如果是关于内联函数的优化,可以参考这个question。特别是,你提到你正在使用Mono(Xamarin),还有一些其他考虑因素。
答案 1 :(得分:0)
UIActionSheet的示例代码,应该可以帮到你。
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);
}
}
}
并调用这样的代码:
SimpleSheet sSheet = new SimpleSheet(new System.Collections.Generic.List<string>() { "option1", "option2" });
sSheet.Selected += (selectedValue) => {
Console.WriteLine("SelectedValue = "+selectedValue);
};
sSheet.Show(this.View);
希望它可以帮到你。