Exemplo n.º 1
0
        /// <summary>
        /// Populate the Tab with samples name for a given control.
        /// </summary>
        /// <param name="sampleAssembly">Name of the sample assembly.</param>
        /// <param name="controlName">Name of the control.</param>
        private void PopulateSamplesTab(Assembly sampleAssembly, string controlName)
        {
            if (SamplesTab == null)
            {
                return;
            }

            // Unhook this event when dynamically before adding new tabs.
            SamplesTab.SelectionChanged -= OnSamplesTabSelectionChanged;

            if (SamplesTab.Items.Count > 0)
            {
                SamplesTab.Items.Clear();
            }

            // Construct a sorted list of samples
            IList <SampleBrowserItem> samples = new List <SampleBrowserItem>();

            foreach (Type type in sampleAssembly.GetTypes())
            {
                SampleAttribute attribute = type.GetCustomAttributes(typeof(SampleAttribute), false).OfType <SampleAttribute>().FirstOrDefault();

                CategoryAttribute categoryAttribute = type.GetCustomAttributes(typeof(CategoryAttribute), false).OfType <CategoryAttribute>().FirstOrDefault();
                if (attribute != null && categoryAttribute != null)
                {
                    if (categoryAttribute.Category.Equals(controlName))
                    {
                        SampleBrowserItem.AddSample(samples, type, attribute);
                    }
                }
            }
            // Populate the Tab Control with the samples.
            foreach (SampleBrowserItem item in samples)
            {
                TabItem tabItem = new TabItem();
                if (SampleTabItemStyle != null)
                {
                    tabItem.Style = SampleTabItemStyle;
                }

                tabItem.Header = item;
                SamplesTab.Items.Add(tabItem);
            }

            // Hook up this event when done creating the Sample tabs.
            SamplesTab.SelectionChanged += OnSamplesTabSelectionChanged;

            // Select first tab
            if (SamplesTab.Items.Count > 0)
            {
                (SamplesTab.Items[0] as TabItem).IsSelected = true;
                LoadSample();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Create a SampleBrowserItem and inserts into the right location.
        /// </summary>
        /// <param name="Samples">Collections of Samples.</param>
        /// <param name="type">Type of Sample.</param>
        /// <param name="attribute">Attributes for the Sample.</param>
        internal static void AddSample(IList <SampleBrowserItem> Samples, Type type, SampleAttribute attribute)
        {
            string name = attribute.Name;
            // Find any existing item
            SampleBrowserItem item = null;

            if (Samples != null)
            {
                foreach (SampleBrowserItem other in Samples)
                {
                    if (string.Compare(other.OriginalName, name, StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        item = other;
                        break;
                    }
                }
            }
            // Escape any name sorting
            string escapedName = name;

            if (name[0] == '(')
            {
                int index = name.IndexOf(')');
                if (index > 0)
                {
                    escapedName = name.Substring(index + 1, name.Length - index - 1);
                }
            }

            item = new SampleBrowserItem {
                Name = escapedName, OriginalName = name, SampleType = type, SampleLevel = attribute.DifficultyLevel
            };

            // Insert the item into the list in sorted order (linearly
            // because no item should have very many children)
            int sortedIndex = 0;

            foreach (SampleBrowserItem other in Samples)
            {
                if (string.Compare(other.OriginalName, name, StringComparison.OrdinalIgnoreCase) > 0)
                {
                    break;
                }
                else
                {
                    sortedIndex++;
                }
            }
            Samples.Insert(sortedIndex, item);
        }