Skip to content

Instantly share code, notes, and snippets.

@vmilev
Created March 1, 2016 08:24
Show Gist options
  • Save vmilev/8af6c5a413322ed6091b to your computer and use it in GitHub Desktop.
Save vmilev/8af6c5a413322ed6091b to your computer and use it in GitHub Desktop.
Share Code Blog 3
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!--
Insert the following code into your project file:
<Import Project="PreprocessXaml.targets" />
-->
<PropertyGroup>
<MarkupCompilePass1DependsOn>
$(MarkupCompilePass1DependsOn)
PreprocessXaml;
</MarkupCompilePass1DependsOn>
</PropertyGroup>
<Target Name="PreprocessXaml">
<!-- Run the preprocessor on every page that is added to the project as link. The processed file will be saved at the link position. -->
<Exec Condition="('%(Page.Link)' != '')" Command="XamlPreprocessor.exe &quot;%(Page.FullPath)&quot; &quot;%(Page.Link)&quot;"/>
<ItemGroup>
<ProcessedPages Include="@(Page->'%(Link)')" />
<!-- Remove the original (linked) pages so that they are not compiled. -->
<Page Remove="@(Page)" Condition="('%(Page.Link)' != '')" />
<!-- Include the processed pages instead. -->
<Page Include="@(ProcessedPages)" />
</ItemGroup>
</Target>
</Project>
#if WPF
using System.Windows;
using System.Windows.Controls;
#endif
#if NETFX_CORE
using Windows.UI.Xaml.Controls;
#endif
namespace MyApp.Controls
{
public sealed class MyTemplatedControl : Control
{
public MyTemplatedControl()
{
#if NETFX_CORE
this.DefaultStyleKey = typeof(MyTemplatedControl);
#endif
}
#if WPF
static MyTemplatedControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyTemplatedControl), new FrameworkPropertyMetadata(typeof(MyTemplatedControl)));
}
#endif
}
}
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyApp.Controls">
<Style TargetType="local:MyTemplatedControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:MyTemplatedControl">
<Border
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
namespace XamlPreprocessor
{
public class XamlPreprocessor
{
const string regex = @"""using:(?<namespace>[\w\.\-_]*)""";
public static void Main(string[] args)
{
string unprocessedXaml = File.ReadAllText(args[0]);
//Create destination directory if it doesn't exist
(new FileInfo(args[1])).Directory.Create();
var preprocessor = new XamlPreprocessor();
var processedXaml = preprocessor.Process(unprocessedXaml);
File.WriteAllText(args[1], processedXaml);
}
public string Process(string text)
{
return Regex.Replace(text, regex, RegExMatchEvaluator);
}
private string RegExMatchEvaluator(Match match)
{
return match.Result("\"clr-namespace:" + match.Groups["namespace"] + "\"");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment