/// <summary>
 /// Gets the name of C# the xaml file.
 /// </summary>
 public static string GetSamplesXamlFileName(this SampleModel model, Language language = Language.CSharp)
 {
     return(string.Format("{0}.xaml", model.GetSampleName(language)));
 }
        /// <summary>
        /// Creates a new control from sample.
        /// </summary>
        /// <param name="sampleModel">Sample that is transformed into a control</param>
        /// <returns>Sample as a control.</returns>
        public Control SampleToControl(SampleModel sampleModel)
        {
            var fullTypeAsString = string.Format("{0}.{1}", sampleModel.SampleNamespace,
                sampleModel.GetSampleName(_selectedLanguage));
            var sampleType = _samplesAssembly.GetType(fullTypeAsString);

            var item = sampleType.GetConstructor(new Type[] { }).Invoke(new object[] { });

           return (Control)item;
        }
Пример #3
0
        /// <summary>
        /// Creates new instance of <see cref="SampleStructureMap"/> by desirialing it from the json file provided.
        /// Returned instance will be fully loaded including other information that is not provided
        /// in the json file like samples.
        /// </summary>
        /// <param name="metadataFilePath">Full path to the metadata JSON file</param>
        /// <param name="language">Language that is used to create the samples</param>
        /// <returns>Deserialized <see cref="SampleStructureMap"/></returns>
        internal static SampleStructureMap Create(string metadataFilePath, Language language = Language.CSharp)
        {
            var serializer = new DataContractJsonSerializer(typeof(SampleStructureMap));

            SampleStructureMap structureMap = null;

            // Create new instance of SampleStuctureMap
            var metadataFile = new FileInfo(metadataFilePath);
            var json         = File.ReadAllText(metadataFilePath);

            var jsonInBytes = Encoding.UTF8.GetBytes(json);

            using (MemoryStream stream = new MemoryStream(jsonInBytes))
            {
                // De-serialize sample model
                structureMap         = serializer.ReadObject(stream) as SampleStructureMap;
                structureMap.Samples = new List <SampleModel>();
            }

            // Create all samples and add them to the groups since they are not part of
            // main configuration file
            var rootDirectory = metadataFile.Directory;

            var samplesDirectory   = new DirectoryInfo(Path.Combine(rootDirectory.FullName, "Samples"));
            var tutorialsDirectory = new DirectoryInfo(Path.Combine(rootDirectory.FullName, "Tutorials"));
            var workflowDirectory  = new DirectoryInfo(Path.Combine(rootDirectory.FullName, "Workflows"));

            var sampleGroupFolders = samplesDirectory.GetDirectories();

            // Create all samples
            foreach (var sampleGroupFolder in sampleGroupFolders) // ie. Samples\Layers
            {
                // This creates samples from all folders and adds them to the samples list
                // This means that sample is created even if it's not defined in the groups list
                var sampleFolders = sampleGroupFolder.GetDirectories();
                foreach (var sampleFolder in sampleFolders)  // ie. Samples\Layers\ArcgISTiledLayerFromUrl
                {
                    var sampleModel = SampleModel.Create(
                        Path.Combine(sampleFolder.FullName, "metadata.json"));

                    if (sampleModel != null)
                    {
                        structureMap.Samples.Add(sampleModel);
                    }
                }
            }

            // Create all tutorials
            if (tutorialsDirectory.Exists)
            {
                foreach (var sampleFolder in tutorialsDirectory.GetDirectories()) // ie. Tutorials\AddMapToApp
                {
                    var sampleModel = SampleModel.Create(
                        Path.Combine(sampleFolder.FullName, "metadata.json"));

                    if (sampleModel != null)
                    {
                        structureMap.Samples.Add(sampleModel);
                    }
                }
            }

            // Create all workflows
            if (workflowDirectory.Exists)
            {
                foreach (var sampleFolder in workflowDirectory.GetDirectories()) // ie. Workflows\SearchFeatures
                {
                    var sampleModel = SampleModel.Create(
                        Path.Combine(sampleFolder.FullName, "metadata.json"));

                    if (sampleModel != null)
                    {
                        structureMap.Samples.Add(sampleModel);
                    }
                }
            }

            // Set samples to the sub-categories
            var addedSamples = new List <SampleModel>();

            foreach (var cateory in structureMap.Categories)
            {
                foreach (var subCategory in cateory.SubCategories)
                {
                    if (subCategory.Samples == null)
                    {
                        subCategory.Samples = new List <SampleModel>();
                    }

                    if (subCategory.SampleNames == null)
                    {
                        subCategory.SampleNames = new List <string>();
                    }

                    foreach (var sampleName in subCategory.SampleNames)
                    {
                        var sample = structureMap.Samples.FirstOrDefault(x => x.SampleName == sampleName);

                        if (sample == null)
                        {
                            continue;
                        }

                        subCategory.Samples.Add(sample);
                        addedSamples.Add(sample);
                    }
                }
            }

            // Add samples that are not defined to the end of the groups
            var notAddedSamples = structureMap.Samples.Where(x => !addedSamples.Contains(x)).ToList();

            foreach (var sampleModel in notAddedSamples)
            {
                var category = structureMap.Categories.FirstOrDefault(x => x.CategoryName == sampleModel.Category);
                if (category == null)
                {
                    continue;
                }

                var subCategory = category.SubCategories.FirstOrDefault(x => x.SubCategoryName == sampleModel.SubCategory);
                if (subCategory != null)
                {
                    subCategory.SampleNames.Add(sampleModel.SampleName);
                    subCategory.Samples.Add(sampleModel);
                }
            }

            if (structureMap.Featured == null)
            {
                structureMap.Featured = new List <FeaturedModel>();
            }

            // Set all sample models to the featured models
            foreach (var featured in structureMap.Featured)
            {
                var sample = structureMap.Samples.FirstOrDefault(x => x.SampleName == featured.SampleName);
                if (sample != null)
                {
                    featured.Sample = sample;
                }
            }

            return(structureMap);
        }
Пример #4
0
        /// <summary>
        /// Creates new instance of <see cref="SampleStructureMap"/> by deserializing it from the json file provided.
        /// Returned instance will be fully loaded including other information that is not provided
        /// in the json file like samples.
        /// </summary>
        /// <param name="metadataFilePath">Full path to the metadata JSON file</param>
        /// <param name="language">Language that is used to create the samples</param>
        /// <returns>Deserialized <see cref="SampleStructureMap"/></returns>
        internal static SampleStructureMap Create(string metadataFilePath, Language language = Language.CSharp)
        {
            var serializer = new DataContractJsonSerializer(typeof(SampleStructureMap));

            SampleStructureMap structureMap = null;

            var groupsMetadataFileInfo = new FileInfo(metadataFilePath);

            if (!groupsMetadataFileInfo.Exists || groupsMetadataFileInfo == null)
            {
                throw new FileNotFoundException("Groups.json file not found from given location.");
            }

            // Create new instance of SampleStuctureMap
            var json = File.ReadAllText(metadataFilePath);

            var jsonInBytes = Encoding.UTF8.GetBytes(json);

            using (var stream = new MemoryStream(jsonInBytes))
            {
                // De-serialize sample model
                structureMap = serializer.ReadObject(stream) as SampleStructureMap;
                if (structureMap == null)
                {
                    throw new SerializationException("Couldn't create StructureMap from provided groups.json file stream.");
                }
                structureMap.Samples = new List <SampleModel>();
            }

            var pathList = new List <string>();

            foreach (var category in structureMap.Categories)
            {
                foreach (var subCategory in category.SubCategories)
                {
                    if (subCategory.SampleInfos != null)
                    {
                        foreach (var sample in subCategory.SampleInfos)
                        {
                            pathList.Add(sample.Path.Replace("/", "\\"));
                        }
                    }
                }
            }

            foreach (var samplePath in pathList)
            {
                var sampleMetadataFilePath = Path.Combine(
                    groupsMetadataFileInfo.Directory.FullName, samplePath, "metadata.json");
                var sampleModel = SampleModel.Create(sampleMetadataFilePath);
                if (sampleModel != null)
                {
                    structureMap.Samples.Add(sampleModel);
                }
            }

            foreach (var category in structureMap.Categories)
            {
                foreach (var subCategory in category.SubCategories)
                {
                    if (subCategory.Samples == null)
                    {
                        subCategory.Samples = new List <SampleModel>();
                    }

                    foreach (var sampleInfo in subCategory.SampleInfos)
                    {
                        var sample = structureMap.Samples.FirstOrDefault(x => x.SampleName == sampleInfo.SampleName);

                        if (sample == null)
                        {
                            continue;
                        }

                        subCategory.Samples.Add(sample);
                    }
                }
            }

            if (structureMap.Featured == null)
            {
                structureMap.Featured = new List <FeaturedModel>();
            }

            // Set all sample models to the featured models
            foreach (var featured in structureMap.Featured)
            {
                var sample = structureMap.Samples.FirstOrDefault(x => x.SampleName == featured.SampleName);
                if (sample != null)
                {
                    featured.Sample = sample;
                }
            }

            return(structureMap);
        }
 /// <summary>
 /// Check if the sample has a type registered.
 /// </summary>
 /// <param name="sampleModel">SampleModel that is checked.</param>
 /// <returns>Returns true if the type if found. False otherwice.</returns>
 private bool DoesSampleTypeExists(SampleModel sampleModel)
 {
     var fullTypeAsString = string.Format("{0}.{1}", sampleModel.SampleNamespace,
        sampleModel.GetSampleName(_selectedLanguage));
     var sampleType = _samplesAssembly.GetType(fullTypeAsString);
     if (sampleType == null)
         return false;
     return true;
 }
 /// <summary>
 /// Creates a new control from sample.
 /// </summary>
 /// <param name="sampleModel">Sample that is transformed into a control</param>
 /// <returns>Sample as a control.</returns>
 public Control SampleToControl(SampleModel sampleModel)
 {
     var fullTypeAsString = string.Format("{0}.{1}", sampleModel.SampleNamespace,
         sampleModel.GetSampleName(_selectedLanguage));
     var sampleType = _samplesAssembly.GetType(fullTypeAsString);
     var item = Activator.CreateInstance(sampleType);
     return (Control)item;
 }
        private void SelectSample(SampleModel selectedSample)
        {
            if (selectedSample == null) return;

            SampleManager.Current.SelectedSample = selectedSample;
            DescriptionContainer.DataContext = selectedSample;

            try
            {
                SampleContainer.Content = SampleManager.Current.SampleToControl(selectedSample);

                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            catch (Exception exception)
            {
                // failed to create new instance of the sample
                // TODO handle
            }
            CategoriesRegion.Visibility = Visibility.Collapsed;
        }