MaterialDesignInXAML/MaterialDesignInXamlToolkit · error · ArgumentException
Non primary hues provided.
Error message
Non primary hues provided.
What it means
`Swatch` (MaterialDesignColors.Wpf) represents a named colour swatch and requires at least one primary hue to pick an `ExemplarHue`. Its constructor throws `ArgumentException` if `primaryHues` resolves to an empty list, because the exemplar lookup `primaryHuesList[ExemplarHueIndex]` would otherwise throw `ArgumentOutOfRangeException`.
Source
Thrown at src/MaterialDesignColors.Wpf/Swatch.cs:15
namespace MaterialDesignColors;
/// <summary>
/// Defines a single colour swatch.
/// </summary>
public class Swatch
{
public Swatch(string name, IEnumerable<Hue> primaryHues, IEnumerable<Hue> secondaryHues)
{
if (name is null) throw new ArgumentNullException(nameof(name));
if (primaryHues is null) throw new ArgumentNullException(nameof(primaryHues));
if (secondaryHues is null) throw new ArgumentNullException(nameof(secondaryHues));
var primaryHuesList = primaryHues.ToList();
if (primaryHuesList.Count == 0) throw new ArgumentException("Non primary hues provided.", nameof(primaryHues));
Name = name;
PrimaryHues = primaryHuesList;
var secondaryHuesList = secondaryHues.ToList();
SecondaryHues = secondaryHuesList;
ExemplarHue = primaryHuesList[ExemplarHueIndex];
if (SecondaryHueIndex >= 0 && SecondaryHueIndex < secondaryHuesList.Count)
{
SecondaryExemplarHue = secondaryHuesList[SecondaryHueIndex];
}
}
public string Name { get; }
public Hue ExemplarHue { get; }
public Hue? SecondaryExemplarHue { get; }
View on GitHub (pinned to 98edec3a0b)
Solutions
- Ensure the primaryHues collection passed to the `Swatch` constructor contains at least one `Hue`.
- Skip/handle the empty case upstream: `if (!primaryHues.Any()) continue;` before constructing the swatch.
- If building a custom `SwatchesProvider`, validate the resource assembly actually defines primary hue entries.
- Unit-test custom swatch generation against empty inputs.
Example fix
// before
var swatch = new Swatch("Red", Enumerable.Empty<Hue>(), secondaryHues); // throws
// after
var primaryHues = BuildPrimaryHues();
if (primaryHues.Count == 0) continue;
var swatch = new Swatch("Red", primaryHues, secondaryHues); Defensive patterns
Strategy: validation
Validate before calling
var primary = BuildPrimaryHues(); if (primary is null || primary.Count == 0) continue; var swatch = new Swatch(name, primary, secondary);
Type guard
static bool HasPrimaryHues(IEnumerable<Hue> hues)
=> hues is not null && hues.Any(); Prevention
- Validate custom swatch sources contain primary hues before constructing Swatch.
- Unit-test swatch builders against empty/filtered inputs.
- Use the official MaterialDesignColors resource assemblies.
When it happens
Trigger: Constructing `new Swatch(name, primaryHues, secondaryHues)` with `primaryHues` being an empty enumeration (e.g. `Enumerable.Empty<Hue>()` or a filtered list that yielded nothing).
Common situations: A custom swatch provider that filters hues and returns none; a malformed/empty theme resource assembly; code that builds swatches dynamically and passes an empty source collection.
Related errors
- kinds must contain at least one value
- Entry {entry.Key} was not of type Color
- Entry {foregroundKey} was not of type Color
- MinuteSelectionStep must be a divisor of 60 and between 1 an
- ColorReference does not contain any color
AI-assisted analysis of MaterialDesignInXAML/MaterialDesignInXamlToolkit@98edec3a0b (2026-08-13).
Data as JSON: /api/errors/c55a72b907bfb3bb.
Report an issue: GitHub.