简体   繁体   English

方法C#中的方法(Xamarin)

[英]Method inside method c# (Xamarin)

i am using a google guide to create place suggestions in my Android app made with Xamarin. 我正在使用Google指南在Xamarin制作的Android应用中创建位置建议。 I was wondering how would you convert this Java code to c#, and how to deal with the methods inside a method in c#? 我想知道如何将Java代码转换为c#,以及如何处理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). 只需创建一个新的命名类即可扩展或实现Filter (取决于它是接口还是类)。 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". 对于单方法接口,通常,委托是表示类型的最简单方法-我相信Xamarin使用的一个惯用法-然后您可以使用lambda表达式或匿名方法来编写“内联”实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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