Skip to content

Commit

Permalink
fix(netstd): late template application is ignored
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiaoy312 committed Nov 29, 2024
1 parent 58e477b commit 2293898
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 5 deletions.
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 @@ -207,7 +207,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

0 comments on commit 2293898

Please sign in to comment.