protected sealed override bool TryCreateAbsoluteUri(Uri baseUri, Uri sourceUri, out Uri result)
        {
            Uri packUri;

            if (sourceUri != null)
            {
                if (SharedThemeResourceDictionary.TryConvertToPackUri(sourceUri, out packUri))
                {
                    sourceUri = packUri;
                }

                UriDescription sourceUriDescription;
                if (SharedThemeResourceDictionary.TryGetUriDescription(sourceUri, out sourceUriDescription))
                {
                    if (SharedThemeResourceDictionary.IsAssemblyInfoFilled(sourceUriDescription))
                    {
                        result = SharedThemeResourceDictionary.CreateUri(sourceUriDescription);
                        return(true);
                    }
                }
            }

            if (SharedThemeResourceDictionary.TryConvertToPackUri(baseUri, out packUri))
            {
                baseUri = packUri;
            }

            var newBaseUri = this.GetBaseUri();

            if (newBaseUri != null)
            {
                if (SharedThemeResourceDictionary.TryConvertToPackUri(newBaseUri, out packUri))
                {
                    newBaseUri = packUri;
                }

                if (baseUri != null)
                {
                    Uri combinedUri;
                    if (SharedThemeResourceDictionary.TryCombineUri(newBaseUri, baseUri, out combinedUri))
                    {
                        newBaseUri = combinedUri;
                    }
                }

                if (sourceUri != null)
                {
                    return(SharedThemeResourceDictionary.TryCombineUri(newBaseUri, sourceUri, out result));
                }
            }
            else
            {
                newBaseUri = baseUri;
            }

            return(base.TryCreateAbsoluteUri(newBaseUri, sourceUri, out result));
        }
        private static Uri CreateUri(UriDescription description)
        {
            Debug.Assert(description != null);

            return(SharedThemeResourceDictionary.CreateUri(
                       description.AssemblyName,
                       description.AssemblyVersion,
                       description.AssemblyPublicKeyToken,
                       description.RelativePath));
        }
        private static Uri CreateBaseUriFromAssemblyFullName(Assembly assembly)
        {
            Debug.Assert(assembly != null);

            var fullName               = assembly.FullName;
            var assemblyName           = SharedThemeResourceDictionary.GetAssemblyName(fullName);
            var assemblyVersion        = SharedThemeResourceDictionary.GetAssemblyVersion(fullName);
            var assemblyPublicKeyToken = SharedThemeResourceDictionary.GetAssemblyPublicKeyToken(fullName);

            return(SharedThemeResourceDictionary.CreateUri(assemblyName, assemblyVersion, assemblyPublicKeyToken, string.Empty));
        }
        private static bool TryCombineUri(Uri baseUri, Uri relativeUri, out Uri result)
        {
            result = null;

            Debug.Assert((baseUri != null) && (relativeUri != null));
            Debug.Assert(baseUri.IsAbsoluteUri);

            UriDescription baseUriDescription;

            if (!SharedThemeResourceDictionary.TryGetUriDescription(baseUri, out baseUriDescription))
            {
                return(false);
            }

            UriDescription relativeUriDescription;

            if (SharedThemeResourceDictionary.TryGetUriDescription(relativeUri, out relativeUriDescription))
            {
                if (!string.IsNullOrEmpty(relativeUriDescription.AssemblyName) && !string.Equals(relativeUriDescription.AssemblyName, baseUriDescription.AssemblyName, StringComparison.InvariantCultureIgnoreCase))
                {
                    return(false);
                }
            }
            else if (relativeUri.IsAbsoluteUri)
            {
                return(false);
            }
            else
            {
                relativeUriDescription = null;
            }

            var baseUriPath     = baseUriDescription.RelativePath;
            var relativeUriPath = (relativeUriDescription != null) ? relativeUriDescription.RelativePath : Uri.EscapeUriString(relativeUri.ToString());

            if (relativeUriPath.StartsWith(SharedThemeResourceDictionary.UriPathSeparator, StringComparison.InvariantCultureIgnoreCase))
            {
                baseUriPath     = string.Empty;
                relativeUriPath = relativeUriPath.Substring(SharedThemeResourceDictionary.UriPathSeparator.Length);
            }

            var assemblyDescription = ((relativeUriDescription != null) && !SharedThemeResourceDictionary.IsAssemblyInfoFilled(baseUriDescription))
                                  ? relativeUriDescription
                                  : baseUriDescription;
            var newBaseUri = SharedThemeResourceDictionary.CreateUri(
                assemblyDescription.AssemblyName,
                assemblyDescription.AssemblyVersion,
                assemblyDescription.AssemblyPublicKeyToken,
                baseUriPath);
            var newRelativeUri = new Uri(relativeUriPath, UriKind.Relative);

            return(Uri.TryCreate(newBaseUri, newRelativeUri, out result));
        }
        private static bool TryConvertToPackUri(Uri sourceUri, out Uri result)
        {
            if (SharedThemeResourceDictionary.IsPackUri(sourceUri))
            {
                result = new Uri(SharedThemeResourceDictionary.SchemePackBaseUri, sourceUri);
            }
            else
            {
                result = null;
            }

            return(result != null);
        }
        protected Uri CreateBaseUri(Assembly assembly)
        {
            if (assembly == null)
            {
                return(null);
            }

            try
            {
                return(SharedThemeResourceDictionary.CreateBaseUriFromAssembly(assembly));
            }
            catch (SecurityException)
            {
                return(SharedThemeResourceDictionary.CreateBaseUriFromAssemblyFullName(assembly));
            }
        }
        private static string ParseAssemblyVersion(string version)
        {
            var symbol = SharedThemeResourceDictionary.UriAssemblyVersionSymbol;

            if (string.IsNullOrEmpty(version) || !version.StartsWith(symbol, StringComparison.InvariantCultureIgnoreCase))
            {
                return(version);
            }

            Version ver;

            if (!Version.TryParse(version.Substring(symbol.Length), out ver))
            {
                return(version);
            }

            return(SharedThemeResourceDictionary.GetAssemblyVersion(ver));
        }
        private static bool TryGetUriDescription(Uri uri, out UriDescription description)
        {
            description = null;
            if ((uri == null) || !uri.IsAbsoluteUri)
            {
                return(false);
            }

            if (!string.Equals(uri.Scheme, SharedThemeResourceDictionary.UriSchemePack, StringComparison.InvariantCultureIgnoreCase))
            {
                return(false);
            }

            var segments = uri.Segments;

            if ((segments == null) || (segments.Length < 2))
            {
                return(false);
            }

            var componentPart = segments[1];
            var index         = componentPart.IndexOf(SharedThemeResourceDictionary.UriComponent, StringComparison.InvariantCultureIgnoreCase);

            if (index < 0)
            {
                return(false);
            }

            string assemblyName           = null;
            string assemblyVersion        = null;
            string assemblyPublicKeyToken = null;

            var assemblyInfo = componentPart.Substring(0, index);

            if (assemblyInfo.Length > 0)
            {
                var assemblyParts = assemblyInfo.Split(new string[] { SharedThemeResourceDictionary.UriAssemblyInfoSeparator }, StringSplitOptions.None);
                Debug.Assert(assemblyParts.Length <= 3);

                assemblyName           = (assemblyParts.Length > 0) ? assemblyParts[0] : null;
                assemblyVersion        = (assemblyParts.Length > 1) ? SharedThemeResourceDictionary.ParseAssemblyVersion(assemblyParts[1]) : null;
                assemblyPublicKeyToken = (assemblyParts.Length > 2) ? assemblyParts[2] : null;
            }

            string relativePath = null;

            if (segments.Length > 2)
            {
                var sb = new StringBuilder();
                for (int i = 2; i < segments.Length; i++)
                {
                    sb.Append(segments[i]);
                }

                relativePath = sb.ToString();
            }

            description = new UriDescription(assemblyName, assemblyVersion, assemblyPublicKeyToken, relativePath);

            return(true);
        }