简体   繁体   中英

Method inside method c# (Xamarin)

i am using a google guide to create place suggestions in my Android app made with Xamarin. I was wondering how would you convert this Java code to c#, and how to deal with the methods inside a method in c#? Thanks!

public Filter getFilter() {
    Filter filter = new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults filterResults = new FilterResults();
            if (constraint != null) {
                // Retrieve the autocomplete results.
                resultList = autocomplete(constraint.toString());

                // Assign the data to the FilterResults
                filterResults.values = resultList;
                filterResults.count = resultList.size();
            }
            return filterResults;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            if (results != null && results.count > 0) {
                notifyDataSetChanged();
            }
            else {
                notifyDataSetInvalidated();
            }
        }};
    return filter;
}

Just create a new named class extending or implementing Filter (depending on whether it's an interface or a class). Make it a nested class if you don't need it elsewhere.

For single-method interfaces, normally a delegate is the simplest way of representing the type - an idiom which Xamarin uses, I believe - and then you can use lambda expressions or anonymous methods to write the implementation "inline".

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM