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

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

Xamarin Forms CollectionView stays empty after binding it

问题

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

  1. public class TemplateListViewModel
  2. {
  3. public ObservableCollection<TemplateSource> sourceList { get; set; }
  4. public TemplateListViewModel()
  5. {
  6. sourceList = new ObservableCollection<TemplateSource>();
  7. loadingTemplates += onLoadingTemplates;
  8. LoadTemplateList();
  9. }
  10. private event EventHandler loadingTemplates = delegate { };
  11. private void LoadTemplateList()
  12. {
  13. loadingTemplates(this, EventArgs.Empty);
  14. }
  15. private async void onLoadingTemplates(object sender, EventArgs args)
  16. {
  17. List<Template> templateList = await App.RestService.GetTemplates(App.User);
  18. foreach (var template in templateList)
  19. {
  20. ImageSource source = ImageSource.FromUri(new Uri("mysite.org/myapp/" + template.FileName));
  21. TemplateSource templateSource = new TemplateSource { Id = template.Id, Source = source };
  22. sourceList.Add(templateSource);
  23. }
  24. }
  25. }

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

  1. <ContentPage.Content>
  2. <StackLayout HorizontalOptions="Fill">
  3. <Frame IsClippedToBounds="True" HeightRequest="45" CornerRadius="5" Padding="0" Margin="15,15,15,0" BackgroundColor="Transparent">
  4. <Entry Placeholder="Search" ReturnType="Done" PlaceholderColor="Gray" x:Name="txtSearch" Margin="5,0,0,0" TextColor="White" />
  5. </Frame>
  6. <CollectionView ItemsLayout="HorizontalList" ItemsSource="{Binding sourceList}">
  7. <CollectionView.ItemTemplate>
  8. <DataTemplate>
  9. <ff:CachedImage
  10. Source="{Binding .}"
  11. VerticalOptions="Center"
  12. HorizontalOptions="Fill" />
  13. </DataTemplate>
  14. </CollectionView.ItemTemplate>
  15. </CollectionView>
  16. </StackLayout>
  17. </ContentPage.Content>

最后,在page.xaml.cs(代码后台)中:

  1. protected override void OnAppearing()
  2. {
  3. TemplateListViewModel vm = new TemplateListViewModel();
  4. BindingContext = vm;
  5. base.OnAppearing();
  6. }

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

我做错了什么?

英文:

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

  1. public class TemplateListViewModel
  2. {
  3. public ObservableCollection&lt;TemplateSource&gt; sourceList { get; set; }
  4. public TemplateListViewModel()
  5. {
  6. sourceList = new ObservableCollection&lt;TemplateSource&gt;();
  7. loadingTemplates += onLoadingTemplates;
  8. LoadTemplateList();
  9. }
  10. private event EventHandler loadingTemplates = delegate { };
  11. private void LoadTemplateList()
  12. {
  13. loadingTemplates(this, EventArgs.Empty);
  14. }
  15. private async void onLoadingTemplates(object sender, EventArgs args)
  16. {
  17. List&lt;Template&gt; templateList = await App.RestService.GetTemplates(App.User);
  18. foreach (var template in templateList)
  19. {
  20. ImageSource source = ImageSource.FromUri(new Uri(&quot;mysite.org/myapp/&quot; + template.FileName));
  21. TemplateSource templateSource = new TemplateSource { Id = template.Id, Source = source };
  22. sourceList.Add(templateSource);
  23. }
  24. }
  25. }

And in my XAML I use this code:

  1. &lt;ContentPage.Content&gt;
  2. &lt;StackLayout HorizontalOptions=&quot;Fill&quot;&gt;
  3. &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;
  4. &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;
  5. &lt;/Frame&gt;
  6. &lt;CollectionView ItemsLayout=&quot;HorizontalList&quot; ItemsSource=&quot;{Binding sourceList}&quot;&gt;
  7. &lt;CollectionView.ItemTemplate&gt;
  8. &lt;DataTemplate&gt;
  9. &lt;ff:CachedImage
  10. Source=&quot;{Binding .}&quot;
  11. VerticalOptions=&quot;Center&quot;
  12. HorizontalOptions=&quot;Fill&quot; /&gt;
  13. &lt;/DataTemplate&gt;
  14. &lt;/CollectionView.ItemTemplate&gt;
  15. &lt;/CollectionView&gt;
  16. &lt;/StackLayout&gt;
  17. &lt;/ContentPage.Content&gt;

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

  1. protected override void OnAppearing()
  2. {
  3. TemplateListViewModel vm = new TemplateListViewModel();
  4. BindingContext = vm;
  5. base.OnAppearing();
  6. }

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项!),你应该像这样更改你的代码:

  1. <ff:CachedImage
  2. Source="{Binding Source}"
  3. VerticalOptions="Center"
  4. 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:

  1. &lt;ff:CachedImage
  2. Source=&quot;{Binding Source}&quot;
  3. VerticalOptions=&quot;Center&quot;
  4. 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:

确定