protected virtual View CreateView(object value, View convertView, ViewGroup parent, DataTemplateProvider templateProvider, int defaultTemplate)
        {
            var valueView = value as View;
            if (valueView != null)
                return valueView;

            int? templateId = null;
            int id;
            if (templateProvider.TrySelectResourceTemplate(value, out id))
                templateId = id;
            else
            {
                object template;
                if (templateProvider.TrySelectTemplate(value, out template))
                {
                    if (template != null)
                    {
                        valueView = template as View;
                        if (valueView != null)
                        {
                            valueView.SetDataContext(value);
                            return valueView;
                        }
                        if (template is int)
                            templateId = (int)template;
                        else
                            value = template;
                    }
                }
                else
                    templateId = templateProvider.GetTemplateId();
            }

            if (templateId == null)
            {
                if (!(convertView is TextView))
                    convertView = LayoutInflater.Inflate(defaultTemplate, null);
                var textView = convertView as TextView;
                if (textView != null)
                    textView.Text = value.ToStringSafe("(null)");
                return textView;
            }
            var oldId = GetViewTemplateId(convertView);
            if (oldId == null || oldId.Value != templateId.Value)
                convertView = CreateView(value, parent, templateId.Value);
            convertView.SetDataContext(value);
            return convertView;
        }
        protected virtual View CreateView(object value, View convertView, ViewGroup parent, DataTemplateProvider templateProvider, int defaultTemplate)
        {
            var valueView = value as View;

            if (valueView != null)
            {
                return(valueView);
            }

            int?templateId = null;
            int id;

            if (templateProvider.TrySelectResourceTemplate(value, out id))
            {
                templateId = id;
            }
            else
            {
                object template;
                if (templateProvider.TrySelectTemplate(value, out template))
                {
                    if (template != null)
                    {
                        valueView = template as View;
                        if (valueView != null)
                        {
                            valueView.SetDataContext(value);
                            return(valueView);
                        }
                        if (template is int)
                        {
                            templateId = (int)template;
                        }
                        else
                        {
                            value = template;
                        }
                    }
                }
                else
                {
                    templateId = templateProvider.GetTemplateId();
                }
            }

            if (templateId == null)
            {
                if (!(convertView is TextView))
                {
                    convertView = LayoutInflater.Inflate(defaultTemplate, null);
                }
                var textView = convertView as TextView;
                if (textView != null)
                {
                    textView.Text = value.ToStringSafe("(null)");
                }
                return(textView);
            }
            var oldId = GetViewTemplateId(convertView);

            if (oldId == null || oldId.Value != templateId.Value)
            {
                convertView = CreateView(value, parent, templateId.Value);
            }
            convertView.SetDataContext(value);
            return(convertView);
        }