diff --git a/src/Controls/src/Core/ContentView.cs b/src/Controls/src/Core/ContentView.cs index ee59c4ad46f4..672d9f247542 100644 --- a/src/Controls/src/Core/ContentView.cs +++ b/src/Controls/src/Core/ContentView.cs @@ -5,7 +5,7 @@ namespace Microsoft.Maui.Controls { /// [ContentProperty("Content")] - public class ContentView : TemplatedView, IContentView + public partial class ContentView : TemplatedView { /// public static readonly BindableProperty ContentProperty = BindableProperty.Create(nameof(Content), typeof(View), typeof(ContentView), null, propertyChanged: TemplateUtilities.OnContentChanged); @@ -17,9 +17,6 @@ public View Content set { SetValue(ContentProperty, value); } } - object IContentView.Content => Content; - IView IContentView.PresentedContent => ((this as IControlTemplated).TemplateRoot as IView) ?? Content; - protected override void OnBindingContextChanged() { base.OnBindingContextChanged(); diff --git a/src/Controls/src/Core/HandlerImpl/ContentView/ContentView.Impl.cs b/src/Controls/src/Core/HandlerImpl/ContentView/ContentView.Impl.cs new file mode 100644 index 000000000000..4c44965267a4 --- /dev/null +++ b/src/Controls/src/Core/HandlerImpl/ContentView/ContentView.Impl.cs @@ -0,0 +1,14 @@ +namespace Microsoft.Maui.Controls +{ + public partial class ContentView : IContentView + { + object IContentView.Content => Content; + IView IContentView.PresentedContent => ((this as IControlTemplated).TemplateRoot as IView) ?? Content; + + protected override void OnApplyTemplate() + { + base.OnApplyTemplate(); + Handler?.UpdateValue(nameof(Content)); + } + } +} diff --git a/src/Controls/tests/DeviceTests/Elements/ContentView/ContentViewTests.Android.cs b/src/Controls/tests/DeviceTests/Elements/ContentView/ContentViewTests.Android.cs new file mode 100644 index 000000000000..068f2eac7738 --- /dev/null +++ b/src/Controls/tests/DeviceTests/Elements/ContentView/ContentViewTests.Android.cs @@ -0,0 +1,21 @@ +using Microsoft.Maui.Handlers; +using Microsoft.Maui.Platform; + +namespace Microsoft.Maui.DeviceTests +{ + public partial class ContentViewTests + { + static int GetChildCount(ContentViewHandler contentViewHandler) + { + return contentViewHandler.PlatformView.ChildCount; + } + + static int GetContentChildCount(ContentViewHandler contentViewHandler) + { + if (contentViewHandler.PlatformView.GetChildAt(0) is LayoutViewGroup childLayoutViewGroup) + return childLayoutViewGroup.ChildCount; + else + return 0; + } + } +} diff --git a/src/Controls/tests/DeviceTests/Elements/ContentView/ContentViewTests.Windows.cs b/src/Controls/tests/DeviceTests/Elements/ContentView/ContentViewTests.Windows.cs new file mode 100644 index 000000000000..2e32a119dae1 --- /dev/null +++ b/src/Controls/tests/DeviceTests/Elements/ContentView/ContentViewTests.Windows.cs @@ -0,0 +1,21 @@ +using Microsoft.Maui.Handlers; +using Microsoft.Maui.Platform; + +namespace Microsoft.Maui.DeviceTests +{ + public partial class ContentViewTests + { + static int GetChildCount(ContentViewHandler contentViewHandler) + { + return contentViewHandler.PlatformView.Children.Count; + } + + static int GetContentChildCount(ContentViewHandler contentViewHandler) + { + if (contentViewHandler.PlatformView.Children[0] is LayoutPanel childLayoutPanel) + return childLayoutPanel.Children.Count; + else + return 0; + } + } +} diff --git a/src/Controls/tests/DeviceTests/Elements/ContentView/ContentViewTests.cs b/src/Controls/tests/DeviceTests/Elements/ContentView/ContentViewTests.cs new file mode 100644 index 000000000000..0e5b5256f2b5 --- /dev/null +++ b/src/Controls/tests/DeviceTests/Elements/ContentView/ContentViewTests.cs @@ -0,0 +1,55 @@ +using System; +using System.Threading.Tasks; +using Microsoft.Maui.Controls; +using Microsoft.Maui.Handlers; +using Microsoft.Maui.Hosting; +using Microsoft.Maui.Platform; +using Xunit; + +namespace Microsoft.Maui.DeviceTests +{ + [Category(TestCategory.ContentView)] + public partial class ContentViewTests : HandlerTestBase + { + void SetupBuilder() + { + EnsureHandlerCreated(builder => + { + builder.ConfigureMauiHandlers(handlers => + { + handlers.AddHandler(); + handlers.AddHandler(); + handlers.AddHandler(); + }); + }); + } + + [Fact("ContentView updating it's ControlTemplate works")] + public async Task ControlTemplateUpdates() + { + SetupBuilder(); + var child = new Label { Text = "Content 1" }; + var contentView = new Microsoft.Maui.Controls.ContentView(); + var header = new Label { Text = "Header" }; + var footer = new Label { Text = "Footer" }; + var presenter = new ContentPresenter(); + var grid = new Grid(); + + var contentViewHandler = await CreateHandlerAsync(contentView); + + await InvokeOnMainThreadAsync(() => + { + contentView.Content = child; + Assert.True(GetChildCount(contentViewHandler) == 1); + Assert.True(GetContentChildCount(contentViewHandler) == 0); + grid.Children.Add(header); + grid.Children.Add(presenter); + grid.Children.Add(footer); + var dataTemplate = new ControlTemplate(() => grid); + contentView.ControlTemplate = dataTemplate; + Assert.True(GetChildCount(contentViewHandler) == 1); + Assert.True(GetContentChildCount(contentViewHandler) == 3); + }); + } + } +} diff --git a/src/Controls/tests/DeviceTests/Elements/ContentView/ContentViewTests.iOS.cs b/src/Controls/tests/DeviceTests/Elements/ContentView/ContentViewTests.iOS.cs new file mode 100644 index 000000000000..0e38b37d5927 --- /dev/null +++ b/src/Controls/tests/DeviceTests/Elements/ContentView/ContentViewTests.iOS.cs @@ -0,0 +1,17 @@ +using Microsoft.Maui.Handlers; + +namespace Microsoft.Maui.DeviceTests +{ + public partial class ContentViewTests + { + static int GetChildCount(ContentViewHandler contentViewHandler) + { + return contentViewHandler.PlatformView.Subviews.Length; + } + + static int GetContentChildCount(ContentViewHandler contentViewHandler) + { + return contentViewHandler.PlatformView.Subviews[0].Subviews.Length; + } + } +} diff --git a/src/Controls/tests/DeviceTests/TestCategory.cs b/src/Controls/tests/DeviceTests/TestCategory.cs index 3b4ad1a9fb7f..8616444e1a33 100644 --- a/src/Controls/tests/DeviceTests/TestCategory.cs +++ b/src/Controls/tests/DeviceTests/TestCategory.cs @@ -4,6 +4,7 @@ public static class TestCategory { public const string Behavior = "Behavior"; public const string Button = "Button"; + public const string ContentView = "ContentView"; public const string Dispatcher = "Dispatcher"; public const string Editor = "Editor"; public const string Element = "Element";