【问题标题】:Xamarin Forms Stop loading data in CollectionViewXamarin Forms 停止在 CollectionView 中加载数据
【发布时间】:2020-01-09 07:14:57
【问题描述】:

我有问题。我创建了一个使用自定义ViewModelCollectionView。在那个ViewModel 中,我对我的网页进行了网络调用,以获取 20 个图像文件名。得到结果后,我会调用 foreach 文件名来获取该文件名的ImageSource。现在我创建了一个Load data incrementally 代码来加载CollectionView 20 个数据包。这是我的 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;
}

这是视图模型:

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 次网络通话之后,应用程序得到了一切。之后,应用从 webcall 收到的结果是 "Nothing"

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

【问题讨论】:

  • 停止发出请求。 CollectionView 不知道没有更多数据要接收。您必须在代码中确定这一点并停止发出请求。如果 API 调用未返回任何数据,请设置一些标志,以便您知道停止发出请求。

标签: c# xamarin xamarin.forms xamarin.android xamarin.ios


【解决方案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;
    }

【讨论】:

  • 谢谢,我创建了一个更长的版本,但这很好用!!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多