C# 如何创建简单的Xamarin.Forms项目视图

C# 如何创建简单的Xamarin.Forms项目视图,c#,xaml,xamarin.forms,repeater,C#,Xaml,Xamarin.forms,Repeater,如果您是Xamarin.Forms开发人员,您很可能在内置的ListView中遇到问题。使用简单的转发器使用数据模板绑定ItemsSource不是更容易吗?我就是这么想的 在SL/WPF中有一个ItemsControl,它的工作原理就是这样的-没有设计,没有选择,只是重复项目 现在XLabs中有一个,但是如果您不想要所有的软件包,这里有一个基于QiMata的更简单的解决方案 工具: Visual Studio 2015@Win 10(或在OS X上使用Xamarin Studio) Xamar

如果您是Xamarin.Forms开发人员,您很可能在内置的
ListView
中遇到问题。使用简单的转发器使用数据模板绑定ItemsSource不是更容易吗?我就是这么想的

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

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

工具:

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

在Xamarin.Forms PCL项目中创建一个新类。我将我的名字命名为HliItemsView(因为“视图”是XF中“控件”的术语,Hli是我的品牌)

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

我的视图基于
滚动视图
,因为它是一个列表。这样,项目将自动滚动,就像
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;
        }
    }
}

在Xamarin.Forms PCL项目中创建一个新类。我将我的名字命名为HliItemsView(因为“视图”是XF中“控件”的术语,Hli是我的品牌)

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

我的视图基于
滚动视图
,因为它是一个列表。这样,项目将自动滚动,就像
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;
        }
    }
}

WPF中的Xaml和Xamarin之间存在一些差异,您可以看到

但是,您可以在Xamarin GitHub repo中找到一个功能齐全的ItemsControl示例实现(源代码)

示例用法如下所示:

    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
...
             xmlns:local="clr-namespace:YourNameSpaceHere" 
...
      

您可以看到,WPF中的Xaml和Xamarin之间存在一些差异

但是,您可以在Xamarin GitHub repo中找到一个功能齐全的ItemsControl示例实现(源代码)

示例用法如下所示:

    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
...
             xmlns:local="clr-namespace:YourNameSpaceHere" 
...
      

这是一个问题吗?这是一个问题吗?在2021年,您应该改用CollectionView。早在2016年,这是一个不错的选择,除非你有很多项目,而这些项目目前被滚动到看不见的地方。由于它没有虚拟化项目,因此在一开始就创建了所有项目,这需要大量的时间和内存。2021年,您应该改用CollectionView。早在2016年,这是一个不错的选择,除非你有很多项目,而这些项目目前被滚动到看不见的地方。因为它没有虚拟化项目,所以它在一开始就创建了所有项目,这需要大量的时间和内存。