【发布时间】:2015-02-21 18:30:14
【问题描述】:
我一直致力于优化我的 Xamarin.Forms 应用,以及我的 CarouselPage 如何滚动图像。我已经实现了一次仅延迟加载 3 张图像的逻辑(轮播中的某个时间点可能有 100 张)。
- currentIndex 是滑动前页面的索引。
- WINDOW_SIZE 是一个常数,表示要向左侧加载多少图像,并且 当前页面的右侧。
- CustomContent 是填充轮播的 ContentPage 类。
- layout 是 CustomContent 内部的私有 RelativeLayout 在构造函数中构建的 ContentPage。
这是在我的 Carousel 的 OnCurrentPageChanged() 事件中调用的代码:
protected override void OnCurrentPageChanged ()
{
base.OnCurrentPageChanged();
int newIndex = this.Children.IndexOf(this.CurrentPage);
if (newIndex > currentIndex)
{
currentIndex++;
UnloadImages();
LoadImages();
}
else if (newIndex < currentIndex)
{
currentIndex--;
UnloadImages();
LoadImages();
}
}
这里是被调用的函数:
private void LoadImages ()
{
int lowIndex = currentIndex - WINDOW_SIZE >= 0 ? currentIndex - WINDOW_SIZE : 0;
int highIndex = currentIndex + WINDOW_SIZE <= this.Children.Count() - 1 ? currentIndex + WINDOW_SIZE : this.Children.Count() - 1;
for (int i = lowIndex; i <= highIndex; i++)
{
CustomContent custom = (CustomContent) this.Children[i];
custom.LoadImage();
}
}
private void UnloadImages ()
{
int lowIndex = currentIndex - WINDOW_SIZE >= 0 ? currentIndex - WINDOW_SIZE : 0;
int highIndex = currentIndex + WINDOW_SIZE <= this.Children.Count () - 1 ? currentIndex + WINDOW_SIZE : this.Children.Count () - 1;
if (lowIndex - 1 >= 0)
{
CustomContent custom = (CustomContent) this.Children[lowIndex - 1];
custom.UnloadImage();
}
if (highIndex + 1 <= thoughts.Count () - 1)
{
CustomContent thought = (CustomContent) this.Children[highIndex + 1];
custom.UnloadImage();
}
}
这是在填充轮播的 (CustomContent) ContentPages 中调用的代码:
public void LoadImage ()
{
this.Content = layout;
}
public void UnloadImage ()
{
this.Content = null;
}
【问题讨论】:
标签: xamarin xamarin.forms