0

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) => ?

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523

2 Answers2

1

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.

Alanc Liu
  • 1,294
  • 10
  • 15
  • Thanks for your answer. It helps. And I also want to ask, – Alice King Dec 20 '16 at 16:57
  • Thanks for your answer. It helps. And I also want to ask, is (null != Selected) same with (Selected != null)? If not ,what does it mean? Thx : ) – Alice King Dec 20 '16 at 16:58
  • Nice question, actually there is no different. That's my habit, because my first programing language is C. For C compiler, it can support the condition like "if(a = 1)" and it means that is true, so to prevent the mistake like " a = 1 "(the correct one is " a == 1"), I prefer to use " 1 == a", if I make a mistake like " 1 = a " the compiler will give me an error, it can help me to check it. – Alanc Liu Dec 21 '16 at 03:03
  • Glad to help you and you can mark it as answer if you think it helps. – Alanc Liu Dec 21 '16 at 03:05
0

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.

Community
  • 1
  • 1
Kuroro
  • 1,841
  • 1
  • 19
  • 32