Пример #1
0
        private static IReadOnlyList<string> GetResourceNames( Assembly assembly, string resourceName )
        {
            Contract.Requires( assembly != null );
            Contract.Requires( !string.IsNullOrEmpty( resourceName ) );
            Contract.Ensures( Contract.Result<IReadOnlyList<string>>() != null );
            Contract.Ensures( Contract.ForAll( Contract.Result<IReadOnlyList<string>>(), name => !string.IsNullOrEmpty( name ) ) );

            // XAML resources are stored in the *.g.resources element
            var manifestKey = XamlResourcesFormat.FormatInvariant( assembly.GetAssemblyName() );
            IReadOnlyList<string> resourceNames;

            // look up cached resource names
            if ( xamlResources.TryGetValue( manifestKey, out resourceNames ) )
                return resourceNames;

            // no resources
            if ( !assembly.GetManifestResourceNames().Contains( manifestKey, StringComparer.Ordinal ) )
                throw new IOException( ExceptionMessage.MissingResourceException.FormatDefault( resourceName ) );

            // prevent cache from group exponentially
            if ( xamlResources.Count > 100 )
                xamlResources.Clear();

            using ( var stream = assembly.GetManifestResourceStream( manifestKey ) )
            {
                var reader = new System.Resources.ResourceReader( stream );
                resourceNames = new List<string>( reader.Cast<System.Collections.DictionaryEntry>().Select( entry => (string) entry.Key ) );
            }

            // cache resource names so the manifest doesn't have to be reloaded
            xamlResources[manifestKey] = resourceNames;

            return resourceNames;
        }
Пример #2
0
        public static Uri ResourceUri(string resource, string subfolder = "Icons", Assembly assembly = null)
        {
            assembly = assembly ?? typeof(ResourceHelper).Assembly;

            return new Uri(string.Format(
                "pack://application:,,,/{0};component/{1}/{2}",
                assembly.GetAssemblyName(),
                subfolder,
                resource));
        }
Пример #3
0
        private string GetVersionFromAssembly(Assembly assembly) {
            if (assembly == null)
                return null;

            string version = assembly.GetInformationalVersion();
            if (String.IsNullOrEmpty(version) || String.Equals(version, "0.0.0.0"))
                version = assembly.GetFileVersion();

            if (String.IsNullOrEmpty(version) || String.Equals(version, "0.0.0.0"))
                version = assembly.GetVersion();
            
            if (String.IsNullOrEmpty(version) || String.Equals(version, "0.0.0.0")) {
                var assemblyName = assembly.GetAssemblyName();
                version = assemblyName != null ? assemblyName.Version.ToString() : null;
            }

            return !String.IsNullOrEmpty(version) && !String.Equals(version, "0.0.0.0") ? version : null;
        }
Пример #4
0
        public StandardMenuItem WithIcon(Assembly source, string path)
        {
            var manager = IoC.Get<IResourceService>();
            var iconSource = manager.GetBitmap(path, source.GetAssemblyName());

            if (source != null)
                Icon = new Image
                {
                    Source = iconSource,
                    Width = 16,
                    Height = 16
                };

            return this;
        }
Пример #5
0
        /// <summary>
        /// Returns the component of a given assembly
        /// <para>For example: /[assemblyName];component</para>
        /// <para>where [assemblyName] is a name of the assembly</para>
        /// </summary>
        public static string GetAssemblyComponent(this Assembly assembly)
        {
            var assemblyName = assembly.GetAssemblyName();

            return(GetAssemblyComponent(assemblyName));
        }
Пример #6
0
        /// <summary>
        /// Returns the pack for a given assembly.
        /// <para>For example: pack://application:,,,/[assemblyName];component</para>
        /// <para>where [assemblyName] is a name of the assembly</para>
        /// </summary>
        public static string GetAssemblyPack(this Assembly assembly)
        {
            string assemblyName = assembly.GetAssemblyName();

            return(GetAssemblyPack(assemblyName));
        }
 /// <summary>Converts the path to a resource into a fully qualified component URI (eg "/assembly;component/Images/MyImage.png").</summary>
 /// <param name="resourcePath">
 ///    The path to the resource from the root of the project.<BR/>
 ///    This may, or may not, contain a leading '/' character.<BR/>
 ///    For example: "Images/MyImage.png"<BR/>
 /// </param>
 /// <param name="assembly">The assembly the resource is within.</param>
 /// <returns>A fully qualified URI.</returns>
 public static Uri ToComponentUri(this string resourcePath, Assembly assembly)
 {
     resourcePath = resourcePath.TrimStart("/".ToCharArray());
     var url = string.Format("/{0};component/{1}", assembly.GetAssemblyName(), resourcePath);
     return new Uri(url, UriKind.Relative);
 }
Пример #8
0
        private static string ResolveResourceName( Assembly assembly, string resourceName )
        {
            Contract.Requires( assembly != null );
            Contract.Requires( !string.IsNullOrEmpty( resourceName ) );
            Contract.Ensures( !string.IsNullOrEmpty( Contract.Result<string>() ) );

            var manifestResources = GetResourceNames( assembly, resourceName );
            var resourceNames = MatchResourceNames( manifestResources, resourceName );

            // must be exactly one resource
            // NOTE: WPF and Silverlight throw different exceptions for missing resources; match the behavior
            if ( !resourceNames.Any() )
                throw new IOException( ExceptionMessage.MissingResourceException.FormatDefault( resourceName ) );
            else if ( resourceNames.Count > 1 )
                throw new IOException( ExceptionMessage.AmbiguousResourceException.FormatDefault( resourceName, assembly.GetAssemblyName(), resourceNames.Count ) );

            return resourceNames[0];
        }
Пример #9
0
        public StandardToolBarItem WithIcon(Assembly source, string path)
        {
            var manager = IoC.Get<IResourceManager>();
            Icon = manager.GetBitmap(path, source.GetAssemblyName());

            return this;
        }
Пример #10
0
		public StandardMenuItem WithIcon(Assembly source, string path)
		{
			var manager = IoC.Get<IResourceManager>();
			var iconSource = manager.GetBitmap(path, source.GetAssemblyName());
            //IconSource = new Uri(path, UriKind.Relative);
		    IconSource = new Uri("pack://application:,,,/"+source.GetAssemblyName() + ";component/" + path, UriKind.RelativeOrAbsolute);

			if (source != null)
				Icon = new Image
				{
					Source = iconSource,
					Width = 16,
					Height = 16
				};

			return this;
		}