├── .gitattributes ├── .gitignore ├── .gitmodules ├── Create-NuGetPackages.ps1 ├── LICENSE.md ├── OmniXaml.Attributes ├── ContentAttribute.cs ├── DependsOnAttribute.cs ├── FragmentLoaderAttribute.cs ├── NameAttribute.cs ├── NamescopeAttribute.cs ├── OmniXaml.Attributes.csproj ├── TypeConverterMember.cs ├── XmlnsDefinitionAttribute.cs └── XmlnsPrefixAttribute.cs ├── OmniXaml.Services ├── AttributeBasedMetadataProvider.cs ├── AttributeBasedStringValueConverter.cs ├── AttributeBasedTypeDirectory.cs ├── AttributeBasedValueConverter.cs ├── BasicXamlLoader.cs ├── DirectCompatibilitySourceValueConverter.cs ├── ExtendedXamlLoader.cs ├── IXamlLoader.cs ├── MarkupExtensionValuePipeline.cs ├── NoActionValuePipeline.cs ├── NodeToObjectBuilder.cs ├── OmniXaml.Services.csproj ├── SimpleInstanceCreator.cs ├── TemplatePipeline.cs ├── TwoPassesNodeAssembler.cs └── XamlLoader.cs ├── OmniXaml.Tests ├── AssemblyInfo.cs ├── FuncAssignmentApplier.cs ├── FuncInstanceCreator.cs ├── FuncStringConverter.cs ├── FuncStringConverterExtended.cs ├── InstanceCreatorMock.cs ├── Integration │ └── XamlTests.cs ├── MemberAssigmentApplierTests.cs ├── MemberAssignmentTests.cs ├── Model │ ├── AttachedEventArgs.cs │ ├── Button.cs │ ├── Collection.cs │ ├── CollectionExtension.cs │ ├── ConstructionFragmentLoader.cs │ ├── Converters.cs │ ├── Custom │ │ ├── CustomControl.cs │ │ ├── CustomGrid.cs │ │ ├── CustomWindow.cs │ │ └── ParametrizedExtension.cs │ ├── DataTemplate.cs │ ├── Grid.cs │ ├── ItemsControl.cs │ ├── ModelObject.cs │ ├── MyImmutable.cs │ ├── Reference.cs │ ├── ReferenceTarget.cs │ ├── ResourceDictionary.cs │ ├── Setter.cs │ ├── SimpleExtension.cs │ ├── TemplateContent.cs │ ├── TestWindow.cs │ ├── TextBlock.cs │ ├── TextWrapping.cs │ ├── VisualStateGroup.cs │ ├── VisualStateManager.cs │ └── Window.cs ├── ObjectAssemblerTests.cs ├── OmniXaml.Tests.csproj ├── Phase2BuilderTests.cs ├── PipelineMock.cs ├── SimpleValueConverter.cs ├── StringValueConverterTests.cs ├── TypeDirectoryTests.cs └── XmlToNodes │ ├── AssignmentExtractorTests.cs │ ├── CData.cs │ ├── ClassDirective.cs │ ├── CollectionsTests.cs │ ├── DictionaryTests.cs │ ├── EventsTests.cs │ ├── StandardTests.cs │ └── XamlToTreeParserTestsBase.cs ├── OmniXaml.sln ├── OmniXaml ├── Ambient │ ├── AmbientMemberAssignment.cs │ ├── AmbientRegistrator.cs │ └── IAmbientRegistrator.cs ├── Annotator.cs ├── Assignment.cs ├── AssignmentExtractor.cs ├── AttachedEvent.cs ├── AttachedProperty.cs ├── AttributeBasedInstanceProperties.cs ├── BuildContext.cs ├── BuiltInConversionParser.cs ├── ComponentModelTypeConverterBasedSourceValueConverter.cs ├── ConstructionNode.cs ├── ConvertContext.cs ├── ConverterValueContext.cs ├── CreationHints.cs ├── CreationResult.cs ├── DelegateParser.cs ├── Dependency.cs ├── Directive.cs ├── DirectiveExtractor.cs ├── ExtensionValueContext.cs ├── IAssignmentExtractor.cs ├── IBuildContext.cs ├── IContentPropertyRegistry.cs ├── IConverterContextFactory.cs ├── IFullObjectBuilder.cs ├── IInlineParser.cs ├── IInstanceCreator.cs ├── IInstanceHolder.cs ├── IInstanceLifecycleSignaler.cs ├── IMarkupExtension.cs ├── IMemberAssigmentApplier.cs ├── INamescopeAnnotator.cs ├── INodeAssembler.cs ├── INodeToObjectBuilder.cs ├── IPrefixAnnotator.cs ├── IPrefixedTypeResolver.cs ├── ISourceValueConverter.cs ├── IStringSourceValueConverter.cs ├── IValueConverter.cs ├── IValuePipeline.cs ├── IXamlToTreeParser.cs ├── IXmlTypeResolver.cs ├── InjectableMember.cs ├── InlineParser.cs ├── InlineParsers │ └── Extensions │ │ ├── AssignmentNode.cs │ │ ├── IdentifierNode.cs │ │ ├── MarkupExtensionNode.cs │ │ ├── MarkupExtensionNodeToConstructionNodeConverter.cs │ │ ├── MarkupExtensionParser.cs │ │ ├── Option.cs │ │ ├── OptionsCollection.cs │ │ ├── PositionalOption.cs │ │ ├── PropertyOption.cs │ │ ├── StringNode.cs │ │ └── TreeNode.cs ├── InstanceLifecycleSignaler.cs ├── KeyedInstance.cs ├── Member.cs ├── MemberAssigmentApplier.cs ├── MemberAssignment.cs ├── Metadata │ ├── DependencyRegistration.cs │ ├── DependencyRegistrations.cs │ ├── FragmentLoaderInfo.cs │ ├── GenericMetadata.cs │ ├── IConstructionFragmentLoader.cs │ ├── IMetadataProvider.cs │ ├── Metadata.cs │ └── MetadataProvider.cs ├── MutablePipelineUnit.cs ├── Namescope.cs ├── NamescopeAnnotator.cs ├── NamescopeX.cs ├── NodeAssembler.cs ├── NodeAssignment.cs ├── NodeExtensions.cs ├── ObjectBuilderContext.cs ├── OmniXaml.csproj ├── ParseException.cs ├── ParseResult.cs ├── PositionalParameter.cs ├── PrefixAnnotator.cs ├── PrefixedTypeResolver.cs ├── PrimitiveParser.cs ├── SourceValueConverter.cs ├── SourceValueConverterTypeDescriptorContext.cs ├── StandardEvent.cs ├── StandardProperty.cs ├── StringUtils.cs ├── StringValueConverter.cs ├── SuperSmartSourceValueConverter.cs ├── TypeExtensions.cs ├── TypeLocation │ ├── Address.cs │ ├── AddressPack.cs │ ├── AssemblyNameConfig.cs │ ├── ClrNamespace.cs │ ├── ConfiguredAssembly.cs │ ├── ConfiguredAssemblyWithNamespaces.cs │ ├── ITypeDirectory.cs │ ├── Namespace.cs │ ├── PrefixDeclaration.cs │ ├── Route.cs │ ├── TypeDirectory.cs │ ├── TypeLocationException.cs │ └── XamlNamespace.cs ├── TypeNotFoundException.cs ├── ValueContext.cs ├── ValuePipeline.cs ├── XamlParserException.cs ├── XamlToTreeParser.cs └── XmlTypeXmlTypeResolver.cs ├── README.md ├── SampleModel ├── Model.xml ├── Model │ ├── Animal.cs │ └── Zoo.cs ├── Program.cs ├── Properties │ └── AssemblyInfo.cs ├── SampleModel.xproj ├── project.fragment.lock.json └── project.json ├── UniversalApp ├── Adapters │ ├── ConstructionFragmentLoader.cs │ ├── OmniDataTemplate.cs │ └── TemplateContent.cs ├── App.xaml ├── App.xaml.cs ├── Assets │ ├── LockScreenLogo.scale-200.png │ ├── SplashScreen.scale-200.png │ ├── Square150x150Logo.scale-200.png │ ├── Square44x44Logo.scale-200.png │ ├── Square44x44Logo.targetsize-24_altform-unplated.png │ ├── StoreLogo.png │ └── Wide310x150Logo.scale-200.png ├── Context │ ├── MetadataProvider.cs │ ├── Registrator.cs │ └── XamlLoader.cs ├── MainWindow.xml ├── Package.appxmanifest ├── Properties │ ├── AssemblyInfo.cs │ └── Default.rd.xml ├── Sample.xml ├── UwpApp.csproj ├── mario.png └── project.json ├── WpfApp ├── App.config ├── App.xaml ├── App.xaml.cs ├── Context │ ├── BasicXamlLoader.cs │ └── ContentPropertyRegistry.cs ├── MainWindow.xml ├── MyExtension.cs ├── Properties │ ├── AssemblyInfo.cs │ ├── Resources.Designer.cs │ ├── Resources.resx │ ├── Settings.Designer.cs │ └── Settings.settings ├── Sample.xml ├── TestingWindow.xaml ├── TestingWindow.xaml.cs ├── WpfApp.csproj ├── mario.png └── project.json ├── XamlLoadTest ├── AssemblyProperties.cs ├── Converters.cs ├── Program.cs ├── Properties │ └── launchSettings.json ├── XamlLoadTest.csproj └── test.xaml └── appveyor.yml /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "Zafiro"] 2 | path = Zafiro 3 | url = https://github.com/SuperJMN/Zafiro.git 4 | -------------------------------------------------------------------------------- /Create-NuGetPackages.ps1: -------------------------------------------------------------------------------- 1 | [CmdletBinding()] 2 | Param( 3 | [Parameter(Mandatory=$True, Position=1)] 4 | [string]$Version 5 | ) 6 | 7 | function CreatePackage($file, $version) 8 | { 9 | Write-Host 'Building package inside ' $file.DirectoryName 10 | & 'nuget.exe' 'pack' $file.FullName -Version $version 11 | } 12 | 13 | $csprojs = Get-ChildItem -Filter "*.csproj" -Recurse 14 | 15 | foreach ($proj in $csprojs) 16 | { 17 | 18 | $jsonPath = ($proj.DirectoryName) + '\project.json' 19 | $json = Get-Item $jsonPath 20 | 21 | $content = Get-Content $json.FullName 22 | $contains = $content -match 'packOptions' 23 | if ($contains) 24 | { 25 | CreatePackage $proj $version 26 | } 27 | } 28 | 29 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 OmniGUI Platform 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /OmniXaml.Attributes/ContentAttribute.cs: -------------------------------------------------------------------------------- 1 | namespace OmniXaml.Attributes 2 | { 3 | using System; 4 | 5 | [AttributeUsage(AttributeTargets.Property)] 6 | public class ContentAttribute : Attribute 7 | { 8 | public string Name { get; set; } 9 | } 10 | } -------------------------------------------------------------------------------- /OmniXaml.Attributes/DependsOnAttribute.cs: -------------------------------------------------------------------------------- 1 | namespace OmniXaml.Attributes 2 | { 3 | using System; 4 | 5 | [AttributeUsage(AttributeTargets.Property)] 6 | public class DependsOnAttribute : Attribute 7 | { 8 | public DependsOnAttribute(string nameOfDependency) 9 | { 10 | Dependency = nameOfDependency; 11 | } 12 | public string Dependency { get; private set; } 13 | } 14 | } -------------------------------------------------------------------------------- /OmniXaml.Attributes/FragmentLoaderAttribute.cs: -------------------------------------------------------------------------------- 1 | namespace OmniXaml.Attributes 2 | { 3 | using System; 4 | 5 | [AttributeUsage(AttributeTargets.Property)] 6 | public class FragmentLoaderAttribute : Attribute 7 | { 8 | public string PropertyName { get; set; } 9 | public Type FragmentLoader { get; set; } 10 | public Type Type { get; set; } 11 | } 12 | } -------------------------------------------------------------------------------- /OmniXaml.Attributes/NameAttribute.cs: -------------------------------------------------------------------------------- 1 | namespace OmniXaml.Attributes 2 | { 3 | using System; 4 | 5 | [AttributeUsage(AttributeTargets.Property)] 6 | public class NameAttribute : Attribute 7 | { 8 | } 9 | } -------------------------------------------------------------------------------- /OmniXaml.Attributes/NamescopeAttribute.cs: -------------------------------------------------------------------------------- 1 | namespace OmniXaml.Attributes 2 | { 3 | using System; 4 | 5 | [AttributeUsage(AttributeTargets.Class, Inherited = false)] 6 | public class NamescopeAttribute : Attribute 7 | { 8 | } 9 | } -------------------------------------------------------------------------------- /OmniXaml.Attributes/OmniXaml.Attributes.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | netstandard1.2 4 | OmniXaml.Attributes 5 | 2.0.0 6 | José Manuel Nieto (@SuperJMN) 7 | The Cross-platform XAML Framework 8 | false 9 | First release 10 | WPF XAML Cross-Platform Avalonia AvaloniaUI 11 | 2.1.0 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /OmniXaml.Attributes/TypeConverterMember.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace OmniXaml.Attributes 4 | { 5 | [AttributeUsage(AttributeTargets.Field)] 6 | public class TypeConverterMember : Attribute 7 | { 8 | public Type SourceType { get; } 9 | 10 | public TypeConverterMember(Type sourceType) 11 | { 12 | SourceType = sourceType; 13 | } 14 | } 15 | } -------------------------------------------------------------------------------- /OmniXaml.Attributes/XmlnsDefinitionAttribute.cs: -------------------------------------------------------------------------------- 1 | namespace OmniXaml.Attributes 2 | { 3 | using System; 4 | 5 | [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] 6 | public sealed class XmlnsDefinitionAttribute : Attribute 7 | { 8 | public string XmlNamespace { get; } 9 | 10 | public string ClrNamespace { get; } 11 | 12 | public string AssemblyName { get; set; } 13 | 14 | public XmlnsDefinitionAttribute(string xmlNamespace, string clrNamespace) 15 | { 16 | if (xmlNamespace == null) 17 | { 18 | throw new ArgumentNullException("xmlNamespace"); 19 | } 20 | 21 | if (clrNamespace == null) 22 | { 23 | throw new ArgumentNullException("clrNamespace"); 24 | } 25 | 26 | XmlNamespace = xmlNamespace; 27 | ClrNamespace = clrNamespace; 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /OmniXaml.Attributes/XmlnsPrefixAttribute.cs: -------------------------------------------------------------------------------- 1 | namespace OmniXaml.Attributes 2 | { 3 | using System; 4 | 5 | [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] 6 | public class XmlnsPrefixAttribute : Attribute 7 | { 8 | public string XmlNamespace { get; } 9 | 10 | /// Gets the recommended prefix associated with this attribute. 11 | /// The recommended prefix string. 12 | public string Prefix { get; } 13 | 14 | public XmlnsPrefixAttribute(string xmlNamespace, string prefix) 15 | { 16 | if (xmlNamespace == null) 17 | { 18 | throw new ArgumentNullException(nameof(xmlNamespace)); 19 | } 20 | 21 | if (prefix == null) 22 | { 23 | throw new ArgumentNullException(nameof(prefix)); 24 | } 25 | 26 | XmlNamespace = xmlNamespace; 27 | Prefix = prefix; 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /OmniXaml.Services/AttributeBasedMetadataProvider.cs: -------------------------------------------------------------------------------- 1 | namespace OmniXaml.Services 2 | { 3 | using System; 4 | using System.Reflection; 5 | using Attributes; 6 | using Zafiro.Core; 7 | using Metadata; 8 | 9 | public class AttributeBasedMetadataProvider : IMetadataProvider 10 | { 11 | public Metadata Get(Type type) 12 | { 13 | return new Metadata 14 | { 15 | ContentProperty = type.GetAttributeFromProperty((info, attribute) => info.Name), 16 | RuntimePropertyName = type.GetAttributeFromProperty((info, attribute) => info.Name), 17 | IsNamescope = type.GetAttributeFromType(attribute => attribute) != null, 18 | FragmentLoaderInfo = type.GetAttributeFromProperty((info, attribute) => GetFragmentLoaderInfo(type, info, attribute)), 19 | PropertyDependencies = GetDependencyRegistrations(type), 20 | }; 21 | } 22 | 23 | private DependencyRegistrations GetDependencyRegistrations(Type type) 24 | { 25 | var regs = type.GetAttributesFromProperties(GetDependencyRegistration); 26 | return new DependencyRegistrations(regs); 27 | } 28 | 29 | private static DependencyRegistration GetDependencyRegistration(PropertyInfo info, DependsOnAttribute attribute) 30 | { 31 | return new DependencyRegistration 32 | { 33 | PropertyName = info.Name, 34 | DependsOn = attribute.Dependency, 35 | }; 36 | } 37 | 38 | private static FragmentLoaderInfo GetFragmentLoaderInfo(Type type, MemberInfo memberInfo, FragmentLoaderAttribute attribute) 39 | { 40 | var constructionFragmentLoader = (IConstructionFragmentLoader) Activator.CreateInstance(attribute.FragmentLoader); 41 | 42 | return new FragmentLoaderInfo 43 | { 44 | Loader = constructionFragmentLoader, 45 | Type = type, 46 | PropertyName = memberInfo.Name, 47 | }; 48 | } 49 | } 50 | } -------------------------------------------------------------------------------- /OmniXaml.Services/AttributeBasedStringValueConverter.cs: -------------------------------------------------------------------------------- 1 | namespace OmniXaml.Services 2 | { 3 | using System.Collections.Generic; 4 | using System.Reflection; 5 | 6 | public class AttributeBasedStringValueConverter : AttributeBasedValueConverter, IStringSourceValueConverter 7 | { 8 | public AttributeBasedStringValueConverter(IList assemblies) : base(assemblies) 9 | { 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /OmniXaml.Services/AttributeBasedTypeDirectory.cs: -------------------------------------------------------------------------------- 1 | namespace OmniXaml.Services 2 | { 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Reflection; 7 | using Attributes; 8 | using TypeLocation; 9 | 10 | public class AttributeBasedTypeDirectory : ITypeDirectory 11 | { 12 | private readonly ITypeDirectory inner; 13 | 14 | public AttributeBasedTypeDirectory(IList assemblies) 15 | { 16 | inner = new TypeDirectory(GetNamespaces(assemblies)); 17 | } 18 | 19 | private IEnumerable GetNamespaces(IEnumerable assemblies) 20 | { 21 | var routes = from a in assemblies 22 | let attributes = a.GetCustomAttributes() 23 | from byNamespace in attributes.GroupBy(arg => arg.XmlNamespace) 24 | let name = byNamespace.Key 25 | let clrNamespaces = byNamespace.Select(arg => arg.ClrNamespace) 26 | let configuredAssemblyWithNamespaces = Route.Assembly(a).WithNamespaces(clrNamespaces.ToArray()) 27 | select new {Ns = name, configuredAssemblyWithNamespaces}; 28 | 29 | var nss = from route in routes 30 | group route by route.Ns 31 | into g 32 | let ns = g.Select(arg => arg.configuredAssemblyWithNamespaces).ToArray() 33 | select XamlNamespace.Map(g.Key).With(ns); 34 | 35 | return nss; 36 | } 37 | 38 | public Type GetTypeByFullAddress(Address address) 39 | { 40 | return inner.GetTypeByFullAddress(address); 41 | } 42 | } 43 | } -------------------------------------------------------------------------------- /OmniXaml.Services/AttributeBasedValueConverter.cs: -------------------------------------------------------------------------------- 1 | namespace OmniXaml.Services 2 | { 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Reflection; 7 | using Attributes; 8 | using Zafiro.Core; 9 | 10 | public class AttributeBasedValueConverter : IValueConverter 11 | { 12 | private readonly IDictionary> converterFuncs = new Dictionary>(); 13 | 14 | public AttributeBasedValueConverter(IList assemblies) 15 | { 16 | AddConvertersFromAssemblies(assemblies); 17 | } 18 | 19 | private void AddConvertersFromAssemblies(IList assemblies) 20 | { 21 | var converterEntries = 22 | from type in assemblies.AllExportedTypes() 23 | from member in type.GetRuntimeFields() 24 | let attr = member.GetCustomAttribute() 25 | where attr != null 26 | where member.IsStatic 27 | select new { type, member, attr.SourceType }; 28 | 29 | foreach (var converterEntry in converterEntries) 30 | { 31 | var staticFieldValue = converterEntry.member.GetValue(null); 32 | var converterFunc = (Func)staticFieldValue; 33 | converterFuncs.Add(converterEntry.SourceType, converterFunc); 34 | } 35 | } 36 | 37 | public (bool, object) Convert(TInput input, Type desiredTargetType, ConvertContext context) 38 | { 39 | if (converterFuncs.ContainsKey(desiredTargetType)) 40 | { 41 | return converterFuncs[desiredTargetType](input, context); 42 | } 43 | 44 | return (false, null); 45 | } 46 | } 47 | 48 | 49 | } -------------------------------------------------------------------------------- /OmniXaml.Services/BasicXamlLoader.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Reflection; 3 | using OmniXaml.Metadata; 4 | 5 | namespace OmniXaml.Services 6 | { 7 | public class BasicXamlLoader : XamlLoader 8 | { 9 | private INodeToObjectBuilder builder; 10 | 11 | public BasicXamlLoader(IList assemblies) 12 | { 13 | Assemblies = assemblies; 14 | } 15 | 16 | public IList Assemblies { get; } 17 | 18 | public override IXamlToTreeParser Parser => new XamlToTreeParser(MetadataProvider, InlineParsers, XmlTypeResolver); 19 | public override IMemberAssigmentApplier AssignmentApplier => new MemberAssigmentApplier(ValuePipeline); 20 | 21 | protected override IValuePipeline ValuePipeline => new NoActionValuePipeline(); 22 | 23 | protected virtual IEnumerable InlineParsers => new List 24 | { 25 | new InlineParser(XmlTypeResolver) 26 | }; 27 | 28 | protected virtual IXmlTypeResolver XmlTypeResolver => new XmlTypeXmlTypeResolver(new AttributeBasedTypeDirectory(Assemblies)); 29 | 30 | protected virtual IMetadataProvider MetadataProvider => new AttributeBasedMetadataProvider(); 31 | 32 | public override IInstanceCreator InstanceCreator => new SimpleInstanceCreator(); 33 | 34 | public override IStringSourceValueConverter StringSourceValueConverter => new SuperSmartSourceValueConverter( 35 | new IStringSourceValueConverter[] 36 | { 37 | new DirectCompatibilitySourceValueConverter(), 38 | new AttributeBasedStringValueConverter(Assemblies), new ComponentModelTypeConverterBasedSourceValueConverter() 39 | }); 40 | 41 | public override INodeToObjectBuilder Builder => builder ?? (builder = CreateBuilder()); 42 | 43 | private INodeToObjectBuilder CreateBuilder() 44 | { 45 | return new NodeToObjectBuilder(new TwoPassesNodeAssembler(new NodeAssembler(InstanceCreator, 46 | StringSourceValueConverter, AssignmentApplier))); 47 | } 48 | } 49 | } -------------------------------------------------------------------------------- /OmniXaml.Services/DirectCompatibilitySourceValueConverter.cs: -------------------------------------------------------------------------------- 1 | namespace OmniXaml.Services 2 | { 3 | using System; 4 | using Zafiro.Core; 5 | 6 | public class DirectCompatibilitySourceValueConverter : IStringSourceValueConverter 7 | { 8 | public (bool, object) Convert(string strValue, Type desiredTargetType, ConvertContext context) 9 | { 10 | if (typeof(string).IsAssignableFrom(desiredTargetType)) 11 | { 12 | return (true, strValue); 13 | } 14 | 15 | return (false, null); 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /OmniXaml.Services/IXamlLoader.cs: -------------------------------------------------------------------------------- 1 | namespace OmniXaml.Services 2 | { 3 | public interface IXamlLoader 4 | { 5 | object Load(string xaml, object intance = null); 6 | IStringSourceValueConverter StringSourceValueConverter { get; } 7 | } 8 | } -------------------------------------------------------------------------------- /OmniXaml.Services/MarkupExtensionValuePipeline.cs: -------------------------------------------------------------------------------- 1 | namespace OmniXaml.Services 2 | { 3 | public class MarkupExtensionValuePipeline : ValuePipeline 4 | { 5 | public MarkupExtensionValuePipeline(IValuePipeline inner) : base(inner) 6 | { 7 | } 8 | 9 | protected override void HandleCore(object parent, Member member, MutablePipelineUnit mutable, INodeToObjectBuilder builder, BuilderContext context) 10 | { 11 | var extension = mutable.Value as IMarkupExtension; 12 | if (extension != null) 13 | { 14 | var keyedInstance = new KeyedInstance(parent); 15 | var assignment = new Assignment(keyedInstance, member, mutable.Value); 16 | var finalValue = extension.GetValue(new ExtensionValueContext(assignment, null, null, null)); 17 | mutable.Value = finalValue; 18 | } 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /OmniXaml.Services/NoActionValuePipeline.cs: -------------------------------------------------------------------------------- 1 | namespace OmniXaml.Services 2 | { 3 | public class NoActionValuePipeline : IValuePipeline 4 | { 5 | public void Handle(object parent, Member member, MutablePipelineUnit mutable, INodeToObjectBuilder builder, BuilderContext context) 6 | { 7 | } 8 | } 9 | } -------------------------------------------------------------------------------- /OmniXaml.Services/NodeToObjectBuilder.cs: -------------------------------------------------------------------------------- 1 | namespace OmniXaml.Services 2 | { 3 | public class NodeToObjectBuilder : INodeToObjectBuilder 4 | { 5 | private readonly INodeAssembler assembler; 6 | 7 | public NodeToObjectBuilder(INodeAssembler assembler) 8 | { 9 | this.assembler = assembler; 10 | } 11 | 12 | public object Build(ConstructionNode node, BuilderContext context = null) 13 | { 14 | assembler.Assemble(node, this, null, context ?? new BuilderContext()); 15 | return node.Instance; 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /OmniXaml.Services/OmniXaml.Services.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | netstandard1.2 4 | OmniXaml.Services 5 | 2.0.0 6 | José Manuel Nieto (@SuperJMN) 7 | The Cross-platform XAML Framework 8 | false 9 | First release 10 | WPF XAML Cross-Platform Avalonia AvaloniaUI 11 | 2.1.0 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /OmniXaml.Services/SimpleInstanceCreator.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace OmniXaml.Services 4 | { 5 | public class SimpleInstanceCreator : IInstanceCreator 6 | { 7 | public CreationResult Create(Type type, CreationHints creationHints) 8 | { 9 | var instance = Activator.CreateInstance(type); 10 | return new CreationResult(instance, new CreationHints()); 11 | } 12 | } 13 | } -------------------------------------------------------------------------------- /OmniXaml.Services/TemplatePipeline.cs: -------------------------------------------------------------------------------- 1 | using OmniXaml.Metadata; 2 | 3 | namespace OmniXaml.Services 4 | { 5 | public class TemplatePipeline : ValuePipeline 6 | { 7 | private readonly IMetadataProvider metadataProvider; 8 | 9 | public TemplatePipeline(IValuePipeline pipeline, IMetadataProvider metadataProvider) : base(pipeline) 10 | { 11 | this.metadataProvider = metadataProvider; 12 | } 13 | 14 | protected override void HandleCore(object parent, Member member, MutablePipelineUnit mutable, INodeToObjectBuilder builder, BuilderContext context) 15 | { 16 | var deferringLoader = metadataProvider.Get(parent.GetType()).FragmentLoaderInfo; 17 | 18 | if (deferringLoader == null) 19 | { 20 | return; 21 | } 22 | 23 | if (IsApplicable(deferringLoader, parent, member)) 24 | { 25 | mutable.Value = deferringLoader.Loader.Load(mutable.ParentNode, builder, context); 26 | } 27 | } 28 | 29 | private static bool IsApplicable(FragmentLoaderInfo loader, object parent, Member member) 30 | { 31 | return loader.Type == parent.GetType() && string.Equals(member.MemberName, loader.PropertyName); 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /OmniXaml.Services/TwoPassesNodeAssembler.cs: -------------------------------------------------------------------------------- 1 | namespace OmniXaml.Services 2 | { 3 | public class TwoPassesNodeAssembler : INodeAssembler 4 | { 5 | private readonly NodeAssembler assembler; 6 | 7 | public TwoPassesNodeAssembler(NodeAssembler nodeAssembler) 8 | { 9 | assembler = nodeAssembler; 10 | } 11 | 12 | public void Assemble(ConstructionNode node, INodeToObjectBuilder nodeToObjectBuilder, ConstructionNode parent = null, BuilderContext context = null) 13 | { 14 | assembler.Assemble(node, nodeToObjectBuilder, parent, context); 15 | assembler.Assemble(node, nodeToObjectBuilder, parent, context); 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /OmniXaml.Services/XamlLoader.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | namespace OmniXaml.Services 6 | { 7 | public abstract class XamlLoader : IXamlLoader 8 | { 9 | public abstract IXamlToTreeParser Parser { get; } 10 | public abstract IMemberAssigmentApplier AssignmentApplier { get; } 11 | 12 | public abstract IInstanceCreator InstanceCreator { get; } 13 | public abstract IStringSourceValueConverter StringSourceValueConverter { get; } 14 | protected abstract IValuePipeline ValuePipeline { get; } 15 | 16 | public abstract INodeToObjectBuilder Builder { get; } 17 | 18 | public object Load(string xaml, object intance = null) 19 | { 20 | var prefixAnnotator = new PrefixAnnotator(); 21 | var constructionNode = Parser.Parse(xaml, prefixAnnotator).Root; 22 | var build = Builder.Build(constructionNode); 23 | if (build == null) 24 | { 25 | var unresolvedNodes = GetUnresolvedNodes(constructionNode); 26 | throw new ParseException("XAML cannot be parsed due to nodes that cannot be resolved:" + string.Join(",", unresolvedNodes)); 27 | } 28 | return build; 29 | } 30 | 31 | private IEnumerable GetUnresolvedNodes(ConstructionNode constructionNode) 32 | { 33 | if (!constructionNode.IsCreated && SubNodesAreCreated(constructionNode)) 34 | { 35 | yield return constructionNode; 36 | } 37 | 38 | foreach (var constructionNodeAssignment in constructionNode.Assignments) 39 | { 40 | var values = constructionNodeAssignment.Values; 41 | foreach (var value in values) 42 | { 43 | foreach (var unresolved in GetUnresolvedNodes(value)) 44 | { 45 | yield return unresolved; 46 | } 47 | } 48 | } 49 | } 50 | 51 | private bool SubNodesAreCreated(ConstructionNode constructionNode) 52 | { 53 | var membersCreated = constructionNode.Assignments.All(ma => ma.Values.All(node => node.IsCreated)); 54 | var childrenCreated = constructionNode.Assignments.All(ma => ma.Values.All(node => node.IsCreated)); 55 | 56 | return membersCreated && childrenCreated; 57 | } 58 | } 59 | } -------------------------------------------------------------------------------- /OmniXaml.Tests/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using OmniXaml.Attributes; 2 | 3 | [assembly: XmlnsDefinition("root", "OmniXaml.Tests.Model")] -------------------------------------------------------------------------------- /OmniXaml.Tests/FuncAssignmentApplier.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace OmniXaml.Tests 4 | { 5 | public class FuncAssignmentApplier : IMemberAssigmentApplier 6 | { 7 | private Action func; 8 | 9 | public FuncAssignmentApplier(Action func) 10 | { 11 | this.func = func; 12 | } 13 | 14 | public void ExecuteAssignment(NodeAssignment assignment, INodeToObjectBuilder builder, BuilderContext context) 15 | { 16 | func(assignment, builder, context); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /OmniXaml.Tests/FuncInstanceCreator.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace OmniXaml.Tests 4 | { 5 | public class FuncInstanceCreator : IInstanceCreator 6 | { 7 | private readonly Func func; 8 | 9 | public FuncInstanceCreator(Func func) 10 | { 11 | this.func = func; 12 | } 13 | 14 | public CreationResult Create(Type type, CreationHints creationHints = null) 15 | { 16 | return func(creationHints, type); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /OmniXaml.Tests/FuncStringConverter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace OmniXaml.Tests 4 | { 5 | internal class FuncStringConverter : IStringSourceValueConverter 6 | { 7 | private readonly Func func; 8 | 9 | public FuncStringConverter(Func func) 10 | { 11 | this.func = func; 12 | } 13 | 14 | public (bool, object) Convert(string strValue, Type desiredTargetType, ConvertContext context) 15 | { 16 | return func(strValue); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /OmniXaml.Tests/FuncStringConverterExtended.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace OmniXaml.Tests 4 | { 5 | public class FuncStringConverterExtended : IStringSourceValueConverter 6 | { 7 | private readonly Func func; 8 | 9 | public FuncStringConverterExtended(Func func) 10 | { 11 | this.func = func; 12 | } 13 | 14 | public (bool, object) Convert(string strValue, Type desiredTargetType, ConvertContext context) 15 | { 16 | return func(strValue, desiredTargetType); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /OmniXaml.Tests/InstanceCreatorMock.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace OmniXaml.Tests 4 | { 5 | internal class InstanceCreatorMock : IInstanceCreator 6 | { 7 | private Func createFunc = (type, hints) => new CreationResult(Activator.CreateInstance(type), new CreationHints()); 8 | 9 | public void SetObjectFactory(Func factory) 10 | { 11 | this.createFunc = factory; 12 | } 13 | 14 | public CreationResult Create(Type type, CreationHints creationHints) 15 | { 16 | return createFunc(type, creationHints); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /OmniXaml.Tests/MemberAssignmentTests.cs: -------------------------------------------------------------------------------- 1 | namespace OmniXaml.Tests 2 | { 3 | public class MemberAssignmentTests 4 | { 5 | //[Fact] 6 | //public void AttachedPropertyAssignment() 7 | //{ 8 | // ICollection groups = new List(); 9 | // var instance = new Window(); 10 | // var sut = new Assignment(new KeyedInstance(instance), Member.FromAttached("VisualStateGroups"), groups); 11 | // sut.ExecuteAssignment(); 12 | 13 | // Assert.IsAssignableFrom>(VisualStateManager.GetVisualStateGroups(instance)); 14 | //} 15 | 16 | //[Fact] 17 | //public void StandardPropertyAssignment() 18 | //{ 19 | // var instance = new TextBlock(); 20 | // var sut = new Assignment(new KeyedInstance(instance), Member.FromStandard(tb => tb.Text), "Some text"); 21 | // sut.ExecuteAssignment(); 22 | 23 | // Assert.Equal("Some text", instance.Text); 24 | //} 25 | } 26 | } -------------------------------------------------------------------------------- /OmniXaml.Tests/Model/AttachedEventArgs.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace OmniXaml.Tests.Model 4 | { 5 | public class AttachedEventArgs : EventArgs 6 | { 7 | public object Event { get; set; } 8 | } 9 | } -------------------------------------------------------------------------------- /OmniXaml.Tests/Model/Button.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace OmniXaml.Tests.Model 4 | { 5 | public class Button : ModelObject 6 | { 7 | public Button() 8 | { 9 | 10 | } 11 | 12 | public event EventHandler Click; 13 | 14 | public void ClickButton() 15 | { 16 | Click?.Invoke(this, EventArgs.Empty); 17 | } 18 | 19 | public object Content { get; set; } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /OmniXaml.Tests/Model/Collection.cs: -------------------------------------------------------------------------------- 1 | namespace OmniXaml.Tests.Model 2 | { 3 | using System.Collections.ObjectModel; 4 | using System.Linq; 5 | 6 | public class Collection : Collection 7 | { 8 | public string Title { get; set; } 9 | 10 | protected bool Equals(Collection other) 11 | { 12 | return string.Equals(Title, other.Title) && Enumerable.SequenceEqual(this, other); 13 | } 14 | 15 | public override bool Equals(object obj) 16 | { 17 | if (ReferenceEquals(null, obj)) 18 | return false; 19 | if (ReferenceEquals(this, obj)) 20 | return true; 21 | if (obj.GetType() != this.GetType()) 22 | return false; 23 | return Equals((Collection) obj); 24 | } 25 | 26 | public override int GetHashCode() 27 | { 28 | return (Title != null ? Title.GetHashCode() : 0); 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /OmniXaml.Tests/Model/CollectionExtension.cs: -------------------------------------------------------------------------------- 1 | namespace OmniXaml.Tests.Model 2 | { 3 | using System.Collections.ObjectModel; 4 | 5 | public class CollectionExtension : IMarkupExtension 6 | { 7 | public object GetValue(ExtensionValueContext context) 8 | { 9 | return new Collection {"Item 1", "Item 2"}; 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /OmniXaml.Tests/Model/ConstructionFragmentLoader.cs: -------------------------------------------------------------------------------- 1 | namespace OmniXaml.Tests.Model 2 | { 3 | using Metadata; 4 | 5 | public class ConstructionFragmentLoader : IConstructionFragmentLoader 6 | { 7 | public object Load(ConstructionNode node, INodeToObjectBuilder builder, BuilderContext context) 8 | { 9 | return new TemplateContent(node, builder); 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /OmniXaml.Tests/Model/Converters.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using OmniXaml.Attributes; 4 | using Zafiro.Core; 5 | 6 | namespace OmniXaml.Tests.Model 7 | { 8 | public static class Converters 9 | { 10 | [TypeConverterMember(typeof(ReferenceTarget))] 11 | public static Func Convert = (o, c) => 12 | { 13 | if (c != null) 14 | { 15 | var node = c.Node; 16 | var dicotomize = o.Dicotomize('.'); 17 | var name = dicotomize.Item1; 18 | var prop = dicotomize.Item2; 19 | var target = LookupName(name, node); 20 | 21 | if (target != null) 22 | { 23 | var instance = target.Instance; 24 | return (true, new ReferenceTarget() { Instance = instance, PropertyName = prop }); 25 | } 26 | } 27 | 28 | return (false, null); 29 | }; 30 | 31 | private static ConstructionNode LookupName(string name, ConstructionNode node) 32 | { 33 | var root = FindRoot(node); 34 | 35 | return LookupNameCore(name, root); 36 | } 37 | 38 | private static ConstructionNode LookupNameCore(string name, ConstructionNode root) 39 | { 40 | if (string.Equals(root.Name, name)) 41 | { 42 | return root; 43 | } 44 | 45 | var children = root.GetAllChildren(); 46 | 47 | return children.Select(node => LookupNameCore(name, node)).FirstOrDefault(childLookup => childLookup != null); 48 | } 49 | 50 | private static ConstructionNode FindRoot(ConstructionNode node) 51 | { 52 | if (node.Parent == null) 53 | { 54 | return node; 55 | } 56 | else 57 | { 58 | return FindRoot(node.Parent); 59 | } 60 | } 61 | } 62 | } -------------------------------------------------------------------------------- /OmniXaml.Tests/Model/Custom/CustomControl.cs: -------------------------------------------------------------------------------- 1 | namespace OmniXaml.Tests.Model.Custom 2 | { 3 | public class CustomControl 4 | { 5 | } 6 | } -------------------------------------------------------------------------------- /OmniXaml.Tests/Model/Custom/CustomGrid.cs: -------------------------------------------------------------------------------- 1 | namespace OmniXaml.Tests.Model.Custom 2 | { 3 | public class CustomGrid 4 | { 5 | public static void SetValue(object instance, object value) 6 | { 7 | } 8 | 9 | public static object GetValue(object instance) 10 | { 11 | return null; 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /OmniXaml.Tests/Model/Custom/CustomWindow.cs: -------------------------------------------------------------------------------- 1 | namespace OmniXaml.Tests.Model.Custom 2 | { 3 | internal class CustomWindow : Window 4 | { 5 | public string CustomProperty { get; set; } 6 | 7 | protected bool Equals(CustomWindow other) 8 | { 9 | return base.Equals(other) && string.Equals(CustomProperty, other.CustomProperty); 10 | } 11 | 12 | public override bool Equals(object obj) 13 | { 14 | if (ReferenceEquals(null, obj)) 15 | return false; 16 | if (ReferenceEquals(this, obj)) 17 | return true; 18 | if (obj.GetType() != this.GetType()) 19 | return false; 20 | return Equals((CustomWindow) obj); 21 | } 22 | 23 | public override int GetHashCode() 24 | { 25 | unchecked 26 | { 27 | return (base.GetHashCode()*397) ^ (CustomProperty != null ? CustomProperty.GetHashCode() : 0); 28 | } 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /OmniXaml.Tests/Model/Custom/ParametrizedExtension.cs: -------------------------------------------------------------------------------- 1 | namespace OmniXaml.Tests.Model.Custom 2 | { 3 | public class ParametrizedExtension : IMarkupExtension 4 | { 5 | public ParametrizedExtension(string str) 6 | { 7 | String = str; 8 | } 9 | 10 | public object GetValue(ExtensionValueContext context) 11 | { 12 | return String; 13 | } 14 | 15 | public string String { get; set; } 16 | 17 | protected bool Equals(ParametrizedExtension other) 18 | { 19 | return string.Equals(String, other.String); 20 | } 21 | 22 | public override bool Equals(object obj) 23 | { 24 | if (ReferenceEquals(null, obj)) 25 | return false; 26 | if (ReferenceEquals(this, obj)) 27 | return true; 28 | if (obj.GetType() != this.GetType()) 29 | return false; 30 | return Equals((ParametrizedExtension) obj); 31 | } 32 | 33 | public override int GetHashCode() 34 | { 35 | return (String != null ? String.GetHashCode() : 0); 36 | } 37 | } 38 | } -------------------------------------------------------------------------------- /OmniXaml.Tests/Model/DataTemplate.cs: -------------------------------------------------------------------------------- 1 | namespace OmniXaml.Tests.Model 2 | { 3 | using Attributes; 4 | 5 | [Namescope] 6 | public class DataTemplate 7 | { 8 | [Content] 9 | [FragmentLoader(FragmentLoader = typeof(ConstructionFragmentLoader))] 10 | public TemplateContent Content { get; set; } 11 | 12 | protected bool Equals(DataTemplate other) 13 | { 14 | return Equals(Content, other.Content); 15 | } 16 | 17 | public override bool Equals(object obj) 18 | { 19 | if (ReferenceEquals(null, obj)) 20 | return false; 21 | if (ReferenceEquals(this, obj)) 22 | return true; 23 | if (obj.GetType() != this.GetType()) 24 | return false; 25 | return Equals((DataTemplate) obj); 26 | } 27 | 28 | public override int GetHashCode() 29 | { 30 | return 0; 31 | } 32 | } 33 | } -------------------------------------------------------------------------------- /OmniXaml.Tests/Model/Grid.cs: -------------------------------------------------------------------------------- 1 | namespace OmniXaml.Tests.Model 2 | { 3 | public class Grid 4 | { 5 | public static void SetRow(object instance, int value) 6 | { 7 | } 8 | 9 | public static int GetRow(object instance) 10 | { 11 | return 1; 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /OmniXaml.Tests/Model/ItemsControl.cs: -------------------------------------------------------------------------------- 1 | namespace OmniXaml.Tests.Model 2 | { 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using Attributes; 6 | 7 | public class ItemsControl : ModelObject 8 | { 9 | public string HeaderText { get; set; } 10 | public DataTemplate ItemTemplate { get; set; } 11 | [Content] 12 | public ICollection Items { get; set; } = new List(); 13 | 14 | public List ItemsSource { get; set; } 15 | 16 | protected bool Equals(ItemsControl other) 17 | { 18 | return Items != null && (base.Equals(other) && string.Equals(HeaderText, other.HeaderText) && Equals(ItemTemplate, other.ItemTemplate) && Enumerable.SequenceEqual(Items, other.Items)); 19 | } 20 | 21 | public override bool Equals(object obj) 22 | { 23 | if (ReferenceEquals(null, obj)) return false; 24 | if (ReferenceEquals(this, obj)) return true; 25 | if (obj.GetType() != this.GetType()) return false; 26 | return Equals((ItemsControl) obj); 27 | } 28 | 29 | public override int GetHashCode() 30 | { 31 | unchecked 32 | { 33 | int hashCode = base.GetHashCode(); 34 | hashCode = (hashCode * 397) ^ (HeaderText != null ? HeaderText.GetHashCode() : 0); 35 | hashCode = (hashCode * 397) ^ (ItemTemplate != null ? ItemTemplate.GetHashCode() : 0); 36 | hashCode = (hashCode * 397) ^ (Items != null ? Items.GetHashCode() : 0); 37 | return hashCode; 38 | } 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /OmniXaml.Tests/Model/ModelObject.cs: -------------------------------------------------------------------------------- 1 | namespace OmniXaml.Tests.Model 2 | { 3 | using System; 4 | using System.Collections.Generic; 5 | using Attributes; 6 | 7 | public class ModelObject 8 | { 9 | [Name] 10 | public string Name { get; set; } 11 | 12 | public double Height { get; set; } 13 | public double Width { get; set; } 14 | 15 | private Dictionary> handlers = new Dictionary>(); 16 | 17 | public void AddHandler(object routedEvent, Action handler, bool handledEventsToo = false) 18 | { 19 | if(handlers.ContainsKey(routedEvent)) 20 | { 21 | handlers[routedEvent] += handler; 22 | } 23 | else 24 | { 25 | handlers.Add(routedEvent, handler); 26 | } 27 | } 28 | 29 | public void RaiseEvent(AttachedEventArgs args) 30 | { 31 | if (handlers.ContainsKey(args.Event)) 32 | { 33 | handlers[args.Event]?.Invoke(args); 34 | } 35 | } 36 | 37 | protected bool Equals(ModelObject other) 38 | { 39 | return string.Equals(Name, other.Name) && Height.Equals(other.Height) && Width.Equals(other.Width); 40 | } 41 | 42 | public override bool Equals(object obj) 43 | { 44 | if (ReferenceEquals(null, obj)) 45 | return false; 46 | if (ReferenceEquals(this, obj)) 47 | return true; 48 | if (obj.GetType() != this.GetType()) 49 | return false; 50 | return Equals((ModelObject) obj); 51 | } 52 | 53 | public override int GetHashCode() 54 | { 55 | unchecked 56 | { 57 | var hashCode = (Name != null ? Name.GetHashCode() : 0); 58 | hashCode = (hashCode*397) ^ Height.GetHashCode(); 59 | hashCode = (hashCode*397) ^ Width.GetHashCode(); 60 | return hashCode; 61 | } 62 | } 63 | } 64 | } -------------------------------------------------------------------------------- /OmniXaml.Tests/Model/MyImmutable.cs: -------------------------------------------------------------------------------- 1 | namespace OmniXaml.Tests.Model 2 | { 3 | public class MyImmutable : ModelObject 4 | { 5 | private readonly string argument; 6 | 7 | public MyImmutable(string argument) 8 | { 9 | this.Argument = argument; 10 | } 11 | 12 | public string Argument { get; } 13 | 14 | protected bool Equals(MyImmutable other) 15 | { 16 | return string.Equals(argument, other.argument); 17 | } 18 | 19 | public override bool Equals(object obj) 20 | { 21 | if (ReferenceEquals(null, obj)) 22 | return false; 23 | if (ReferenceEquals(this, obj)) 24 | return true; 25 | if (obj.GetType() != this.GetType()) 26 | return false; 27 | return Equals((MyImmutable) obj); 28 | } 29 | 30 | public override int GetHashCode() 31 | { 32 | return (argument != null ? argument.GetHashCode() : 0); 33 | } 34 | } 35 | } -------------------------------------------------------------------------------- /OmniXaml.Tests/Model/Reference.cs: -------------------------------------------------------------------------------- 1 | namespace OmniXaml.Tests.Model 2 | { 3 | public class Reference : IMarkupExtension 4 | { 5 | public object GetValue(ExtensionValueContext context) 6 | { 7 | return Target?.Value; 8 | } 9 | 10 | public ReferenceTarget Target { get; set; } 11 | } 12 | } -------------------------------------------------------------------------------- /OmniXaml.Tests/Model/ReferenceTarget.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | 3 | namespace OmniXaml.Tests.Model 4 | { 5 | public class ReferenceTarget 6 | { 7 | public object Instance { get; set; } 8 | public string PropertyName { get; set; } 9 | public object Value => Instance.GetType().GetRuntimeProperty(PropertyName).GetValue(Instance); 10 | } 11 | } -------------------------------------------------------------------------------- /OmniXaml.Tests/Model/ResourceDictionary.cs: -------------------------------------------------------------------------------- 1 | namespace OmniXaml.Tests.Model 2 | { 3 | using System.Collections.Generic; 4 | using Zafiro.Core; 5 | 6 | public class ResourceDictionary : Dictionary 7 | { 8 | public override bool Equals(object obj) 9 | { 10 | var rd = obj as ResourceDictionary; 11 | return this.ContentEquals(rd); 12 | } 13 | 14 | public override int GetHashCode() 15 | { 16 | return base.GetHashCode(); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /OmniXaml.Tests/Model/Setter.cs: -------------------------------------------------------------------------------- 1 | namespace OmniXaml.Tests.Model 2 | { 3 | using Attributes; 4 | 5 | public class Setter 6 | { 7 | private string value; 8 | 9 | 10 | public string Property { get; set; } 11 | 12 | [DependsOn(nameof(Property))] 13 | public string Value 14 | { 15 | get { return value; } 16 | set 17 | { 18 | this.value = value; 19 | if (Property != null) 20 | { 21 | RightOrder = true; 22 | } 23 | } 24 | } 25 | 26 | public bool RightOrder { get; set; } 27 | } 28 | } -------------------------------------------------------------------------------- /OmniXaml.Tests/Model/SimpleExtension.cs: -------------------------------------------------------------------------------- 1 | namespace OmniXaml.Tests.Model 2 | { 3 | public class SimpleExtension : IMarkupExtension 4 | { 5 | public object GetValue(ExtensionValueContext context) 6 | { 7 | return Property; 8 | } 9 | 10 | public string Property { get; set; } 11 | } 12 | } -------------------------------------------------------------------------------- /OmniXaml.Tests/Model/TemplateContent.cs: -------------------------------------------------------------------------------- 1 | namespace OmniXaml.Tests.Model 2 | { 3 | public class TemplateContent 4 | { 5 | private readonly ConstructionNode node; 6 | private readonly INodeToObjectBuilder builder; 7 | 8 | public TemplateContent(ConstructionNode node, INodeToObjectBuilder builder) 9 | { 10 | this.node = node; 11 | this.builder = builder; 12 | } 13 | 14 | public object Load() 15 | { 16 | return builder.Build(node); 17 | } 18 | 19 | protected bool Equals(TemplateContent other) 20 | { 21 | return Equals(node, other.node); 22 | } 23 | 24 | public override bool Equals(object obj) 25 | { 26 | if (ReferenceEquals(null, obj)) 27 | return false; 28 | if (ReferenceEquals(this, obj)) 29 | return true; 30 | if (obj.GetType() != this.GetType()) 31 | return false; 32 | return Equals((TemplateContent) obj); 33 | } 34 | 35 | public override int GetHashCode() 36 | { 37 | return (node != null ? node.GetHashCode() : 0); 38 | } 39 | } 40 | } -------------------------------------------------------------------------------- /OmniXaml.Tests/Model/TestWindow.cs: -------------------------------------------------------------------------------- 1 | namespace OmniXaml.Tests.Model 2 | { 3 | using System; 4 | 5 | internal class TestWindow : Window 6 | { 7 | internal bool ButtonClicked { get; private set; } = false; 8 | internal bool WindowLoaded { get; private set; } = false; 9 | 10 | internal void OnClick(object sender, EventArgs args) 11 | { 12 | ButtonClicked = true; 13 | } 14 | 15 | internal void OnLoad(EventArgs args) 16 | { 17 | WindowLoaded = true; 18 | } 19 | 20 | public event EventHandler Clicked; 21 | } 22 | } -------------------------------------------------------------------------------- /OmniXaml.Tests/Model/TextBlock.cs: -------------------------------------------------------------------------------- 1 | namespace OmniXaml.Tests.Model 2 | { 3 | using Attributes; 4 | 5 | public class TextBlock : ModelObject 6 | { 7 | [Content] 8 | public string Text { get; set; } 9 | 10 | public TextWrapping TextWrapping { get; set; } 11 | 12 | 13 | protected bool Equals(TextBlock other) 14 | { 15 | return base.Equals(other) && string.Equals(Text, other.Text) && TextWrapping == other.TextWrapping; 16 | } 17 | 18 | public override bool Equals(object obj) 19 | { 20 | if (ReferenceEquals(null, obj)) 21 | return false; 22 | if (ReferenceEquals(this, obj)) 23 | return true; 24 | if (obj.GetType() != this.GetType()) 25 | return false; 26 | return Equals((TextBlock)obj); 27 | } 28 | 29 | public override int GetHashCode() 30 | { 31 | unchecked 32 | { 33 | int hashCode = base.GetHashCode(); 34 | hashCode = (hashCode * 397) ^ (Text != null ? Text.GetHashCode() : 0); 35 | hashCode = (hashCode * 397) ^ (int)TextWrapping; 36 | return hashCode; 37 | } 38 | } 39 | } 40 | } -------------------------------------------------------------------------------- /OmniXaml.Tests/Model/TextWrapping.cs: -------------------------------------------------------------------------------- 1 | namespace OmniXaml.Tests.Model 2 | { 3 | public enum TextWrapping 4 | { 5 | Wrap, 6 | NoWrap, 7 | } 8 | } -------------------------------------------------------------------------------- /OmniXaml.Tests/Model/VisualStateGroup.cs: -------------------------------------------------------------------------------- 1 | namespace OmniXaml.Tests.Model 2 | { 3 | public class VisualStateGroup 4 | { 5 | } 6 | } -------------------------------------------------------------------------------- /OmniXaml.Tests/Model/VisualStateManager.cs: -------------------------------------------------------------------------------- 1 | namespace OmniXaml.Tests.Model 2 | { 3 | using System.Collections.Generic; 4 | using Zafiro.Core; 5 | 6 | public class VisualStateManager 7 | { 8 | private static readonly IDictionary> GroupsByInstance = new Dictionary>(); 9 | 10 | public static void SetVisualStateGroups(object instance, ICollection groups) 11 | { 12 | GroupsByInstance.Add(instance, groups); 13 | } 14 | public static ICollection GetVisualStateGroups(object instance) 15 | { 16 | return GroupsByInstance.GetCreate(instance, () => new List()); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /OmniXaml.Tests/Model/Window.cs: -------------------------------------------------------------------------------- 1 | namespace OmniXaml.Tests.Model 2 | { 3 | using Attributes; 4 | 5 | [Namescope] 6 | public class Window : ModelObject 7 | { 8 | public static readonly object LoadedEvent = new object(); 9 | 10 | public string Title { get; set; } 11 | [Content] 12 | public object Content { get; set; } 13 | 14 | public ResourceDictionary Resources { get; set; } = new ResourceDictionary(); 15 | 16 | public override string ToString() 17 | { 18 | return $"{nameof(Title)}: {Title}, {nameof(Content)}: {Content}"; 19 | } 20 | 21 | protected bool Equals(Window other) 22 | { 23 | return base.Equals(other) && string.Equals(Title, other.Title) && Equals(Content, other.Content) && Equals(Resources, other.Resources); 24 | } 25 | 26 | public override bool Equals(object obj) 27 | { 28 | if (ReferenceEquals(null, obj)) 29 | return false; 30 | if (ReferenceEquals(this, obj)) 31 | return true; 32 | if (obj.GetType() != this.GetType()) 33 | return false; 34 | return Equals((Window) obj); 35 | } 36 | 37 | public override int GetHashCode() 38 | { 39 | unchecked 40 | { 41 | return ((Title != null ? Title.GetHashCode() : 0)*397) ^ (Content != null ? Content.GetHashCode() : 0); 42 | } 43 | } 44 | } 45 | } -------------------------------------------------------------------------------- /OmniXaml.Tests/OmniXaml.Tests.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netcoreapp1.1 5 | OmniXaml.Tests 6 | OmniXaml.Tests 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /OmniXaml.Tests/Phase2BuilderTests.cs: -------------------------------------------------------------------------------- 1 | using Xunit; 2 | 3 | namespace OmniXaml.Tests 4 | { 5 | public class Phase2BuilderTests 6 | { 7 | [Fact] 8 | public void SingleInstance() 9 | { 10 | 11 | } 12 | } 13 | } -------------------------------------------------------------------------------- /OmniXaml.Tests/PipelineMock.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace OmniXaml.Tests 4 | { 5 | internal class PipelineMock : IValuePipeline 6 | { 7 | public void SetMutator(Action func) 8 | { 9 | this.mutator = func; 10 | } 11 | 12 | private Action mutator = (o, member, arg3) => { }; 13 | 14 | public void Handle(object parent, Member member, MutablePipelineUnit mutable, INodeToObjectBuilder builder, BuilderContext context) 15 | { 16 | mutator(parent, member, mutable); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /OmniXaml.Tests/SimpleValueConverter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.ComponentModel; 3 | 4 | namespace OmniXaml.Tests 5 | { 6 | internal class SimpleValueConverter : IStringSourceValueConverter 7 | { 8 | public (bool, object) Convert(string strValue, Type desiredTargetType, ConvertContext context) 9 | { 10 | var convertFrom = TypeDescriptor.GetConverter(desiredTargetType).ConvertFrom(strValue); 11 | return (true, convertFrom); 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /OmniXaml.Tests/StringValueConverterTests.cs: -------------------------------------------------------------------------------- 1 | using OmniXaml.Tests.Model; 2 | using Xunit; 3 | 4 | namespace OmniXaml.Tests 5 | { 6 | public class StringValueConverterTests 7 | { 8 | [Theory] 9 | [InlineData("hola", "hola")] 10 | [InlineData("1", 1)] 11 | [InlineData("1.4", 1.4)] 12 | [InlineData("1.6", 1.6F)] 13 | [InlineData("Wrap", TextWrapping.Wrap)] 14 | public void Convert(string sourceValue, object converted) 15 | { 16 | var sut = CreateSut(); 17 | Assert.Equal((true, converted), sut.Convert(sourceValue, converted.GetType())); 18 | } 19 | 20 | private static StringValueConverter CreateSut() 21 | { 22 | return new StringValueConverter(); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /OmniXaml.Tests/TypeDirectoryTests.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using OmniXaml.Tests.Model; 3 | using OmniXaml.TypeLocation; 4 | using Xunit; 5 | 6 | namespace OmniXaml.Tests 7 | { 8 | public class TypeDirectoryTests 9 | { 10 | [Fact] 11 | public void GetTypeFromXamlNamespace() 12 | { 13 | var sut = CreateSut(); 14 | var type = sut.GetTypeByFullAddress(new Address("root", "TextBlock")); 15 | Assert.NotNull(type); 16 | } 17 | 18 | [Fact] 19 | public void GetTypeFromClrNamespace() 20 | { 21 | var sut = CreateSut(); 22 | var type = sut.GetTypeByFullAddress(new Address("using:OmniXaml.Tests.Model;assembly=OmniXaml.Tests", "TextBlock")); 23 | Assert.NotNull(type); 24 | } 25 | 26 | private ITypeDirectory CreateSut() 27 | { 28 | var type = typeof(TextBlock); 29 | var assembly = type.GetTypeInfo().Assembly; 30 | var xamlNamespaces = XamlNamespace.Map("root").With(Route.Assembly(assembly).WithNamespaces(type.Namespace)); 31 | return new TypeDirectory(new[] { xamlNamespaces }); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /OmniXaml.Tests/XmlToNodes/CData.cs: -------------------------------------------------------------------------------- 1 | using OmniXaml.Tests.Model; 2 | using Xunit; 3 | 4 | namespace OmniXaml.Tests.XmlToNodes 5 | { 6 | public class CData : XamlToTreeParserTestsBase 7 | { 8 | [Fact] 9 | public void CDataInsidePropertyElement() 10 | { 11 | var parseResult = ParseResult(@""); 12 | 13 | var expected = new ConstructionNode().WithAssignment(w => w.Content, "Hello"); 14 | 15 | Assert.Equal(expected, parseResult.Root); 16 | } 17 | 18 | [Fact] 19 | public void CDataAsContentProperty() 20 | { 21 | var parseResult = ParseResult(@""); 22 | 23 | var expected = new ConstructionNode().WithAssignment(w => w.Content, "Hello"); 24 | 25 | Assert.Equal(expected, parseResult.Root); 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /OmniXaml.Tests/XmlToNodes/ClassDirective.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using OmniXaml.Tests.Model; 3 | using OmniXaml.Tests.Model.Custom; 4 | using Xunit; 5 | 6 | namespace OmniXaml.Tests.XmlToNodes 7 | { 8 | public class ClassDirective : XamlToTreeParserTestsBase 9 | { 10 | [Fact] 11 | public void Class() 12 | { 13 | var xaml = @""; 14 | var p = ParseResult(xaml); 15 | ConstructionNode expected = new ConstructionNode { InstantiateAs = typeof(CustomWindow) }; 16 | 17 | Assert.Equal(expected, p.Root); 18 | } 19 | 20 | [Fact] 21 | public void EventAttachedToCorrectInstance() 22 | { 23 | var xaml = @""; 24 | var p = ParseResult(xaml); 25 | 26 | var expected = new ConstructionNode() 27 | .WithAssignment("Clicked", "OnClick"); 28 | 29 | Assert.Equal(expected, p.Root); 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /OmniXaml.Tests/XmlToNodes/CollectionsTests.cs: -------------------------------------------------------------------------------- 1 | using OmniXaml.Tests.Model; 2 | using Xunit; 3 | 4 | namespace OmniXaml.Tests.XmlToNodes 5 | { 6 | public class CollectionsTests : XamlToTreeParserTestsBase 7 | { 8 | [Fact] 9 | public void EmptyCollection() 10 | { 11 | var tree = ParseResult(@""); 12 | Assert.Equal(new ConstructionNode(), tree.Root); 13 | } 14 | 15 | [Fact] 16 | public void Collection() 17 | { 18 | var tree = ParseResult(@""); 19 | var memberAssignments = new[] 20 | { 21 | new MemberAssignment 22 | { 23 | Member = Member.FromStandard(collection => collection.Title), 24 | SourceValue = "My title" 25 | } 26 | }; 27 | var children = new[] 28 | { 29 | new ConstructionNode() 30 | }; 31 | var expected = new ConstructionNode() 32 | .WithAssignments(memberAssignments) 33 | .WithChildren(children); 34 | 35 | Assert.Equal(expected, tree.Root); 36 | } 37 | 38 | [Fact] 39 | public void PropertyThatIsCollection() 40 | { 41 | var tree = ParseResult(@""); 42 | var memberAssignments = new[] 43 | { 44 | new MemberAssignment 45 | { 46 | Member = Member.FromStandard(collection => collection.Items), 47 | Values = new[] 48 | { 49 | new ConstructionNode(), 50 | new ConstructionNode(), 51 | new ConstructionNode(), 52 | } 53 | } 54 | }; 55 | var expected = new ConstructionNode().WithAssignments(memberAssignments); 56 | 57 | Assert.Equal(expected, tree.Root); 58 | } 59 | } 60 | } -------------------------------------------------------------------------------- /OmniXaml.Tests/XmlToNodes/DictionaryTests.cs: -------------------------------------------------------------------------------- 1 | using OmniXaml.Tests.Model; 2 | using Xunit; 3 | 4 | namespace OmniXaml.Tests.XmlToNodes 5 | { 6 | public class DictionaryTests : XamlToTreeParserTestsBase 7 | { 8 | [Fact] 9 | public void Key() 10 | { 11 | var children = new[] {new ConstructionNode {Key = "MyKey"},}; 12 | var expected = new ConstructionNode() 13 | .WithChildren(children); 14 | 15 | var actual = ParseResult(@""); 16 | 17 | Assert.Equal(expected, actual.Root); 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /OmniXaml.Tests/XmlToNodes/EventsTests.cs: -------------------------------------------------------------------------------- 1 | using OmniXaml.Tests.Model; 2 | using Xunit; 3 | 4 | namespace OmniXaml.Tests.XmlToNodes 5 | { 6 | public class EventsTests : XamlToTreeParserTestsBase 7 | { 8 | [Fact] 9 | public void BasicEvent() 10 | { 11 | var tree = ParseResult(@" 12 |