Skip to content

Commit 4a6fbfa

Browse files
authored
[Handlers] Make sure to update Content on handlers when the ControlTemplate changes (#7235)
* [Handlers] Make sure to update Content on handlers when the ControlTemplate changes * [DeviceTests] Fix tests NRE * [DeviceTests] Add device tests for ControlTemplate updates on ContentView * Update handler value after calling base ApplyTemplate * [Tests] Fix get childcount when there's no child
1 parent 3409e87 commit 4a6fbfa

File tree

7 files changed

+130
-4
lines changed

7 files changed

+130
-4
lines changed

src/Controls/src/Core/ContentView.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Microsoft.Maui.Controls
55
{
66
/// <include file="../../docs/Microsoft.Maui.Controls/ContentView.xml" path="Type[@FullName='Microsoft.Maui.Controls.ContentView']/Docs" />
77
[ContentProperty("Content")]
8-
public class ContentView : TemplatedView, IContentView
8+
public partial class ContentView : TemplatedView
99
{
1010
/// <include file="../../docs/Microsoft.Maui.Controls/ContentView.xml" path="//Member[@MemberName='ContentProperty']/Docs" />
1111
public static readonly BindableProperty ContentProperty = BindableProperty.Create(nameof(Content), typeof(View), typeof(ContentView), null, propertyChanged: TemplateUtilities.OnContentChanged);
@@ -17,9 +17,6 @@ public View Content
1717
set { SetValue(ContentProperty, value); }
1818
}
1919

20-
object IContentView.Content => Content;
21-
IView IContentView.PresentedContent => ((this as IControlTemplated).TemplateRoot as IView) ?? Content;
22-
2320
protected override void OnBindingContextChanged()
2421
{
2522
base.OnBindingContextChanged();
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace Microsoft.Maui.Controls
2+
{
3+
public partial class ContentView : IContentView
4+
{
5+
object IContentView.Content => Content;
6+
IView IContentView.PresentedContent => ((this as IControlTemplated).TemplateRoot as IView) ?? Content;
7+
8+
protected override void OnApplyTemplate()
9+
{
10+
base.OnApplyTemplate();
11+
Handler?.UpdateValue(nameof(Content));
12+
}
13+
}
14+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Microsoft.Maui.Handlers;
2+
using Microsoft.Maui.Platform;
3+
4+
namespace Microsoft.Maui.DeviceTests
5+
{
6+
public partial class ContentViewTests
7+
{
8+
static int GetChildCount(ContentViewHandler contentViewHandler)
9+
{
10+
return contentViewHandler.PlatformView.ChildCount;
11+
}
12+
13+
static int GetContentChildCount(ContentViewHandler contentViewHandler)
14+
{
15+
if (contentViewHandler.PlatformView.GetChildAt(0) is LayoutViewGroup childLayoutViewGroup)
16+
return childLayoutViewGroup.ChildCount;
17+
else
18+
return 0;
19+
}
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Microsoft.Maui.Handlers;
2+
using Microsoft.Maui.Platform;
3+
4+
namespace Microsoft.Maui.DeviceTests
5+
{
6+
public partial class ContentViewTests
7+
{
8+
static int GetChildCount(ContentViewHandler contentViewHandler)
9+
{
10+
return contentViewHandler.PlatformView.Children.Count;
11+
}
12+
13+
static int GetContentChildCount(ContentViewHandler contentViewHandler)
14+
{
15+
if (contentViewHandler.PlatformView.Children[0] is LayoutPanel childLayoutPanel)
16+
return childLayoutPanel.Children.Count;
17+
else
18+
return 0;
19+
}
20+
}
21+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Microsoft.Maui.Controls;
4+
using Microsoft.Maui.Handlers;
5+
using Microsoft.Maui.Hosting;
6+
using Microsoft.Maui.Platform;
7+
using Xunit;
8+
9+
namespace Microsoft.Maui.DeviceTests
10+
{
11+
[Category(TestCategory.ContentView)]
12+
public partial class ContentViewTests : HandlerTestBase
13+
{
14+
void SetupBuilder()
15+
{
16+
EnsureHandlerCreated(builder =>
17+
{
18+
builder.ConfigureMauiHandlers(handlers =>
19+
{
20+
handlers.AddHandler<Label, LabelHandler>();
21+
handlers.AddHandler<IContentView, ContentViewHandler>();
22+
handlers.AddHandler<Grid, LayoutHandler>();
23+
});
24+
});
25+
}
26+
27+
[Fact("ContentView updating it's ControlTemplate works")]
28+
public async Task ControlTemplateUpdates()
29+
{
30+
SetupBuilder();
31+
var child = new Label { Text = "Content 1" };
32+
var contentView = new Microsoft.Maui.Controls.ContentView();
33+
var header = new Label { Text = "Header" };
34+
var footer = new Label { Text = "Footer" };
35+
var presenter = new ContentPresenter();
36+
var grid = new Grid();
37+
38+
var contentViewHandler = await CreateHandlerAsync<ContentViewHandler>(contentView);
39+
40+
await InvokeOnMainThreadAsync(() =>
41+
{
42+
contentView.Content = child;
43+
Assert.True(GetChildCount(contentViewHandler) == 1);
44+
Assert.True(GetContentChildCount(contentViewHandler) == 0);
45+
grid.Children.Add(header);
46+
grid.Children.Add(presenter);
47+
grid.Children.Add(footer);
48+
var dataTemplate = new ControlTemplate(() => grid);
49+
contentView.ControlTemplate = dataTemplate;
50+
Assert.True(GetChildCount(contentViewHandler) == 1);
51+
Assert.True(GetContentChildCount(contentViewHandler) == 3);
52+
});
53+
}
54+
}
55+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Microsoft.Maui.Handlers;
2+
3+
namespace Microsoft.Maui.DeviceTests
4+
{
5+
public partial class ContentViewTests
6+
{
7+
static int GetChildCount(ContentViewHandler contentViewHandler)
8+
{
9+
return contentViewHandler.PlatformView.Subviews.Length;
10+
}
11+
12+
static int GetContentChildCount(ContentViewHandler contentViewHandler)
13+
{
14+
return contentViewHandler.PlatformView.Subviews[0].Subviews.Length;
15+
}
16+
}
17+
}

src/Controls/tests/DeviceTests/TestCategory.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ public static class TestCategory
44
{
55
public const string Behavior = "Behavior";
66
public const string Button = "Button";
7+
public const string ContentView = "ContentView";
78
public const string Dispatcher = "Dispatcher";
89
public const string Editor = "Editor";
910
public const string Element = "Element";

0 commit comments

Comments
 (0)