NickeManarin/ScreenToGif · warning · IndexOutOfRangeException
Index out of range while trying to save the resource diction
Error message
Index out of range while trying to save the resource dictionary.
What it means
Thrown by LocalizationHelper.SaveSelected when the selectedIndex parameter is negative or exceeds the last index of Application.Current.Resources.MergedDictionaries. The method saves a specific merged dictionary to a file via XamlWriter.Save.
Source
Thrown at ScreenToGif.Util/LocalizationHelper.cs:453
//Insert at the new position.
Application.Current.Resources.MergedDictionaries.Insert(newIndex, dictionaryAux);
return true;
}
catch (Exception ex)
{
LogWriter.Log(ex, "Move Resource", selectedIndex);
return false;
}
}
public static void SaveSelected(int selectedIndex, string path)
{
try
{
if (selectedIndex < 0 || selectedIndex > Application.Current.Resources.MergedDictionaries.Count - 1)
throw new IndexOutOfRangeException("Index out of range while trying to save the resource dictionary.");
var settings = new XmlWriterSettings { Indent = true };
using var writer = XmlWriter.Create(path, settings);
XamlWriter.Save(Application.Current.Resources.MergedDictionaries[selectedIndex], writer);
}
catch (Exception ex)
{
LogWriter.Log(ex, "Save Resource", selectedIndex);
//Rethrowing, because it's more useful to catch later
throw;
}
}
public static bool Remove(int selectedIndex)
{
try
{View on GitHub (pinned to a4d0a67c21)
Solutions
- Validate selectedIndex against the current MergedDictionaries.Count before calling SaveSelected.
- After removing or reordering dictionaries, reset the UI selection to a valid index.
- Use data binding with proper SelectedIndex handling so the UI cannot reference a stale index.
- Clamp selectedIndex to valid bounds or disable the save button when no valid selection exists.
Example fix
// before
if (selectedIndex < 0 || selectedIndex > Application.Current.Resources.MergedDictionaries.Count - 1)
throw new IndexOutOfRangeException("Index out of range while trying to save the resource dictionary.");
// after: clamp and return false instead of throwing
var count = Application.Current.Resources.MergedDictionaries.Count;
if (selectedIndex < 0 || selectedIndex >= count)
return false; // caller treats as 'nothing to save' Defensive patterns
Strategy: validation
Validate before calling
var count = Application.Current.Resources.MergedDictionaries.Count;
if (selectedIndex < 0 || selectedIndex >= count)
{
// Don't call SaveSelected; the index is stale
return;
} Type guard
static bool IsValidDictionaryIndex(int index)
{
return index >= 0 && index < Application.Current.Resources.MergedDictionaries.Count;
} Try / catch
try
{
LocalizationHelper.SaveSelected(selectedIndex, path);
}
catch (IndexOutOfRangeException ex)
{
LogWriter.Log(ex, $"Invalid dictionary index: {selectedIndex}");
// Reset UI selection to a valid index
} Prevention
- After adding/removing/reordering dictionaries, immediately update the UI selection to a valid index.
- Disable the Save button when selectedIndex is -1 or out of range.
- Use ObservableCollection data binding to keep the UI in sync with the dictionaries list.
When it happens
Trigger: SaveSelected is called with selectedIndex < 0 or selectedIndex > MergedDictionaries.Count - 1. This happens when the UI list selection is out of sync with the actual merged dictionaries collection — for example, after dictionaries were removed but the selection index wasn't updated.
Common situations: User removes a localization dictionary and then immediately tries to save the previously selected index, which no longer exists. Race condition where the MergedDictionaries collection is modified between the UI binding update and the SaveSelected call. selectedIndex was set to -1 (no selection) and not validated by the caller.
Related errors
- File is empty
- File not found
- Can't get language codes. Path to language codes is null
- Empty response from server when getting language codes
- Empty response from server when getting language codes path
AI-assisted analysis of NickeManarin/ScreenToGif@a4d0a67c21 (2026-08-13).
Data as JSON: /api/errors/d73b8fcc61e73c85.
Report an issue: GitHub.