Created
December 22, 2014 17:41
-
-
Save Jalalx/da15740755588a65e643 to your computer and use it in GitHub Desktop.
Install t4toolbox before using this code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<#@ template language="C#" debug="false" hostspecific="false" #> | |
<#@ output extension=".txt" #> | |
<#@ assembly name="System" #> | |
<#@ assembly name="System.Core" #> | |
<#@ assembly name="System.Data.Entity" #> | |
<#@ assembly name="EnvDTE" #> | |
<#@ include file="t4toolbox.tt" #> | |
<#@ import namespace="EnvDTE" #> | |
<#@ import namespace="System" #> | |
<#@ import namespace="System.Collections" #> | |
<#@ import namespace="System.Collections.Generic" #> | |
<#@ import namespace="System.Linq" #> | |
<#@ import namespace="System.Text" #> | |
<#@ import namespace="System.Reflection" #> | |
<# | |
// specify the namespace where your models live... | |
var targetNamespace = "Caliburn.Micro_WPF_Application1.Models"; | |
var dte = (DTE)TransformationContext.Current.GetService(typeof(DTE)); | |
var project = dte.Solution.FindProjectItem(TransformationContext.Current.Host.TemplateFile).ContainingProject; | |
var classes = FindClasses(project, targetNamespace, ""); | |
#> | |
<# foreach (CodeClass c in classes) { #> | |
<!-- View generated for Views\\<#= c.Name #>View.xaml --> | |
<UserControl x:Class="Caliburn.Micro_WPF_Application1.Views.MainView" | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | |
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | |
mc:Ignorable="d" | |
d:DesignHeight="300" d:DesignWidth="300"> | |
<Grid Width="300" Height="300"> | |
<Grid.ColumnDefinitions> | |
<ColumnDefinition Width="100" /> | |
<ColumnDefinition Width="*" /> | |
</Grid.ColumnDefinitions> | |
<# var properties = c.Members.OfType<EnvDTE.CodeProperty>() | |
.Where(p => p.Access.HasFlag(vsCMAccess.vsCMAccessPublic)) | |
.OrderBy(p => p.Name).ToArray(); | |
#> | |
<Grid.RowDefinitions> | |
<#for(int i = 1; i <= properties.Count(); i++){ #> | |
<RowDefinition Height="27" /> | |
<# }#> | |
</Grid.RowDefinitions> | |
<#for(int i = 1; i <= properties.Count(); i++){ | |
#> <Label Grid.Column="0" Grid.Row="<#= i - 1 #>" HorizontalAlignment="Right">First Name:</Label> | |
<TextBox x:Name="<#= properties[i - 1].Name#>" HorizontalAlignment="Left" Grid.Column="1" Grid.Row="<#= i - 1 #>" Width="150" Margin="2" /> | |
<# }#> | |
<# }#> | |
</Grid> | |
</UserControl> | |
<#+ | |
List<CodeClass> FindClasses(Project project, string ns, string className) { | |
List<CodeClass> result = new List<CodeClass>(); | |
FindClasses(project.CodeModel.CodeElements, className, ns, result, false); | |
return result; | |
} | |
void FindClasses(CodeElements elements, string className, string searchNamespace, List<CodeClass> result, bool isNamespaceOk) { | |
if (elements == null) return; | |
foreach (CodeElement element in elements) { | |
if (element is CodeNamespace) { | |
CodeNamespace ns = element as CodeNamespace; | |
if (ns != null) { | |
if (ns.FullName == searchNamespace) | |
FindClasses(ns.Members, className, searchNamespace, result, true); | |
else | |
FindClasses(ns.Members, className, searchNamespace, result, false); | |
} | |
} else if (element is CodeClass && isNamespaceOk) { | |
CodeClass c = element as CodeClass; | |
if (c != null) { | |
if (c.FullName.Contains(className)) | |
result.Add(c); | |
FindClasses(c.Members, className, searchNamespace, result, true); | |
} | |
} | |
} | |
} | |
#> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<#@ template language="C#" debug="false" hostspecific="false" #> | |
<#@ output extension=".txt" #> | |
<#@ assembly name="System" #> | |
<#@ assembly name="System.Core" #> | |
<#@ assembly name="System.Data.Entity" #> | |
<#@ assembly name="EnvDTE" #> | |
<#@ import namespace="EnvDTE" #> | |
<#@ include file="T4Toolbox.tt" #> | |
<#@ import namespace="System" #> | |
<#@ import namespace="System.Collections" #> | |
<#@ import namespace="System.Collections.Generic" #> | |
<#@ import namespace="System.Linq" #> | |
<#@ import namespace="System.Text" #> | |
<#@ import namespace="System.Reflection" #> | |
<# | |
// specify the namespace where your models live... | |
var targetNamespace = "Caliburn.Micro_WPF_Application1.Models"; | |
var generateSaveCommands = true; | |
var dte = (DTE)TransformationContext.Current.GetService(typeof(DTE)); | |
var project = dte.Solution.FindProjectItem(TransformationContext.Current.Host.TemplateFile).ContainingProject; | |
var classes = FindClasses(project, targetNamespace, ""); | |
#> | |
public class ViewModelBase<T> : NotifyPropertyChangedBase | |
{ | |
public ViewModelBase(T model) | |
{ | |
this.Model = model; | |
} | |
public T Model { get; protected set; } | |
} | |
<# foreach (CodeClass c in classes) { #> | |
public class <#= c.Name #>ViewModel : ViewModelBase<<#= c.Name #>> | |
{ | |
<# var properties = c.Members.OfType<EnvDTE.CodeProperty>() | |
.Where(p => p.Access.HasFlag(vsCMAccess.vsCMAccessPublic)) | |
.OrderBy(p => p.Name); | |
#> | |
public <#= c.Name #>ViewModel(<#= c.Name #> model):base(model) | |
{ | |
} | |
<# | |
foreach (var prop in properties) { | |
#> | |
public <#= prop.Type.AsString #> <#= prop.Name #> | |
{ | |
get { return Model.<#= prop.Name #>; } | |
set | |
{ | |
if(value != Model.<#= prop.Name #>) | |
{ | |
Model.<#= prop.Name #> = value; | |
RaiseOnPropertyChanged(() => <#= prop.Name #>); | |
} | |
} | |
} | |
<# } #> | |
<# if(generateSaveCommands) { #> | |
private ICommand _saveCommand; | |
public ICommand SaveCommand | |
{ | |
get | |
{ | |
if(_saveCommand == null) | |
_saveCommand = new ActionCommand(Save, CanSave); | |
return _saveCommand; | |
} | |
} | |
public bool CanSave() | |
{ | |
return this.Model != null; | |
} | |
private void Save() | |
{ | |
} | |
<# } #> | |
} | |
<# } #> | |
<#+ | |
List<CodeClass> FindClasses(Project project, string ns, string className) { | |
List<CodeClass> result = new List<CodeClass>(); | |
FindClasses(project.CodeModel.CodeElements, className, ns, result, false); | |
return result; | |
} | |
void FindClasses(CodeElements elements, string className, string searchNamespace, List<CodeClass> result, bool isNamespaceOk) { | |
if (elements == null) return; | |
foreach (CodeElement element in elements) { | |
if (element is CodeNamespace) { | |
CodeNamespace ns = element as CodeNamespace; | |
if (ns != null) { | |
if (ns.FullName == searchNamespace) | |
FindClasses(ns.Members, className, searchNamespace, result, true); | |
else | |
FindClasses(ns.Members, className, searchNamespace, result, false); | |
} | |
} else if (element is CodeClass && isNamespaceOk) { | |
CodeClass c = element as CodeClass; | |
if (c != null) { | |
if (c.FullName.Contains(className)) | |
result.Add(c); | |
FindClasses(c.Members, className, searchNamespace, result, true); | |
} | |
} | |
} | |
} | |
#> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment