dotnet/wpf · error · ArgumentException

SR.Format(SR.ResourceDictionaryLoadFromFailure, value ==…

Error message

SR.Format(SR.ResourceDictionaryLoadFromFailure, value == null ? "''" : value.ToString())

What it means

ResourceDictionary.Source only accepts a non-null, non-empty absolute Uri; setting it to null or an empty Uri throws an ArgumentException with SR.ResourceDictionaryLoadFromFailure, echoing the (empty) value. The library throws this because it cannot begin loading a dictionary from an empty source.

Solutions

  1. Supply a valid, non-empty absolute Uri (typically a pack URI like new Uri("pack://application:,,,/Themes/Theme.xaml")).
  2. Validate the source string from configuration before constructing the Uri.
  3. Guard the assignment: only set Source when the Uri and its OriginalString are non-empty.

Example fix

// before
dictionary.Source = new Uri(config["ThemePath"] ?? "");
// after
string themePath = config["ThemePath"];
if (!string.IsNullOrEmpty(themePath))
    dictionary.Source = new Uri(themePath, UriKind.Absolute);
Defensive patterns

Strategy: validation

Validate before calling

if (source == null || string.IsNullOrEmpty(source.OriginalString)) throw new ArgumentException("Resource dictionary Source must be a non-empty Uri", nameof(source));

Type guard

bool IsValidSource(Uri source) => source != null && !string.IsNullOrEmpty(source.OriginalString);

Try / catch

try { rd.Source = new Uri(path, UriKind.Absolute); } catch (ArgumentException ex) when (ex.Message.Contains("ResourceDictionaryLoadFromFailure") || ex.Message.Contains("Load from")) { log.Error("Invalid dictionary source", ex); }

Prevention

When it happens

Trigger: Assigning ResourceDictionary.Source = null or Source = new Uri("") (empty OriginalString) in code, or a XAML/binding pipeline that resolves an empty pack URI into Source.

Common situations: Merged-dictionary setup in App.xaml code-behind where a config/appsetting string for a resource path is empty or null; dynamically building ResourceDictionaries at startup.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/a79b9ef9228cc2af. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ResourceDictionary.cs:136

                return _mergedDictionaries;
            }
        }

        ///<summary>
        ///     Uri to load this resource from, it will clear the current state of the ResourceDictionary
        ///</summary>
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public Uri Source
        {
            get
            {
                return _source;
            }
            set
            {
                if (value == null || String.IsNullOrEmpty(value.OriginalString))
                {
                    throw new ArgumentException(SR.Format(SR.ResourceDictionaryLoadFromFailure, value == null ? "''" : value.ToString()));
                }

                ResourceDictionaryDiagnostics.RemoveResourceDictionaryForUri(_source, this);

                ResourceDictionarySourceUriWrapper uriWrapper = value as ResourceDictionarySourceUriWrapper;

                Uri sourceUri;

                // If the Uri we received is a ResourceDictionarySourceUriWrapper it means
                // that it is being passed down by the Baml parsing code, and it is trying to give us more
                // information to avoid possible ambiguities in assembly resolving. Use the VersionedUri
                // to resolve, and the set _source to the OriginalUri so we don't change the return of Source property.
                // The versioned Uri is not stored, if the version info is needed while debugging, once this method
                // returns _reader should be set, from there BamlSchemaContext.LocalAssembly contains the version info.
                if (uriWrapper == null)
                {
                    _source = value;
                    sourceUri = _source;

View on GitHub (pinned to 81131a70a4)