Xamarin Forms停止在CollectionView中加载数据

问题描述 投票:-1回答:1

我有问题。我创建了一个使用自定义CollectionViewViewModel。在该ViewModel中,我对自己的网页进行了网络调用,以获取20个图像文件名。得到结果后,我为每个文件名做一个调用以获取该文件名的ImageSource。现在,我创建了一个Load data incrementally代码,以20包的捆绑方式加载CollectionView数据。这是我的xaml:

<ContentPage.Content>
    <StackLayout HorizontalOptions="Fill" Padding="15">
        <Frame IsClippedToBounds="True" HeightRequest="45" CornerRadius="5" Padding="0" BackgroundColor="Transparent">
            <Entry Placeholder="Search" ReturnType="Done" PlaceholderColor="Gray" x:Name="txtSearch" Margin="5,0,0,0" TextColor="White" />
        </Frame>

        <CollectionView ItemsSource="{Binding sourceList}" RemainingItemsThreshold="6"
            RemainingItemsThresholdReachedCommand="{Binding LoadTemplates}">
            <CollectionView.ItemsLayout>
                <GridItemsLayout Orientation="Vertical"
                Span="2" />
            </CollectionView.ItemsLayout>
            <CollectionView.ItemTemplate>
                <DataTemplate>

                    <ff:CachedImage
                Source="{Binding Source}"
                VerticalOptions="Center"
                HorizontalOptions="Center"
                WidthRequest="{Binding WidthHeight}"
                HeightRequest="{Binding WidthHeight}">
                        <ff:CachedImage.GestureRecognizers>
                            <TapGestureRecognizer Tapped="imgTemplate_Clicked" />
                        </ff:CachedImage.GestureRecognizers>
                    </ff:CachedImage>

                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
    </StackLayout>
</ContentPage.Content>

这里是页面构造函数:

public TemplateList()
{
    InitializeComponent();

    TemplateListViewModel vm = new TemplateListViewModel();
    BindingContext = vm;
}

这里是ViewModel:

public class TemplateListViewModel
{
    public ICommand LoadTemplates => new Command(LoadTemplateList);

    public int CurrentTemplateCountReceived;
    public ObservableCollection<TemplateSource> sourceList { get; set; }
    public double MemeWidthHeight { get; set; }

    public TemplateListViewModel()
    {
        CurrentTemplateCountReceived = 0;
        sourceList = new ObservableCollection<TemplateSource>();

        var mainDisplayInfo = DeviceDisplay.MainDisplayInfo;
        var width = mainDisplayInfo.Width;
        var density = mainDisplayInfo.Density;
        var ScaledWidth = width / density;

        MemeWidthHeight = (ScaledWidth / 2);

        loadingTemplates += onLoadingTemplates;
        LoadTemplateList();
    }

    private event EventHandler loadingTemplates = delegate { };

    private void LoadTemplateList()
    {
        loadingTemplates(this, EventArgs.Empty);
    }

    private async void onLoadingTemplates(object sender, EventArgs args)
    {
        List<Template> templateList = await App.RestService.GetTemplates(App.User, CurrentTemplateCountReceived);

        foreach (var template in templateList)
        {
            ImageSource source = ImageSource.FromUri(new Uri("mysite.org/myapp/" + template.FileName));
            TemplateSource templateSource = new TemplateSource { Id = template.Id, Source = source, WidthHeight= MemeWidthHeight, FileName = template.FileName };
            sourceList.Add(templateSource);
        }

        CurrentTemplateCountReceived = sourceList.Count;
    }
}

现在App.RestService.GetTemplates(App.User, CurrentTemplateCountReceived);只是返回一个带有文件名的列表,但是问题是当我一无所有时,它会继续进行网络通话。在我的服务器上,我有38张图像,因此经过2次网络通话后,应用程序获得了所有内容。之后,应用程序从网络通话中收到的结果为"Nothing"

所以我的问题是:当我处于CollectionView的底部时,如何停止进行网络通话?

c# xamarin xamarin.forms xamarin.android xamarin.ios
1个回答
1
投票
bool moreData = true;

private async void onLoadingTemplates(object sender, EventArgs args)
    {
        if (!moreData) return;

        List<Template> templateList = await App.RestService.GetTemplates(App.User, CurrentTemplateCountReceived);

        if (templateList is null or templateList.Count == 0) {
          moreData = false;
          return;
        }

        foreach (var template in templateList)
        {
            ImageSource source = ImageSource.FromUri(new Uri("mysite.org/myapp/" + template.FileName));
            TemplateSource templateSource = new TemplateSource { Id = template.Id, Source = source, WidthHeight= MemeWidthHeight, FileName = template.FileName };
            sourceList.Add(templateSource);
        }

        CurrentTemplateCountReceived = sourceList.Count;
    }
© www.soinside.com 2019 - 2024. All rights reserved.