Skip to content

Commit 1f71527

Browse files
Add more to TabViewItem Adorner sample Animations + handling unloading (close)
1 parent 558d40c commit 1f71527

File tree

5 files changed

+58
-22
lines changed

5 files changed

+58
-22
lines changed

components/Adorners/samples/AdornersTabBadgeSample.xaml

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
<Page x:Class="AdornersExperiment.Samples.AdornersTabBadgeSample"
33
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
44
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5+
xmlns:animations="using:CommunityToolkit.WinUI.Animations"
56
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
67
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
78
xmlns:muxc="using:Microsoft.UI.Xaml.Controls"
89
xmlns:ui="using:CommunityToolkit.WinUI"
910
mc:Ignorable="d">
1011

11-
<muxc:TabView>
12+
<muxc:TabView TabCloseRequested="TabView_TabCloseRequested">
1213
<muxc:TabViewItem Header="Home">
1314
<ui:AdornerLayer.Xaml>
1415
<muxc:InfoBadge Margin="-4,-8"
@@ -17,7 +18,32 @@
1718
IsHitTestVisible="False"
1819
Opacity="0.9"
1920
Visibility="{x:Bind IsAdornerVisible, Mode=OneWay}"
20-
Value="3" />
21+
Value="3">
22+
<animations:Implicit.ShowAnimations>
23+
<animations:RotationInDegreesAnimation From="45"
24+
To="0"
25+
Duration="0:0:1" />
26+
<animations:TranslationAnimation From="0, 1, 0"
27+
To="0"
28+
Duration="0:0:1" />
29+
<animations:OpacityAnimation From="0"
30+
To="1.0"
31+
Duration="0:0:1" />
32+
</animations:Implicit.ShowAnimations>
33+
34+
<animations:Implicit.HideAnimations>
35+
<animations:OpacityAnimation To="0.0"
36+
Duration="0:0:1" />
37+
<animations:ScalarAnimation Target="Translation.Y"
38+
To="5"
39+
Duration="0:0:1">
40+
<animations:ScalarKeyFrame Key="0.1"
41+
Value="-10" />
42+
<animations:ScalarKeyFrame Key="0.5"
43+
Value="0.0" />
44+
</animations:ScalarAnimation>
45+
</animations:Implicit.HideAnimations>
46+
</muxc:InfoBadge>
2147
</ui:AdornerLayer.Xaml>
2248
</muxc:TabViewItem>
2349
<muxc:TabViewItem Header="Document 1" />

components/Adorners/samples/AdornersTabBadgeSample.xaml.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,9 @@ public AdornersTabBadgeSample()
1313
{
1414
this.InitializeComponent();
1515
}
16+
17+
private void TabView_TabCloseRequested(TabView sender, TabViewTabCloseRequestedEventArgs args)
18+
{
19+
sender.TabItems.Remove(args.Tab);
20+
}
1621
}

components/Adorners/samples/Dependencies.props

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,13 @@
99
For UWP / WinAppSDK / Uno packages, place the package references here.
1010
-->
1111
<Project>
12-
<!-- WinUI 2 / UWP -->
13-
<ItemGroup Condition="'$(IsUwp)' == 'true'">
14-
<!-- <PackageReference Include="Microsoft.Toolkit.Uwp.UI.Controls.Primitives" Version="7.1.2"/> -->
15-
</ItemGroup>
12+
<!-- WinUI 2 / UWP / Uno -->
13+
<ItemGroup Condition="'$(IsUwp)' == 'true' OR ('$(IsUno)' == 'true' AND '$(WinUIMajorVersion)' == '2')">
14+
<PackageReference Include="CommunityToolkit.Uwp.Animations" Version="8.2.250402"/>
15+
</ItemGroup>
1616

17-
<!-- WinUI 2 / Uno -->
18-
<ItemGroup Condition="'$(IsUno)' == 'true' AND '$(WinUIMajorVersion)' == '2'">
19-
<!-- <PackageReference Include="Uno.Microsoft.Toolkit.Uwp.UI.Controls.Primitives" Version="7.1.11"/> -->
20-
</ItemGroup>
21-
22-
<!-- WinUI 3 / WinAppSdk -->
23-
<ItemGroup Condition="'$(IsWinAppSdk)' == 'true'">
24-
<!-- <PackageReference Include="CommunityToolkit.WinUI.UI.Controls.Primitives" Version="7.1.2"/> -->
25-
</ItemGroup>
26-
27-
<!-- WinUI 3 / Uno -->
28-
<ItemGroup Condition="'$(IsUno)' == 'true' AND '$(WinUIMajorVersion)' == '3'">
29-
<!-- <PackageReference Include="Uno.CommunityToolkit.WinUI.UI.Controls.Primitives" Version="7.1.100-dev.15.g12261e2626"/> -->
30-
</ItemGroup>
17+
<!-- WinUI 3 / WinAppSdk / Uno -->
18+
<ItemGroup Condition="'$(IsWinAppSdk)' == 'true' OR ('$(IsUno)' == 'true' AND '$(WinUIMajorVersion)' == '3')">
19+
<PackageReference Include="CommunityToolkit.WinUI.Animations" Version="8.2.250402"/>
20+
</ItemGroup>
3121
</Project>

components/Adorners/src/Adorner.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ private void OnAdornedElementChanged(UIElement? oldvalue, UIElement? newvalue)
6262
// Initial size & layout update
6363
OnSizeChanged(null, null!);
6464
OnLayoutUpdated(null, null!);
65+
66+
// Track if AdornedElement is unloaded
67+
var weakPropertyChangedListenerUnloaded = new WeakEventListener<Adorner, object, RoutedEventArgs>(this)
68+
{
69+
OnEventAction = static (instance, source, eventArgs) => instance.OnUnloaded(source, eventArgs),
70+
OnDetachAction = (weakEventListener) => newfe.Unloaded -= weakEventListener.OnEvent // Use Local References Only
71+
};
72+
newfe.Unloaded += weakPropertyChangedListenerUnloaded.OnEvent;
6573
}
6674
}
6775

@@ -86,6 +94,13 @@ internal void OnLayoutUpdated(object? sender, object e)
8694
}
8795
}
8896

97+
private void OnUnloaded(object source, RoutedEventArgs eventArgs)
98+
{
99+
if (AdornerLayer is null) return;
100+
101+
AdornerLayer.RemoveAdorner(AdornerLayer, this);
102+
}
103+
89104
internal AdornerLayer? AdornerLayer { get; set; }
90105

91106
/// <summary>

components/Adorners/src/AdornerLayer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,9 @@ private static void AttachAdorner(AdornerLayer layer, FrameworkElement adornedEl
245245
layer.Children.Add(adorner);
246246
}
247247

248-
private static void RemoveAdorner(AdornerLayer layer, UIElement adornerXaml)
248+
internal static void RemoveAdorner(AdornerLayer layer, UIElement adornerXaml)
249249
{
250-
var adorner = adornerXaml.FindAscendant<Adorner>();
250+
var adorner = adornerXaml.FindAscendantOrSelf<Adorner>();
251251

252252
if (adorner != null)
253253
{

0 commit comments

Comments
 (0)