コード例 #1
0
        FrameworkElement CreateView(object item)
        {
            Xamarin.Forms.View formsView = null;
            var bindingContext           = item;

            var dt   = bindingContext as Xamarin.Forms.DataTemplate;
            var view = bindingContext as View;

            // Support for List<DataTemplate> as ItemsSource
            if (dt != null)
            {
                formsView = (Xamarin.Forms.View)dt.CreateContent();
            }
            else
            {
                if (view != null)
                {
                    formsView = view;
                }
                else
                {
                    var selector = Element.ItemTemplate as Xamarin.Forms.DataTemplateSelector;
                    if (selector != null)
                    {
                        formsView = (Xamarin.Forms.View)selector.SelectTemplate(bindingContext, Element).CreateContent();
                    }
                    else
                    {
                        formsView = (Xamarin.Forms.View)Element.ItemTemplate.CreateContent();
                    }

                    formsView.BindingContext = bindingContext;
                }
            }

            formsView.Parent = this.Element;

            var element = formsView.ToWindows(new Xamarin.Forms.Rectangle(0, 0, ElementWidth, ElementHeight));

            if (dt == null && view == null)
            {
                formsView.Parent = null;
            }

            return(element);
        }