private static ResourceDictionary LoadOrDefault(string path, int trial = 0, XamlObjectWriterException exception = null) { ResourceDictionary resource = null; try { if (!File.Exists(path)) { return(new ResourceDictionary()); } if (exception != null) { var content = File.ReadAllLines(path).ToList(); content.RemoveAt(exception.LineNumber - 1); File.WriteAllLines(path, content); } using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { try { //Read in ResourceDictionary File resource = (ResourceDictionary)XamlReader.Load(fs); } catch (XamlParseException xx) { if (xx.InnerException is XamlObjectWriterException inner && trial < 5) { return(LoadOrDefault(path, trial + 1, inner)); } resource = new ResourceDictionary(); } catch (Exception ex) { //Sets a default value if null. resource = new ResourceDictionary(); } } //Tries to load the resource from disk. //resource = new ResourceDictionary {Source = new Uri(path, UriKind.RelativeOrAbsolute)}; } catch (Exception) { //Sets a default value if null. resource = new ResourceDictionary(); } return(resource); }
private XamlException CreateException(string message, Exception innerException) { XamlException ex; if (_isWriter) { ex = new XamlObjectWriterException(message, innerException); } else { ex = new XamlObjectReaderException(message, innerException); } return((LineInfo != null) ? LineInfo.WithLineInfo(ex) : ex); }
private XamlException CreateException(string message, Exception innerException) { XamlException exception; if (this._isWriter) { exception = new XamlObjectWriterException(message, innerException); } else { exception = new XamlObjectReaderException(message, innerException); } if (this.LineInfo == null) { return(exception); } return(this.LineInfo.WithLineInfo(exception)); }