private Dictionary <string, CodePropertyReferenceExpression> CreatePropertiesForResources(IEnumerable <Resource> resources) { return(resources.ToDictionary(resource => resource.Key, resource => { DataTemplateResource dataTemplateResource = resource as DataTemplateResource; Tuple <CodeMemberField, CodeMemberProperty> result; if (dataTemplateResource != null) // in case of data templates { const string type = "Storm.Mvvm.DataTemplate"; result = CodeGeneratorHelper.GenerateProxyProperty(resource.PropertyName, type, fieldReference => new List <CodeStatement> { // _field = new DataTemplate(); new CodeAssignStatement(fieldReference, new CodeObjectCreateExpression(CodeGeneratorHelper.GetTypeReferenceFromName(type))), // _field.ViewId = Resource.Id.*** new CodeAssignStatement(new CodePropertyReferenceExpression(fieldReference, "ViewId"), CodeGeneratorHelper.GetAndroidResourceReference(ResourcePart.Layout, dataTemplateResource.ViewId)), // _field.LayoutInflater = LayoutInflater; new CodeAssignStatement(new CodePropertyReferenceExpression(fieldReference, "LayoutInflater"), GetLayoutInflaterReference()), // _field.ViewHolderType = typeof(viewholder class) new CodeAssignStatement(new CodePropertyReferenceExpression(fieldReference, "ViewHolderType"), new CodeTypeOfExpression(string.Format("{0}.{1}", Configuration.GeneratedNamespace, dataTemplateResource.ViewHolderClassName))), }); } else { // create a proxy property to handle the resource string type = resource.Type; Dictionary <string, string> assignments = resource.Properties; result = CodeGeneratorHelper.GenerateProxyProperty(resource.PropertyName, type, fieldReference => CodeGeneratorHelper.GenerateStatementsCreateAndAssign(fieldReference, type, assignments)); } Fields.Add(result.Item1); Properties.Add(result.Item2); return CodeGeneratorHelper.GetPropertyReference(result.Item2); })); }
/// <summary> /// 注册代码 /// </summary> void IAutoRegister.AutoRegist() { var name = GetType().Assembly.FullName.Split(',')[0]; //var file = Path.GetFileNameWithoutExtension(); var uri = new Uri($"/{name};component/DataTemplate/ConfigDataTemplate.xaml", UriKind.Relative); StreamResourceInfo info = Application.GetResourceStream(uri); // ReSharper disable PossibleNullReferenceException if (info != null) { var asm = XamlReader.Load(new Baml2006Reader(info.Stream)) as ResourceDictionary; DataTemplateResource.RegistResource(asm); } GlobalTrigger.RegistTrigger <ConfigTrigger>(); GlobalTrigger.RegistTrigger <ParentConfigTrigger>(); GlobalTrigger.RegistTrigger <PropertyTrigger>(); GlobalTrigger.RegistTrigger <EntityTrigger>(); GlobalTrigger.RegistTrigger <ProjectTrigger>(); GlobalTrigger.RegistTrigger <SolutionTrigger>(); CommandIoc.NewConfigCommand = NewConfigCommand; }
public void Process(DataTemplateResource dataTemplate, List <Resource> resources, List <StyleResource> styleResources, List <DataTemplateResource> dataTemplatesResources, ConfigurationFile configurationFile) { string viewOutputFile = Path.Combine(configurationFile.ResourceLocation, string.Format("{0}.axml", dataTemplate.ViewId)); string viewOutputRelativePath = PathHelper.GetRelativePath(viewOutputFile); Log.LogMessage(MessageImportance.High, "\t\t Preprocessing DataTemplate {0}", dataTemplate.Key); // Extract informations from xml Tuple <List <XmlAttribute>, List <IdViewObject> > expressionResult = _viewFileProcessor.ExtractExpressions(dataTemplate.ResourceElement); List <XmlAttribute> expressionAttributes = expressionResult.Item1; List <IdViewObject> viewObjects = expressionResult.Item2; List <Resource> localResources = _viewFileProcessor.ExtractResources(dataTemplate.ResourceElement); // filter resources to find any dataTemplate in it List <DataTemplateResource> localDataTemplatesResources = localResources.Where(x => ParsingHelper.IsDataTemplateTag(x.ResourceElement)).Select(x => new DataTemplateResource(x)).ToList(); localResources.RemoveAll(x => ParsingHelper.IsDataTemplateTag(x.ResourceElement)); //filter resources for Style List <StyleResource> localStyleResources = resources.Where(x => ParsingHelper.IsStyleTag(x.ResourceElement)).Select(x => new StyleResource(x)).ToList(); localStyleResources.AddRange(styleResources); resources.RemoveAll(x => ParsingHelper.IsStyleTag(x.ResourceElement)); // now write a file for this data template Log.LogMessage(MessageImportance.High, "\t\t\t Generating view file for DataTemplate to {0}", viewOutputRelativePath); _viewFileWriter.Write(dataTemplate.ResourceElement, viewOutputFile, localStyleResources); ResourceFiles.Add(viewOutputRelativePath); // assign an id to all data template before processing it string viewName = dataTemplate.ViewId; foreach (DataTemplateResource localDataTemplate in localDataTemplatesResources) { localDataTemplate.ViewId = string.Format("{0}_DT_{1}", viewName, localDataTemplate.Key); localDataTemplate.ViewHolderClassName = NameGeneratorHelper.GetViewHolderName(); } // process each one List <Resource> mergedResources = new List <Resource>(resources); mergedResources.AddRange(localResources); List <DataTemplateResource> mergedDataTemplatesResources = new List <DataTemplateResource>(dataTemplatesResources); mergedDataTemplatesResources.AddRange(localDataTemplatesResources); foreach (DataTemplateResource localDataTemplate in localDataTemplatesResources) { Process(localDataTemplate, mergedResources, localStyleResources, mergedDataTemplatesResources, configurationFile); } string classOutputFile = Path.Combine(configurationFile.ClassLocation, string.Format("{0}.ui.cs", dataTemplate.ViewHolderClassName)); string classOutputRelativePath = PathHelper.GetRelativePath(classOutputFile); Log.LogMessage(MessageImportance.High, "\t\t\t Generating class file for DataTemplate to {0}", classOutputRelativePath); List <Resource> totalResources = new List <Resource>(mergedResources); totalResources.AddRange(mergedDataTemplatesResources); ViewHolderGenerator generator = new ViewHolderGenerator() { BaseClassType = "Storm.Mvvm.BaseViewHolder", ClassName = dataTemplate.ViewHolderClassName, Configuration = configurationFile, IsPartialClass = false, NamespaceName = configurationFile.GeneratedNamespace, }; generator.Preprocess(expressionAttributes, totalResources, viewObjects); generator.Generate(classOutputFile); ClassFiles.Add(classOutputRelativePath); }