Xamarin Forms CollectionView绑定后仍然保持为空。

huangapple go评论78阅读模式
英文:

Xamarin Forms CollectionView stays empty after binding it

问题

我有一个问题。我创建了一个创建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确实被填充了,所以这不是问题。

我做错了什么?

英文:

I have a problem. I created this class that creates an ImageSource collection ObservableCollection&lt;TemplateSource&gt;:

public class TemplateListViewModel
{
    public ObservableCollection&lt;TemplateSource&gt; sourceList { get; set; }

    public TemplateListViewModel()
    {
        sourceList = new ObservableCollection&lt;TemplateSource&gt;();
        loadingTemplates += onLoadingTemplates;
        LoadTemplateList();
    }

    private event EventHandler loadingTemplates = delegate { };

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

    private async void onLoadingTemplates(object sender, EventArgs args)
    {
        List&lt;Template&gt; templateList = await App.RestService.GetTemplates(App.User);

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

And in my XAML I use this code:

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

        &lt;CollectionView ItemsLayout=&quot;HorizontalList&quot; ItemsSource=&quot;{Binding sourceList}&quot;&gt;
            &lt;CollectionView.ItemTemplate&gt;
                &lt;DataTemplate&gt;

                    &lt;ff:CachedImage
                Source=&quot;{Binding .}&quot;
                VerticalOptions=&quot;Center&quot;
                HorizontalOptions=&quot;Fill&quot; /&gt;

                &lt;/DataTemplate&gt;
            &lt;/CollectionView.ItemTemplate&gt;
        &lt;/CollectionView&gt;
    &lt;/StackLayout&gt;
&lt;/ContentPage.Content&gt;

And finally in the page.xaml.cs (code behind):

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

Now I already got help with this code from @Deczaloth, but he couldn't figure out why the CollectionView stays emtpy after I bind it. Now I already checked, but the sourceList does get filled, so thats not the problem.

What am I doing wrong?

答案1

得分: 0

I can see one potential problem in your code XD:

当你绑定CachedImageSource属性时,你将绑定设置为&quot;.&quot;,但你应该绑定到TemplateSource类的Source属性(在你的上下文中&quot;.&quot;表示一个TemplateSource项!),你应该像这样更改你的代码:

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

I can see one potential problem in your code XD:

When you bind the Source property of CachedImage you set the binding to &quot;.&quot;, but you should instead bind to the Source property of the TemplateSource class (in your context "." means a TemplateSource item!), that is you should change your code like so:

&lt;ff:CachedImage
    Source=&quot;{Binding Source}&quot;
    VerticalOptions=&quot;Center&quot;
    HorizontalOptions=&quot;Fill&quot; /&gt;

huangapple
  • 本文由 发表于 2020年1月7日 00:26:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/59615591.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定