Skip to content

Commit

Permalink
[BUG] [Regions] LayoutViewRegionAdapter should be mapped to Maui.Cont…
Browse files Browse the repository at this point in the history
…rols.Layout instead of Maui.Controls.Compatibility.Layout
  • Loading branch information
Civale, Luca authored and Civale, Luca committed Jun 11, 2024
1 parent 52492f3 commit a701cb5
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ internal static IContainerRegistry RegisterRegionServices(this IContainerRegistr
// TODO: CollectionView is buggy with only last View showing despite multiple Active Views
// BUG: iOS Crash with CollectionView https://github.com/xamarin/Xamarin.Forms/issues/9970
//regionAdapterMappings.RegisterDefaultMapping<CollectionView, CollectionViewRegionAdapter>();
regionAdapterMappings.RegisterDefaultMapping<Layout, LayoutViewRegionAdapter>();
regionAdapterMappings.RegisterDefaultMapping<Microsoft.Maui.Controls.Compatibility.Layout<View>, LayoutViewRegionAdapter>();
regionAdapterMappings.RegisterDefaultMapping<Layout, LayoutRegionAdapter>();
regionAdapterMappings.RegisterDefaultMapping<ScrollView, ScrollViewRegionAdapter>();
regionAdapterMappings.RegisterDefaultMapping<ContentView, ContentViewRegionAdapter>();
return regionAdapterMappings;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using Prism.Properties;

namespace Prism.Navigation.Regions.Adapters;

/// <summary>
/// Adapter that creates a new <see cref="Region"/> and monitors its
/// active view to set it on the adapted <see cref="Layout"/>.
/// </summary>
public class LayoutRegionAdapter : RegionAdapterBase<Layout>
{
/// <summary>
/// Initializes a new instance of <see cref="LayoutRegionAdapter"/>.
/// </summary>
/// <param name="regionBehaviorFactory">The factory used to create the region behaviors to attach to the created regions.</param>
public LayoutRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory)
: base(regionBehaviorFactory)
{
}

/// <summary>
/// Adapts a <see cref="Layout"/> to an <see cref="IRegion"/>.
/// </summary>
/// <param name="region">The new region being used.</param>
/// <param name="regionTarget">The object to adapt.</param>
protected override void Adapt(IRegion region, Layout regionTarget)
{
if (region == null)
throw new ArgumentNullException(nameof(region));

if (regionTarget == null)
throw new ArgumentNullException(nameof(regionTarget));

bool itemsSourceIsSet = regionTarget.Children?.Any() ?? false || regionTarget.IsSet(BindableLayout.ItemsSourceProperty);

if (itemsSourceIsSet)
{
throw new InvalidOperationException(Resources.LayoutViewHasChildrenException);
}

BindableLayout.SetItemsSource(regionTarget, region.Views);
BindableLayout.SetItemTemplate(regionTarget, new RegionItemsSourceTemplate());
}

/// <summary>
/// Creates a new instance of <see cref="IRegion"/>.
/// </summary>
/// <returns>A new instance of <see cref="Region"/>.</returns>
protected override IRegion CreateRegion(IContainerProvider container) =>
container.Resolve<Region>();
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using Microsoft.Maui.Controls.Compatibility;
using Prism.Properties;

namespace Prism.Navigation.Regions.Adapters;

/// <summary>
/// Adapter that creates a new <see cref="Region"/> and monitors its
/// active view to set it on the adapted <see cref="Layout"/>.
/// active view to set it on the adapted <see cref="Layout{View}"/>.
/// </summary>
public class LayoutViewRegionAdapter : RegionAdapterBase<Layout>
public class LayoutViewRegionAdapter : RegionAdapterBase<Layout<View>>
{
/// <summary>
/// Initializes a new instance of <see cref="LayoutViewRegionAdapter"/>.
Expand All @@ -18,11 +19,11 @@ public LayoutViewRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory)
}

/// <summary>
/// Adapts a <see cref="Layout"/> to an <see cref="IRegion"/>.
/// Adapts a <see cref="Layout{View}"/> to an <see cref="IRegion"/>.
/// </summary>
/// <param name="region">The new region being used.</param>
/// <param name="regionTarget">The object to adapt.</param>
protected override void Adapt(IRegion region, Layout regionTarget)
protected override void Adapt(IRegion region, Layout<View> regionTarget)
{
if (region == null)
throw new ArgumentNullException(nameof(region));
Expand Down

0 comments on commit a701cb5

Please sign in to comment.