Xamarin Forms CollectionView绑定后保持为空

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

我有问题。我创建了一个创建ImageSource集合ObservableCollection<TemplateSource>的类:

public class TemplateListViewModel
{
    public ObservableCollection<TemplateSource> sourceList { get; set; }

    public TemplateListViewModel()
    {
        sourceList = new ObservableCollection<TemplateSource>();
        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);

        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 };
            sourceList.Add(templateSource);
        }
    }
}

并且在我的XAML中,我使用以下代码:

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

        <CollectionView ItemsLayout="HorizontalList" ItemsSource="{Binding sourceList}">
            <CollectionView.ItemTemplate>
                <DataTemplate>

                    <ff:CachedImage
                Source="{Binding .}"
                VerticalOptions="Center"
                HorizontalOptions="Fill" />

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

最后是page.xaml.cs(后面的代码):

protected override void OnAppearing()
{
    TemplateListViewModel vm = new TemplateListViewModel();
    BindingContext = vm;
    base.OnAppearing();
}

现在我已经从@Deczaloth那里获得了有关此代码的帮助,但是他无法弄清楚为什么我绑定CollectionView后仍然保持空。现在我已经检查过了,但是sourceList确实被填充了,所以那不是问题。

我在做什么错?

c# xamarin xamarin.forms xamarin.android xamarin.ios
1个回答
1
投票
TemplateSource
© www.soinside.com 2019 - 2024. All rights reserved.