public static string Convert(
            string xaml,
            string sourceFile,
            string fileNameWithPathRelativeToProjectRoot,
            string assemblyNameWithoutExtension,
            ReflectionOnSeparateAppDomainHandler reflectionOnSeparateAppDomain,
            bool isFirstPass,
            bool isSLMigration,
            string outputRootPath,
            string outputAppFilesPath,
            string outputLibrariesPath,
            string outputResourcesPath,
            ILogger logger)
        {
            // Process the "HtmlPresenter" nodes in order to "escape" its content, because the content is HTML and it could be badly formatted and not be parsable using XDocument.Parse.
            xaml = ProcessingHtmlPresenterNodes.Process(xaml);

            // Parse the XML:
            XDocument doc = XDocument.Parse(xaml, LoadOptions.SetLineInfo);

            // Process the "TextBlock" and "Span" nodes in order to surround direct text content with "<Run>" tags:
            ProcessingTextBlockNodes.Process(doc, reflectionOnSeparateAppDomain);

            // Insert implicit nodes in XAML:
            if (!isFirstPass) // Note: we skip this step during the 1st pass because some types are not known yet, so we cannot determine the default "ContentProperty".
            {
                InsertingImplicitNodes.InsertImplicitNodes(doc, reflectionOnSeparateAppDomain);

                FixingPropertiesOrder.FixPropertiesOrder(doc, reflectionOnSeparateAppDomain);

                // Process the "ContentPresenter" nodes in order to transform "<ContentPresenter />" into "<ContentPresenter Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" />"
                ProcessingContentPresenterNodes.Process(doc, reflectionOnSeparateAppDomain);

                // Process the "ColorAnimation" nodes in order to transform "Storyboard.TargetProperty="XXX"" into "Storyboard.TargetProperty="XXX.Color"" if XXX doesn't end on ".Color" or ".Color)"
                ProcessingColorAnimationNodes.Process(doc, reflectionOnSeparateAppDomain);

                // Convert markup extensions into XDocument nodes:
                InsertingMarkupNodesInXaml.InsertMarkupNodes(doc, reflectionOnSeparateAppDomain);

                // Fix names of visual states (for instance "PointerOver" in UWP becomes "MouseOver" in Silverlight and WPF).
                FixingVisualStatesName.Fix(doc, isSLMigration);
            }

            // Generate unique names for XAML elements:
            GeneratingUniqueNames.ProcessDocument(doc);

            // Prepare the code that will be put in the "InitializeComponent" of the Application class, which means that it will be executed when the application is launched:
            string codeToPutInTheInitializeComponentOfTheApplicationClass = string.Format(@"
global::CSHTML5.Internal.StartupAssemblyInfo.OutputRootPath = @""{0}"";
global::CSHTML5.Internal.StartupAssemblyInfo.OutputAppFilesPath = @""{1}"";
global::CSHTML5.Internal.StartupAssemblyInfo.OutputLibrariesPath = @""{2}"";
global::CSHTML5.Internal.StartupAssemblyInfo.OutputResourcesPath = @""{3}"";
", outputRootPath, outputAppFilesPath, outputLibrariesPath, outputResourcesPath);

            // Generate C# code from the tree:
            return(GeneratingCSharpCode.GenerateCSharpCode(doc, sourceFile, fileNameWithPathRelativeToProjectRoot, assemblyNameWithoutExtension, reflectionOnSeparateAppDomain, isFirstPass, isSLMigration, codeToPutInTheInitializeComponentOfTheApplicationClass, logger));
        }
Exemple #2
0
        //internal static bool IsMarkupExtension(XAttribute attribute, out bool hasXamlError)
        //{
        //    if (attribute != null)
        //    {
        //        string attributeValue = attribute.Value;
        //        if (attributeValue.StartsWith("{"))
        //        {
        //            int indexOfNextClosingBracket = attributeValue.IndexOf("}");
        //            string contentBetweenBrackets = attributeValue.Substring(1, indexOfNextClosingBracket - 1);
        //            bool isMarkupExtension = (!string.IsNullOrWhiteSpace(contentBetweenBrackets));
        //            isMarkupExtension = (!string.IsNullOrWhiteSpace(contentBetweenBrackets));
        //            if (isMarkupExtension)
        //            {
        //                //We check whether the first character is a Number because of StringFormat (example: "{Binding ... StringFormat={0:N4}}" the "{0:N4}" part is not a MarkupExtension).
        //                string tempCurrentAttribute = contentBetweenBrackets.Trim();
        //                char c = tempCurrentAttribute[0];
        //                isMarkupExtension = !(c >= '0' && c <= '9');
        //                hasXamlError = !isMarkupExtension;
        //                return isMarkupExtension;
        //            }
        //            else
        //            {
        //                hasXamlError = false;
        //                return false;
        //            }
        //        }
        //    }
        //    hasXamlError = false;
        //    return false;
        //}

        internal static bool IsMarkupExtension(XAttribute attribute)
        {
            if (attribute != null)
            {
                string value = attribute.Value;
                if (value.StartsWith("{"))
                {
                    int indexOfClosingBracket = value.IndexOf('}');
                    if (indexOfClosingBracket < 0)
                    {
                        throw new wpf::System.Windows.Markup.XamlParseException(string.Format("Invalid value for attribute {0}. Use \"{{}}\" to escape '{{'.", attribute.Name), GeneratingCSharpCode.GetLineNumber(attribute.Parent), -1);
                    }
                    string contentBetweenBrackets = value.Substring(1, indexOfClosingBracket - 1);
                    if (string.IsNullOrEmpty(contentBetweenBrackets)) //handle special case where '{' is escaped with "{}"
                    {
                        return(false);
                    }
                    else
                    {
                        string trimmedValue = value.Trim();
                        if (trimmedValue != string.Empty)
                        {
                            char c = trimmedValue[0];
                            return(!(c >= '0' && c <= '9')); //We check whether the first character is a Number because of StringFormat (example: "{Binding ... StringFormat={0:N4}}" the "{0:N4}" part is not a MarkupExtension).
                        }
                        else
                        {
                            throw new wpf::System.Windows.Markup.XamlParseException(string.Format("Invalid value for attribute {0}. Use {} to escape '{'.", attribute.Name), GeneratingCSharpCode.GetLineNumber(attribute.Parent), -1);
                        }
                    }
                }
            }
            return(false);
        }