Add support for CollectionView
See original GitHub issueI decided to add item to discuss challenges and approaches to implement CollectionView. I can see two issues straight away - supporting ItemsSource property and ItemTemplate property.
Supporting ItemsSource property was discussed in https://github.com/xamarin/MobileBlazorBindings/issues/249, and I think same approach with wrapping in delegate should work here as well. The only thing - I’m not sure whether we should do anything about notifying about updates.
Supporting DataTemplate properties will probably be trickier.
Unlike original Xamarin control, I suggest to make CollectionView in MBB generic where T is item type, and to make ItemTemplate property of RenderFragment<T> type.
Smth like this
public class CollectionView<T> : ...
{
[Parameter] public IEnumerable<T> ItemsSource {get; set;}
[Parameter] public RenderFragment<T> ItemTemplate {get; set;}
...
}
So that it could be used like that:
<CollectionView ItemsSource="_models">
<ItemTemplate>
<Label Text="@context.Name" />
</ItemTemplate>
</CollectionView>
@code {
List<MyModel> _models;
}
Now we need render those items, and pass to Xamarin’s ColectionView.
Currently there is no way to render components as root elements, we can only render it as child component (e.g. for Application or ContentPage). Is there an easy way to allow that? Otherwise I suppose we can create “fake” container element, which we’ll simply use to render elements to, and then grab them from it.
As for passing to CollectionView, I suppose we can inherit from DataTemplateSelector, smth like this:
public class RenderFragmentDataTemplateSelector<T>: DataTemplateSelector
{
public RenderFragmentDataTemplateSelector(RenderFragment<T> fragment)
protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
{
return _renderer.RenderFragment(fragment((T)item));
}
}
Any thoughts?
Issue Analytics
- State:
- Created 3 years ago
- Comments:13 (13 by maintainers)

Top Related StackOverflow Question
Yeah I think what @Dreamescaper is saying is the crux of the problem because Blazor doesn’t expose any binding metadata (to an outside observer such as MBB), whereas in Xamarin.Forms you must provide only metadata.
If anyone would like to test suggested CollectionView approach until #346 is merged, I’ve published Microsoft.MobileBlazorBindings and Microsoft.MobileBlazorBindings.Core packages (0.6.69-preview-collectionview) to temporary myget source: https://www.myget.org/F/dreamescaper/api/v3/index.json