Permalink
Browse files

handle types from custom namespaces

  • Loading branch information...
svick committed Sep 27, 2011
1 parent 8ec5931 commit 17e66d0130931cdd4134f30787479de00869967c
View
@@ -34,13 +34,15 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
+ <Reference Include="PresentationFramework" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
+ <Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
View
@@ -1,4 +1,5 @@
using System;
+using System.Windows.Controls;
using XamlConversion;
namespace Demo_app
@@ -22,6 +23,12 @@ static void Main()
</ListView.View>
</ListView>";
Console.WriteLine(new XamlConvertor().ConvertToString(xaml));
+
+ xaml = @"<ListView xmlns:local=""clr-namespace:Demo_app""><ListView.View><local:CustomView /></ListView.View></ListView>";
+ Console.WriteLine(new XamlConvertor().ConvertToString(xaml));
}
}
+
+ class CustomView : ViewBase
+ {}
}
@@ -22,6 +22,9 @@ protected override void ParseName(XName name)
protected override void ParseAttribute(XAttribute attribute)
{
+ if (attribute.IsNamespaceDeclaration)
+ return;
+
var propertyName = attribute.Name.LocalName;
SetProperty(VariableName, Type, propertyName, attribute.Value);
}
@@ -3,7 +3,7 @@
using System.ComponentModel;
using System.Globalization;
using System.Windows.Data;
-using System.Windows.Markup;
+using System.Xaml.Schema;
using System.Xml.Linq;
namespace XamlConversion.Parsers
@@ -31,7 +31,8 @@ protected static Type GetTypeFromXName(XName xName)
string ns = xName.Namespace.NamespaceName;
if (string.IsNullOrEmpty(ns))
ns = "http://schemas.microsoft.com/netfx/2007/xaml/presentation";
- return XamlTypeMapper.DefaultMapper.GetType(ns, xName.LocalName);
+ var xamlSchemaContext = new XamlSchemaContextWithDefault();
+ return xamlSchemaContext.GetXamlType(new XamlTypeName(ns, xName.LocalName)).UnderlyingType;
}
protected static Type GetPropertyType(string name, Type type)
@@ -52,6 +52,7 @@
<Compile Include="Parsers\XamlParser.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="XamlConvertor.cs" />
+ <Compile Include="XamlSchemaContextWithDefault.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
@@ -0,0 +1,34 @@
+using System.Collections.Generic;
+using System.Reflection;
+using System.Xaml;
+using System.Linq;
+
+namespace XamlConversion
+{
+ class XamlSchemaContextWithDefault : XamlSchemaContext
+ {
+ private readonly Assembly m_defaultAssembly;
+
+ public XamlSchemaContextWithDefault() : this(Assembly.GetEntryAssembly())
+ {}
+
+ public XamlSchemaContextWithDefault(Assembly defaultAssembly)
+ : base(GetReferenceAssemblies())
+ {
+ m_defaultAssembly = defaultAssembly;
+ }
+
+ static IEnumerable<Assembly> GetReferenceAssemblies()
+ {
+ return new[] { "WindowsBase", "PresentationCore", "PresentationFramework" }.Select(an => Assembly.LoadWithPartialName(an));
+ }
+
+ protected override Assembly OnAssemblyResolve(string assemblyName)
+ {
+ if (string.IsNullOrEmpty(assemblyName))
+ return m_defaultAssembly;
+
+ return base.OnAssemblyResolve(assemblyName);
+ }
+ }
+}

0 comments on commit 17e66d0

Please sign in to comment.