SilverLight中自定义图标控件

SilverLight中自定义图标控件,第1张

概述SilverLight中自定义图标控件   1.       建立一个用于读取资源的类SharedResources  using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.IO; using System.Linq; using System.Refl

Silverlight中自定义图标控件

 

1.       建立一个用于读取资源的类SharedResources

 using System;

using System.Collections.Generic;

using System.Diagnostics.CodeAnalysis;

using System.IO;

using System.linq;

using System.Reflection;

using System.windows;

using System.windows.Controls;

using System.windows.Markup;

using System.windows.Media.Imaging;

using System.Xml.linq;

 

namespace System.windows.Controls.Samples

{

    /// <summary>

    /// Utility to to load shared resources into another ResourceDictionary.

    /// </summary>

    public static class SharedResources

    {

        /// <summary>

        /// Prefix of images loaded as resources.

        ///需要将资源嵌入,路径用命名空间的形式定义.

        /// </summary>

        private const string ResourceImagePrefix = "System.windows.Controls.Samples.Images.";

 

        /// <summary>

        /// Prefix of icons loaded as resources.

        ///需要将资源嵌入,路径用命名空间的形式定义.

        /// </summary>

        private const string ResourceIconPrefix = "System.windows.Controls.Samples.Icons.";

 

        /// <summary>

        /// Get an embedded resource image from the assembly.

        /// </summary>

        /// <param name="name">name of the image resource.</param>

        /// <returns>

        /// Desired embedded resource image from the assembly.

        /// </returns>

        public static Image Getimage(string name)

        {

            return CreateImage(ResourceImagePrefix,name);

        }

 

        /// <summary>

        /// Get an embedded resource icon from the assembly.

        /// </summary>

        /// <param name="name">name of the icon resource.</param>

        /// <returns>

        /// Desired embedded resource icon from the assembly.

        /// </returns>

        public static Image GetIcon(string name)

        {

            return CreateImage(ResourceIconPrefix,name);

        }

 

        /// <summary>

        /// A cached dictionary of the bitmap images.

        /// </summary>

        private static IDictionary<string,BitmAPImage> cachedBitmAPImages = new Dictionary<string,BitmAPImage>();

 

        /// <summary>

        /// Get an embedded resource image from the assembly.

        /// </summary>

        /// <param name="prefix">The prefix of the full name of the resource.</param>

        /// <param name="name">name of the image resource.</param>

        /// <returns>

        /// Desired embedded resource image from the assembly.

        /// </returns>

        public static Image CreateImage(string prefix,string name)

        {

            Image image = new Image { Tag = name };

 

            BitmAPImage source = null;

            string resourcename = prefix + name;

            if (!cachedBitmAPImages.TryGetValue(resourcename,out source))

            {

                Assembly assembly = typeof(SharedResources).Assembly;

 

                using (Stream resource = assembly.GetManifestResourceStream(resourcename))

                {

                    if (resource != null)

                    {

                        source = new BitmAPImage();

                        source.SetSource(resource);

                    }

                }

                cachedBitmAPImages[resourcename] = source;

            }

            image.source = source;

            return image;

        }

 

        /// <summary>

        /// Get all of the names of embedded resources images in the assembly.

        /// </summary>

        /// <returns>

        /// All of the names of embedded resources images in the assembly.

        /// </returns>

        [SuppressMessage("Microsoft.Design","CA1024:UsePropertIEsWhereAppropriate",Justification = "Does more work than a property should.")]

        public static IEnumerable<string> Getimagenames()

        {

            return GetResourcenames(ResourceImagePrefix);

        }

 

        /// <summary>

        /// Get all of the names of embedded resources icons in the assembly.

        /// </summary>

        /// <returns>

        /// All of the names of embedded resources icons in the assembly.

        /// </returns>

        [SuppressMessage("Microsoft.Design",Justification = "Does more work than a property should.")]

        public static IEnumerable<string> GetIconnames()

        {

            return GetResourcenames(ResourceIconPrefix);

        }

 

        /// <summary>

        /// Get all of the images in the assembly.

        /// </summary>

        /// <returns>All of the images in the assembly.</returns>

        [SuppressMessage("Microsoft.Design",Justification = "Does more work than a property should")]

        public static IEnumerable<Image> Getimages()

        {

            foreach (string name in Getimagenames())

            {

                yIEld return Getimage(name);

            }

        }

 

        /// <summary>

        /// Get all of the icons in the assembly.

        /// </summary>

        /// <returns>All of the icons in the assembly.</returns>

        [SuppressMessage("Microsoft.Design",Justification = "Does more work than a property should")]

        public static IEnumerable<Image> GetIcons()

        {

            foreach (string name in GetIconnames())

            {

                yIEld return Getimage(name);

            }

        }

 

        /// <summary>

        /// Get all the names of embedded resources in the assembly with the

        /// provIDed prefix value.

        /// </summary>

        /// <param name="prefix">The prefix for the full resource name.</param>

        /// <returns>Returns an enumerable of all the resource names that match.</returns>

        private static IEnumerable<string> GetResourcenames(string prefix)

        {

            Assembly assembly = typeof(SharedResources).Assembly;

            foreach (string name in assembly.GetManifestResourcenames())

            {

                // Ignore resources that don't share the images prefix

                if (!name.StartsWith(prefix,StringComparison.OrdinalignoreCase))

                {

                    continue;

                }

 

                // Trim the prefix off of the name

                yIEld return name.Substring(prefix.Length,name.Length - prefix.Length);

            }

        }

    }

}

 

@H_404_1612@2.      定义节点SampleTreeItem

using System;

using System.Collections.Generic;

using System.Collections.ObjectModel;

using System.windows.Markup;

using System.windows.Controls;

 

namespace System.windows.Controls.Samples

{

    /// <summary>

    /// The SampleTreeItem represents a node in the TreeVIEw. 

    /// </summary>

    [contentproperty("Items")]

    public class SampleTreeItem

    {

        /// <summary>

        /// Gets or sets the name of the TreeVIEw node.

        /// </summary>

        public string TreeItemname { get; set; }

 

        /// <summary>

        ///  Gets a collection of SampleTreeItems.

        /// </summary>

        public Collection<SampleTreeItem> Items { get; private set; }

 

        /// <summary>

        /// Initialize a SampleTreeItem.

        /// </summary>

        public SampleTreeItem()

        {

            Items = new Collection<SampleTreeItem>();

        }

 

        /// <summary>

        /// Gets or sets the resource name of the Icon representing this

        /// node.

        /// </summary>

        public string IconResourcename { get; set; }

 

        /// <summary>

        /// Gets the icon representing this type.

        /// </summary>

        public Image Icon

        {

            get

            {

                return SharedResources.GetIcon(IconResourcename);

            }

        }

    }

}

 

@H_404_1612@3.      定义数据源,这里将数据写死在App.xaml

<toolkit:ObjectCollection x:Key="SampleTreeVIEw" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">

            <samplesCommon:SampleTreeItem TreeItemname="Controls">

                <samplesCommon:SampleTreeItem TreeItemname="Calendar" IconResourcename="Calendar.png"/>

                <samplesCommon:SampleTreeItem TreeItemname="ChilDWindow" IconResourcename="ChilDWindow.png" />

                <samplesCommon:SampleTreeItem TreeItemname="DatePicker" IconResourcename="DatePicker.png" />

                <samplesCommon:SampleTreeItem TreeItemname="GrIDSplitter" IconResourcename="GrIDSplitter.png" />

                <samplesCommon:SampleTreeItem TreeItemname="TabControl" IconResourcename="TabControl.png" />

                <samplesCommon:SampleTreeItem TreeItemname="TreeVIEw" IconResourcename="TreeVIEw.png" />

            </samplesCommon:SampleTreeItem>

            <samplesCommon:SampleTreeItem TreeItemname="Data">

                <samplesCommon:SampleTreeItem TreeItemname="DataGrID" IconResourcename="DataGrID.png" />

                <samplesCommon:SampleTreeItem TreeItemname="DataPager" IconResourcename="DataPager.png" />

            </samplesCommon:SampleTreeItem>

            <samplesCommon:SampleTreeItem TreeItemname="DataForm">

                <samplesCommon:SampleTreeItem TreeItemname="DataForm" IconResourcename="DataForm.png" />

            </samplesCommon:SampleTreeItem>

            <samplesCommon:SampleTreeItem TreeItemname="Data input">

                <samplesCommon:SampleTreeItem TreeItemname="ValIDation" IconResourcename="ValIDationSummary.png" />

            </samplesCommon:SampleTreeItem>

            <samplesCommon:SampleTreeItem TreeItemname="DataVisualization">

                <samplesCommon:SampleTreeItem TreeItemname="Area SerIEs" IconResourcename="AreaSerIEs.png"/>

                <samplesCommon:SampleTreeItem TreeItemname="bar SerIEs" IconResourcename="barSerIEs.png"/>

                <samplesCommon:SampleTreeItem TreeItemname="Bubble SerIEs" IconResourcename="BubbleSerIEs.png"/>

                <samplesCommon:SampleTreeItem TreeItemname="Column SerIEs" IconResourcename="ColumnSerIEs.png"/>

                <samplesCommon:SampleTreeItem TreeItemname="line SerIEs" IconResourcename="lineserIEs.png"/>

                <samplesCommon:SampleTreeItem TreeItemname="PIE SerIEs" IconResourcename="PIESerIEs.png"/>

                <samplesCommon:SampleTreeItem TreeItemname="Scatter SerIEs" IconResourcename="ScatterSerIEs.png"/>

                <samplesCommon:SampleTreeItem TreeItemname="TreeMap" IconResourcename="TreeMap.png" />

            </samplesCommon:SampleTreeItem>

            <samplesCommon:SampleTreeItem TreeItemname="input">

                <samplesCommon:SampleTreeItem TreeItemname="autocompletebox" IconResourcename="autocompletebox.png"/>

                <samplesCommon:SampleTreeItem TreeItemname="buttonSpinner" IconResourcename="buttonSpinner.png"/>

                <samplesCommon:SampleTreeItem TreeItemname="Domainupdown" IconResourcename="Domainupdown.png"/>

                <samplesCommon:SampleTreeItem TreeItemname="Numericupdown" IconResourcename="Numericupdown.png"/>

                <samplesCommon:SampleTreeItem TreeItemname="rating" IconResourcename="rating.png"/>

                <samplesCommon:SampleTreeItem TreeItemname="TimePicker" IconResourcename="TimePicker.png"/>

                <samplesCommon:SampleTreeItem TreeItemname="updownBase" IconResourcename="updownBase.png"/>

            </samplesCommon:SampleTreeItem>

            <samplesCommon:SampleTreeItem TreeItemname="Layout">

                <samplesCommon:SampleTreeItem TreeItemname="Accordion" IconResourcename="Accordion.png" />

                <samplesCommon:SampleTreeItem TreeItemname="TransitioningContentControl" IconResourcename="TransitioningContentControl.png" />

            </samplesCommon:SampleTreeItem>

            <samplesCommon:SampleTreeItem TreeItemname="Navigation">

                <samplesCommon:SampleTreeItem TreeItemname="Navigation" IconResourcename="Page.png" />

            </samplesCommon:SampleTreeItem>

            <samplesCommon:SampleTreeItem TreeItemname="Theming">

                <samplesCommon:SampleTreeItem TreeItemname="ImplicitStyleManager" IconResourcename="ImplicitStyleManager.png"/>

                <samplesCommon:SampleTreeItem TreeItemname="theme browser" IconResourcename="themebrowser.png"/>

            </samplesCommon:SampleTreeItem>

            <samplesCommon:SampleTreeItem TreeItemname="Toolkit">

                <samplesCommon:SampleTreeItem TreeItemname="DockPanel" IconResourcename="DockPanel.png" />

                <samplesCommon:SampleTreeItem TreeItemname="Expander" IconResourcename="Expander.png" />

                <samplesCommon:SampleTreeItem TreeItemname="GlobalCalendar" IconResourcename="Calendar.png"/>

                <samplesCommon:SampleTreeItem TreeItemname="VIEwBox" IconResourcename="VIEwBox.png" />

                <samplesCommon:SampleTreeItem TreeItemname="WrapPanel" IconResourcename="WrapPanel.png" />

            </samplesCommon:SampleTreeItem>

        </toolkit:ObjectCollection>

 

@H_404_1612@4.     调用生成带图标节点的树

using System;

using System.windows;

using System.Collections.Generic;

using System.linq;

 

namespace System.windows.Controls.Samples

{

    /// <summary>

    /// System.windows.Controls samples application.

    /// </summary>

    public partial class App : Application

    {

        /// <summary>

        /// Initializes a new instance of the App class.

        /// </summary>

        public App()

        {

            Startup += delegate

            {

                RootVisual = new Samplebrowser(this.GetType().Assembly,SampleTreeItems);

            };

            InitializeComponent();

        }

 

        /// <summary>

        /// Gets a collection of SampleTreeItems to populate the Samplebrowser TreeVIEw.

        /// </summary>

        public static IEnumerable<SampleTreeItem> SampleTreeItems

        {

            get

            {

                IEnumerable<object> data = Application.Current.Resources["SampleTreeVIEw"] as IEnumerable<object>;

                return (data != null) ?

                    data.OfType<SampleTreeItem>() :

                    Enumerable.Empty<SampleTreeItem>();

            }

        }

    }

}

 

5.注意事项

SharedResourcesSampleTreeItem需要与图标放在同一个项目里,图标需要嵌入到资源,而调用的程序可以在别的项目。

 

同一个程序可以有多个项目,每个项目可以有自已的“程序集名称”,

但项目间应该使用同一套“命名空间”,也就是说,不同项目间的“命名空间”不可以有重复.

 

如“程序集名称”可以分别定义成:

System.windows.Controls.Samples

System.windows.Controls.Samples.Common

但它们的默认“命名空间”都是System.windows.Controls.Samples

 

总之,程序集名称是用来管理文件的(不一定是层次结构)。而命名空间才是用来管理代码的层次结构的。

 

为什么会这么设计呢。

有时候一个命名空间(一般对应一个文件夹)中的内容可能会很多,这时我们就需要把它拆分成不同的程序集。以便于管理。而有时候呢,代码量很少,不同的命名空间就直接放在一个文件中了。

 

真正使用时,

一个文件夹对应一个命名空间。如果不同的程序集使用的是同一个命名空间,那么他们应该可以放在同一个文件夹中。也就时说相同命名空间的项目之间,它们的第一级文件及文件夹的名称不可以有重复的。

总结

以上是内存溢出为你收集整理的SilverLight中自定义图标控件全部内容,希望文章能够帮你解决SilverLight中自定义图标控件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1046153.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)