Skip to content

Instantly share code, notes, and snippets.

@robfe
Created January 8, 2012 23:46
Show Gist options
  • Select an option

  • Save robfe/1580167 to your computer and use it in GitHub Desktop.

Select an option

Save robfe/1580167 to your computer and use it in GitHub Desktop.
t4 template for strong access to all your AppBarButtons in a wp7 Page
<#@ template language="C#" hostspecific="True" debug="false" #>
<#@ import namespace="System.IO"#>
<#@ import namespace="System.Linq"#>
<#@ import namespace="System.Xml.Linq"#>
<#@ import namespace="System.Collections.Generic"#>
<#@ import namespace="System.Runtime.Remoting.Messaging"#>
<#@ import namespace="Microsoft.VisualStudio.TextTemplating"#>
<#@ assembly name="System.Core.dll"#>
<#@ assembly name="System.Xml.Linq.dll"#>
<#@ assembly name="System.Xml.dll"#>
<#@ output extension=".Generated.cs" #>
using Microsoft.Phone.Shell;
<#
XNamespace xaml = "http://schemas.microsoft.com/winfx/2006/xaml";
XNamespace shell = "clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone";
foreach(string xamlFile in Directory.EnumerateFiles(Host.ResolvePath(""), "*.xaml", SearchOption.AllDirectories))
{
var doc = XDocument.Load(xamlFile);
var xclass = (string)doc.Root.Attribute(xaml+"Class");
if(xclass == null)
{
continue;
}
var ns = xclass.Substring(0, xclass.LastIndexOf('.'));
var className = xclass.Substring(xclass.LastIndexOf('.')+1);
#>
namespace <#=ns#>
{
partial class <#=className#>
{
<#
int i = 0;
foreach(var button in doc.Descendants(shell+"ApplicationBarIconButton"))
{
var buttonName = "AppBarButton_"+((string)button.Attribute("Text")).Replace(" ", "");
#>
protected ApplicationBarIconButton <#=buttonName#>
{
get
{
return (ApplicationBarIconButton) ApplicationBar.Buttons[<#=i++#>];
}
}
<#
}
#>
}
}
<#
}
#>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment