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

fix(netstd): late template application is ignored (backport #18944) #19003

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 @@ -289,6 +289,40 @@ public async Task VisualStateGroup_TP_Inheritance()
""";
VerifyTree(expectations, setup, checkVSG: true);
}

[TestMethod]
public Task LateTemplateSwapping_NonContentControl() => LateTemplateSwapping<TextBox>();

[TestMethod]
public Task LateTemplateSwapping_ContentControl() => LateTemplateSwapping<ContentControl>();

public async Task LateTemplateSwapping<TControl>() where TControl : Control, new()
{
var templateA = XamlHelper.LoadXaml<ControlTemplate>("""
<ControlTemplate>
<Grid x:Name="RootA" Width="150" Height="50" Background="SkyBlue">
<TextBlock>Template A</TextBlock>
</Grid>
</ControlTemplate>
""");
var templateB = XamlHelper.LoadXaml<ControlTemplate>("""
<ControlTemplate>
<Grid x:Name="RootB" Width="150" Height="50" Background="Pink">
<TextBlock>Template B</TextBlock>
</Grid>
</ControlTemplate>
""");

var sut = new TControl();

sut.Template = templateA;
await UITestHelper.Load(sut, x => x.IsLoaded);
sut.FindFirstDescendantOrThrow<Grid>("RootA");

sut.Template = templateB;
await UITestHelper.WaitForIdle();
sut.FindFirstDescendantOrThrow<Grid>("RootB");
}
}
public partial class TemplatedParentTests // helper methods
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,7 @@ private void RemoveTemplateChild()
}
#endif

private UIElement GetFirstChildNoAddRef() => GetFirstChild();

private UIElement GetFirstChild()
internal override UIElement GetFirstChild()
{
UIElement spFirstChild;
// added in UIElement.GetFirstChild()
Expand Down
25 changes: 24 additions & 1 deletion src/Uno.UI/UI/Xaml/Controls/Control/Control.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,30 @@ public ControlTemplate Template

private protected virtual void OnTemplateChanged(DependencyPropertyChangedEventArgs e)
{
#if !UNO_HAS_ENHANCED_LIFECYCLE
#if UNO_HAS_ENHANCED_LIFECYCLE
if (e.OldValue != e.NewValue)
{
// Reset the template bindings for this control
//ClearPropertySubscriptions();

// When the control template property is set, we clear the visual children
var pUIElement = this.GetFirstChild();
if (pUIElement is { })
{
//CFrameworkTemplate* pNewTemplate = NULL;
//if (e.NewValue?.GetType() == valueObject)
//{
// IFC(DoPointerCast(pNewTemplate, args.m_value.AsObject()));
//}
//else if (args.m_value.GetType() != valueNull)
//{
// IFC(E_INVALIDARG);
//}
RemoveChild(pUIElement);
//IFC(GetContext()->RemoveNameScope(this, Jupiter::NameScoping::NameScopeType::TemplateNameScope));
}
}
#else
_updateTemplate = true;
SetUpdateControlTemplate();
#endif
Expand Down
4 changes: 3 additions & 1 deletion src/Uno.UI/UI/Xaml/FrameworkElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1064,7 +1064,9 @@ private protected virtual bool HasTemplateChild()
return GetFirstChild() is not null;
}

private UIElement/*?*/ GetFirstChild()
internal UIElement GetFirstChildNoAddRef() => GetFirstChild();

internal virtual UIElement/*?*/ GetFirstChild()
{
#if __CROSSRUNTIME__ && !__NETSTD_REFERENCE__
if (GetChildren() is { Count: > 0 } children)
Expand Down
Loading