Skip to content
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
5 changes: 1 addition & 4 deletions src/Controls/src/Core/ContentView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Microsoft.Maui.Controls
{
/// <include file="../../docs/Microsoft.Maui.Controls/ContentView.xml" path="Type[@FullName='Microsoft.Maui.Controls.ContentView']/Docs" />
[ContentProperty("Content")]
public class ContentView : TemplatedView, IContentView
public partial class ContentView : TemplatedView
{
/// <include file="../../docs/Microsoft.Maui.Controls/ContentView.xml" path="//Member[@MemberName='ContentProperty']/Docs" />
public static readonly BindableProperty ContentProperty = BindableProperty.Create(nameof(Content), typeof(View), typeof(ContentView), null, propertyChanged: TemplateUtilities.OnContentChanged);
Expand All @@ -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();
Expand Down
14 changes: 14 additions & 0 deletions src/Controls/src/Core/HandlerImpl/ContentView/ContentView.Impl.cs
Original file line number Diff line number Diff line change
@@ -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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we swap the order of these two?

I realize base.OnApplyTemplate is currently empty but it just feels like logically you'd want the UpdateValue call to happen after OnApplyTemplate

Handler?.UpdateValue(nameof(Content));
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
@@ -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<Label, LabelHandler>();
handlers.AddHandler<IContentView, ContentViewHandler>();
handlers.AddHandler<Grid, LayoutHandler>();
});
});
}

[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<ContentViewHandler>(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);
});
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
1 change: 1 addition & 0 deletions src/Controls/tests/DeviceTests/TestCategory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down