Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ItemContentControl - apply bindings once for ItemTemplate (Windows) #10999

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public CollectionViewGallery()
GalleryBuilder.NavButton("Alternate Layout Galleries", () => new AlternateLayoutGallery(), Navigation),
GalleryBuilder.NavButton("Header/Footer Galleries", () => new HeaderFooterGallery(), Navigation),
GalleryBuilder.NavButton("Nested CollectionViews", () => new NestedGalleries.NestedCollectionViewGallery(), Navigation),
GalleryBuilder.NavButton("Online images", () => new OnlineImages(), Navigation),
}
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:collectionviewgalleries="clr-namespace:Maui.Controls.Sample.Pages.CollectionViewGalleries"
x:Class="Maui.Controls.Sample.Pages.CollectionViewGalleries.OnlineImages"
Title="Online images">

<Grid>
<CollectionView x:Name="CollectionView" BackgroundColor="Yellow">
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="collectionviewgalleries:OnlineImageInfo">
<Image HeightRequest="200" WidthRequest="200" Source="{Binding Uri}" />
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</Grid>
</ContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Xaml;
using Microsoft.Maui.Dispatching;

namespace Maui.Controls.Sample.Pages.CollectionViewGalleries
{
public record OnlineImageInfo(string Uri);

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class OnlineImages : ContentPage
{
public OnlineImages()
{
InitializeComponent();

Dispatcher.DispatchAsync(SetItemsSource);
}

async Task SetItemsSource()
{
await Task.Delay(TimeSpan.FromSeconds(1));

CollectionView.ItemsSource = new ObservableCollection<OnlineImageInfo>
{
new ("https://news.microsoft.com/wp-content/uploads/prod/2022/07/hexagon_print.gif"),
new ("https://news.microsoft.com/wp-content/uploads/prod/2022/07/collaboration-controls-in-power-platform_print.gif"),
new ("https://news.microsoft.com/wp-content/uploads/prod/2022/07/Updatesin-Teams.png"),
new ("https://news.microsoft.com/wp-content/uploads/prod/2022/07/Expanded-Reactions.png"),
new ("https://news.microsoft.com/wp-content/uploads/prod/2022/04/Companion-Devices.jpg"),
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ internal void Realize()
// or if we need to switch DataTemplates (because this instance is being recycled)
// then we'll need to create the content from the template
_visualElement = formsTemplate.CreateContent(dataContext, container) as VisualElement;
_visualElement.BindingContext = dataContext;
_renderer = _visualElement.ToHandler(mauiContext);

// We need to set IsPlatformStateConsistent explicitly; otherwise, it won't be set until the renderer's Loaded
Expand All @@ -186,11 +185,11 @@ internal void Realize()
{
// We are reusing this ItemContentControl and we can reuse the Element
_visualElement = _renderer.VirtualView as VisualElement;
_visualElement.BindingContext = dataContext;
}

Content = _renderer.ToPlatform();
itemsView?.AddLogicalChild(_visualElement);
_visualElement.BindingContext = dataContext;
}

void SetNativeStateConsistent(VisualElement visualElement)
Expand Down