如何创建一个简单的Xamarin.Forms项目视图

Tho*_*röm 0 c# xaml repeater xamarin.forms

如果您是Xamarin.Forms开发人员,那么您很可能遇到内置问题ListView.使用简单的转发器使用DataTemplate绑定ItemsSource会不会更容易?那正是我所想.

在SL/WPF中有一个ItemsControl像这样的工作 - 没有设计,没有选择,只是重复项目.

现在XLabs中有一个,但如果你不想要所有的软件包,这里有一个更简单的解决方案,基于QiMata的这篇文章.

工具:

  • Visual Studio 2015 @ Win 10(或在OS X上使用Xamarin Studio /)
  • Xamarin 4稳定(VS插件)
  • Xamarin.Forms 2.1.0.6529

Tho*_*röm 7

在Xamarin.Forms PCL项目中创建一个新类.我将我的名字命名为HliItemsView(因为"View"是XF中"Controls"的术语,而Hli是我的品牌).

粘贴此代码并根据需要进行修改.

我的观点基于a,ScrollView因为它是一个列表.这样项目就会自动滚动ListView.

using System;
using System.Collections;

using Xamarin.Forms;

namespace HLI.Forms.Controls
{
    public class HliItemsView : ScrollView
    {
        public static readonly BindableProperty ItemTemplateProperty = BindableProperty.Create(
            "ItemTemplate",
            typeof(DataTemplate),
            typeof(HliItemsView),
            null,
            propertyChanged: (bindable, value, newValue) => Populate(bindable));

        public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create(
            "ItemsSource",
            typeof(IEnumerable),
            typeof(HliItemsView),
            null,
            BindingMode.OneWay,
            propertyChanged: (bindable, value, newValue) => Populate(bindable));

        public IEnumerable ItemsSource
        {
            get
            {
                return (IEnumerable)this.GetValue(ItemsSourceProperty);
            }

            set
            {
                this.SetValue(ItemsSourceProperty, value);
            }
        }

        public DataTemplate ItemTemplate
        {
            get
            {
                return (DataTemplate)this.GetValue(ItemTemplateProperty);
            }

            set
            {
                this.SetValue(ItemTemplateProperty, value);
            }
        }

        private static void Populate(BindableObject bindable)
        {
            var repeater = (HliItemsView)bindable;

            // Clean
            repeater.Content = null;

            // Only populate once both properties are recieved
            if (repeater.ItemsSource == null || repeater.ItemTemplate == null)
            {
                return;
            }

            // Create a stack to populate with items
            var list = new StackLayout();

            foreach (var viewModel in repeater.ItemsSource)
            {
                var content = repeater.ItemTemplate.CreateContent();
                if (!(content is View) && !(content is ViewCell))
                {
                    throw new Exception($"Invalid visual object {nameof(content)}");
                }

                var view = content is View ? content as View : ((ViewCell)content).View;
                view.BindingContext = viewModel;

                list.Children.Add(view);
            }

            // Set stack as conent to this ScrollView
            repeater.Content = list;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)