【发布时间】:2019-11-21 22:09:45
【问题描述】:
我有一个 Xamarin.Forms 应用程序,它使用 Alex Rainman 的 CarouselView (https://github.com/alexrainman/CarouselView) 来呈现一个轮播(惊喜!:))。轮播中的每个页面都包含一个项目列表。在 Android 上看起来像这样:
在 Windows 上类似。但是,在 iOS 上我得到了这个:
起初我认为这是 CarouselView 组件中的错误并提交了issue,但作者说它在他这边工作正常。我试图以我能想到的任何方式对其进行调试,但找不到问题。 CarouselView 被实例化并添加到视图层次结构中。如果我用显示列的 ListView 替换它,ViewModel 就会被实例化并且工作正常。现在我没有想法,非常感谢一些帮助!
这是我的代码:
MainPage.xaml:
<controls:CarouselViewControl Orientation="Horizontal"
InterPageSpacing="0"
Position="0"
ItemsSource="{Binding Columns}"
VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand"
BackgroundColor="Gray"
ShowIndicators="True"
ShowArrows="True">
<controls:CarouselViewControl.ItemTemplate>
<DataTemplate>
<ContentView HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
Padding="20">
<ListView HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
BackgroundColor="LightGray"
Header="{Binding Name}"
ItemsSource="{Binding Tasks}"
ItemSelected="ListView_ItemSelected">
<ListView.ItemTemplate>
<DataTemplate>
<ImageCell ImageSource="{Binding Image}"
Text="{Binding Title}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentView>
</DataTemplate>
</controls:CarouselViewControl.ItemTemplate>
</controls:CarouselViewControl>
(后面的代码只是在构造函数中调用InitializeComponent())
ColumnsViewModel.cs:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace ProofOfConcept
{
public class ColumnsViewModel : BindableObject
{
public static readonly BindableProperty PositionProperty = BindableProperty.Create(nameof(Position), typeof(int), typeof(ColumnsViewModel), 0, BindingMode.TwoWay);
public ObservableCollection<Column> Columns { get; set; }
public int Position
{
get { return (int)this.GetValue(PositionProperty); }
set { this.SetValue(PositionProperty, value); }
}
public ColumnsViewModel()
{
Columns = new ObservableCollection<Column>();
Random random = new Random();
for (int i = 0; i < 10; i++)
{
Column column = new Column
{
Name = String.Format("Column {0}", i),
Background = Color.FromRgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255))
};
ObservableCollection<Task> tasks = new ObservableCollection<Task>();
for (int t = 0; t < 25; t++)
{
Task task = new Task
{
Title = String.Format("Column #{0}, Task #{1}", i, t),
Color = Color.Lavender
};
tasks.Add(task);
}
column.Tasks = tasks;
Columns.Add(column);
}
}
}
}
列.cs:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace ProofOfConcept
{
public class Column
{
public string Name { get; set; }
public ObservableCollection<Task> Tasks { get; set; }
public Color Background { get; set; }
public string Image
{
get
{
return "http://loremflickr.com/100/100";
}
}
}
}
Task.cs:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace ProofOfConcept
{
public class Task : BindableObject
{
public static readonly BindableProperty ColorProperty = BindableProperty.Create(nameof(Color), typeof(Color), typeof(Task), Color.White, BindingMode.TwoWay);
public string Title { get; set; }
public string Assignee
{
get
{
return "Ivan";
}
}
public Color Color
{
get { return (Color)this.GetValue(ColorProperty); }
set { this.SetValue(ColorProperty, value); }
}
public string Image
{
get
{
return "http://loremflickr.com/100/100";
}
}
}
}
另外,整个解决方案的here is a zip (没有 Windows 项目,因为它非常大而且并不真正相关)。
【问题讨论】:
标签: xamarin xamarin.forms