Skip to content

Commit

Permalink
Performance optimization for Windows-XAML#902 issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Opiumtm committed May 3, 2016
1 parent ef73476 commit b9734ae
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
48 changes: 46 additions & 2 deletions Template10 (Library)/Behaviors/EllipsisBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
using Windows.UI.Xaml.Controls;
using Template10.Utils;
using System.Linq;
using System.Runtime.CompilerServices;
using Windows.Foundation.Collections;
using Windows.UI.Xaml.Media;
using Template10.Controls;

namespace Template10.Behaviors
{
Expand Down Expand Up @@ -46,10 +49,51 @@ public Visibilities Visibility
DependencyProperty.Register(nameof(Visibility), typeof(Visibilities),
typeof(EllipsisBehavior), new PropertyMetadata(Visibilities.Auto));

private void Update()
private Button FindButtonInternal()
{
return (commandBar as PageHeader)?.GetMoreButton();
}

private Button FindButtonByName()
{
if (VisualTreeHelper.GetChildrenCount(commandBar) > 0)
{
var child = VisualTreeHelper.GetChild(commandBar, 0) as FrameworkElement; /* Templated root */
return child?.FindName("MoreButton") as Button;
}
return null;
}

private Button FindButtonByTreeEnum()
{
var controls = XamlUtils.AllChildren<Control>(commandBar);
var button = controls.OfType<Button>().FirstOrDefault(x => x.Name.Equals("MoreButton"));
return controls.OfType<Button>().FirstOrDefault(x => x.Name.Equals("MoreButton"));
}

private Button FindButton()
{
if (commandBar == null)
{
return null;
}
var r = FindButtonInternal(); // try get button from internal cached value
// most optimized scenario for PageHeader control, no WinRT interop calls
if (r != null)
{
return r;
}
r = FindButtonByName(); // try find button by name from templated root
// minimized WinRT interop calls (3 calls) for MUCH better performance
if (r != null)
{
return r;
}
return FindButtonByTreeEnum(); // fallback method - many WinRT interop calls so it is very expensive
}

private void Update()
{
var button = FindButton();
if (button == null)
return;
switch (Visibility)
Expand Down
13 changes: 13 additions & 0 deletions Template10 (Library)/Controls/PageHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,18 @@ public string Text
{
(d as PageHeader).Content = e.NewValue;
}));

private Button moreButton;

protected override void OnApplyTemplate()
{
base.OnApplyTemplate();
moreButton = GetTemplateChild("MoreButton") as Button;
}

internal Button GetMoreButton()
{
return moreButton;
}
}
}

0 comments on commit b9734ae

Please sign in to comment.