I created and initialized quite a large Dictionary and got this error when starting the application:
An unhandled exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll
Additional information: Zeilennummer "3" und Zeilenposition "9" von "Durch den Aufruf des
Konstruktors für Typ "AbiZeitung.MainWindow", der den angegebenen Bindungseinschränkungen
entspricht, wurde eine Ausnahme ausgelöst.".
If there is a handler for this exception, the program may be safely continued.
Here is a part of the Dictionary:
Dictionary<string, string> teacherList = new Dictionary<string, string>()
{
{"Mr", "M"},
{"Mr", "D"},
{"Mr", "S"},
{"Mr", "W"}
};
Any guess why this happens?
A
Dictionary<T,U>needs to have unique keys. Right now, your collection initializer will raise an exception (ArgumentException) when it tries to add{"Mr","D"}, as “Mr” is already a key.For details, see Dictionary.Add, under exceptions:
If your View creates this dictionary, even indirectly (ie: it’s in a ViewModel generated from the xaml), you’ll get an exception, which in turn will cause the
XamlParseExceptionto occur.If you need a collection like this, I recommend making an
ObservableCollection<T>(orList<T>if this won’t change) of a custom type which contains the two strings.