silverlight   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了SilverLight中自定义图标控件大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

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

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 @R_874_10495@ng resourceImagePrefix = "System.Windows.Controls.Samples.Images.";

 

        /// <sumMary>

        /// Prefix of icons loaded as resources.

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

        /// </sumMary>

        private const @R_874_10495@ng 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(@R_874_10495@ng 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(@R_874_10495@ng Name)

        {

            return CreateImage(resourceIconPrefix,Name);

        }

 

        /// <sumMary>

        /// A cached Dictionary of the bitmap images.

        /// </sumMary>

        private static IDictionary<@R_874_10495@ng,BitmapImage> cachedBitmapImages = new Dictionary<@R_874_10495@ng,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(@R_874_10495@ng prefix,@R_874_10495@ng Name)

        {

            Image image = new Image { Tag = name };

 

            BitmapImage source = null;

            @R_874_10495@ng 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<@R_874_10495@ng> 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<@R_874_10495@ng> 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 (@R_874_10495@ng 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 (@R_874_10495@ng 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<@R_874_10495@ng> Getresourcenames(@R_874_10495@ng prefiX)

        {

            Assembly assembly = typeof(Sharedresources).Assembly;

            foreach (@R_874_10495@ng name in assembly.GetManifestresourcenames())

            {

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

                if (!name.StartsWith(prefix,@R_874_10495@ngComparison.ordinalIgnoreCasE))

                {

                    conTinue;

                }

 

                // Trim the prefix off of the name

                yield return name.Sub@R_874_10495@ng(prefix.Length,name.Length - prefix.Length);

            }

        }

    }

}

 

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 @R_874_10495@ng 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 @R_874_10495@ng Iconresourcename { get; set; }

 

        /// <sumMary>

        /// Gets the icon represenTing this type.

        /// </sumMary>

        public Image Icon

        {

            get

            {

                return Sharedresources.GetIcon(IconresourceName);

            }

        }

    }

}

 

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

<toolkit:ObjectCollection x:Key="SampleTreeView" xmlns="http://scheR_178_11845@as.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="Accor@L_674_29@n" Iconresourcename="Accor@L_674_29@n.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>

 

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中自定义图标控件所遇到的程序开发问题。

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。