Roman Nazarevych
Roman Nazarevych

Reputation: 7703

How to Implement Android Interface in Xamarin

How to implement android interface in xamarin where there is no delegate alternative. Like this ListPopupWindow.SetOnItemClickListener(AdapterView.IOnItemClickListener clickListener) ?

Upvotes: 1

Views: 2727

Answers (1)

Roman Nazarevych
Roman Nazarevych

Reputation: 7703

There is god document on xamarin official site which addresses this topic Android Callable Wrappers The main thing to remember is to subclass Java.Lang.Object.

I also made my own wrapper so that I can reuse it in my code using delegates.

public class OnItemClickListener : Object, AdapterView.IOnItemClickListener{
        public delegate void ItemClick(AdapterView parent, View view, int position, long id);

        public OnItemClickListener(ItemClick itemClickDelegate){
            ItemClickDelegate = itemClickDelegate;
        }

        public ItemClick ItemClickDelegate { get; }

        public void OnItemClick(AdapterView parent, View view, int position, long id){
            ItemClickDelegate(parent, view, position, id);
        }
    }

And usage:

new CallbackWrapers.OnItemClickListener(
                delegate (AdapterView parent, View view1, int position, long id) { 
                     //Do your stuff
                }));

Upvotes: 5

Related Questions