Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BrightShadow/c55ecb2797e7555091e4e7339e52b222 to your computer and use it in GitHub Desktop.
Save BrightShadow/c55ecb2797e7555091e4e7339e52b222 to your computer and use it in GitHub Desktop.
CreateTemplate method for WPFViewModelResolver class.
private DataTemplate CreateTemplate(Type viewType, Type viewModelType)
{
const string xamlTemplate =
"<DataTemplate DataType=\"{{x:Type xViewModels:{0}}}\"><xViews:{1}></xViews:{1}></DataTemplate>";
var xaml = String.Format(xamlTemplate, viewModelType.Name, viewType.Name);
var context = new ParserContext();
context.XamlTypeMapper = new XamlTypeMapper(new string[0]);
context.XamlTypeMapper.AddMappingProcessingInstruction("xViewModels", viewModelType.Namespace, viewModelType.Assembly.FullName);
context.XamlTypeMapper.AddMappingProcessingInstruction("xViews", viewType.Namespace, viewType.Assembly.FullName);
context.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
context.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
context.XmlnsDictionary.Add("xViewModels", "xViewModels");
context.XmlnsDictionary.Add("xViews", "xViews");
return (DataTemplate)XamlReader.Parse(xaml, context);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment