发生 System.Windows.Markup.XamlParseException
我有一个绑定到对象列表的数据网格。用户可以在光标所在位置下方添加一个新行(在代码中,我创建一个新对象并将其插入到列表中的适当位置)。
想象一下数据网格有 4 行
如果光标位于第 4 行,则该行将被添加,但是,如果光标位于任何其他行(1,2 或 3),那么我会得到以下异常:
System发生.Windows.Markup.XamlParseException Message="根元素丢失。" 来源=“演示框架” 行号=0 线位置=0 堆栈跟踪: 在 System.Windows.Markup.XamlReaderHelper.RethrowAsParseException(字符串 keyString,Int32 lineNumber,Int32 linePosition,Exception innerException) 内部异常:System.Xml.XmlException Message="根元素丢失。" 源=“系统.Xml” 行号=0 线位置=0 来源Uri=“” 堆栈跟踪: 在 System.Xml.XmlTextReaderImpl.Throw(异常 e) 在 System.Xml.XmlTextReaderImpl.ParseDocumentContent() 在 System.Windows.Markup.XmlCompatibilityReader.Read() 在 System.Windows.Markup.XamlReaderHelper.Read(XamlNode& xamlNode) InnerException:
注意:当应用程序首次加载时,如果我首先添加一行(通过在最后一行),那么我还可以从任何其他行添加一行。但是,如果我首先尝试从行号 1、2、3 添加一行,则会失败!
任何帮助将不胜感激。我完全迷路了。我怀疑其他人也经历过这种情况,但也许你知道是什么原因导致这种情况或者我如何调试它,因为我不知道从哪里开始:(
private void OnAddRowBelowCursor(DataGrid datagrid)
{
try
{
int index = datagrid.SelectedIndex;
MyObject newObj = new MyObject();
ObjectList.Insert(index + 1, newObj);
Logging.log.Info("Appended object row below the cursor...");
}
catch (Exception ex)
{
Logging.log.Error("Error appending row below cursor. Reason: " + ex.ToString());
}
}
private void OnAppendRowToBottom()
{
try
{
MyObject newObj = new MyObject();
ObjectList.Add(newObj);
Logging.log.Info("Appended object row to bottom...");
}
catch (Exception ex)
{
Logging.log.Error("Error appending row to the bottom of the table. Reason: " + ex.ToString());
}
}
我还注意到在底部添加一行不会失败
谢谢
I have a datagrid bound to a list of objects. Users can add a new row below where the cursor is ( In code I create a new object and insert it in the list at the appropiate position).
Imagine that the datagrid has 4 rows
If the cursor is positioned in row number 4, then the row gets added, however, if the cursor is position in any of the other rows (1,2 or 3) then I get this exception:
System.Windows.Markup.XamlParseException occurred
Message="Root element is missing."
Source="PresentationFramework"
LineNumber=0
LinePosition=0
StackTrace:
at System.Windows.Markup.XamlReaderHelper.RethrowAsParseException(String keyString, Int32 lineNumber, Int32 linePosition, Exception innerException)
InnerException: System.Xml.XmlException
Message="Root element is missing."
Source="System.Xml"
LineNumber=0
LinePosition=0
SourceUri=""
StackTrace:
at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
at System.Windows.Markup.XmlCompatibilityReader.Read()
at System.Windows.Markup.XamlReaderHelper.Read(XamlNode& xamlNode)
InnerException:
NOTE: when the app first loads, if i first add a row (by being in the last row), then i am also able to add a row from any of the other rows. However, if i first try to add a row from row numbers 1,2,3 then it fails!
Any help will be greatly appreciated. I am totally lost. I doubt anyone else has experienced this, but maybe you know what can be causing this or how I could debug it, as I do not know where to start :(
private void OnAddRowBelowCursor(DataGrid datagrid)
{
try
{
int index = datagrid.SelectedIndex;
MyObject newObj = new MyObject();
ObjectList.Insert(index + 1, newObj);
Logging.log.Info("Appended object row below the cursor...");
}
catch (Exception ex)
{
Logging.log.Error("Error appending row below cursor. Reason: " + ex.ToString());
}
}
private void OnAppendRowToBottom()
{
try
{
MyObject newObj = new MyObject();
ObjectList.Add(newObj);
Logging.log.Info("Appended object row to bottom...");
}
catch (Exception ex)
{
Logging.log.Error("Error appending row to the bottom of the table. Reason: " + ex.ToString());
}
}
I have also notice that adding a row to the bottom does not fail
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您正在加载的数据格式错误。我可能会建议您这样一个解决方案,这将允许您 解析任何 XML 或 HTML 源,即使验证失败
Looks like the data Your are loading is badly formatted. I may sugest You such a solution, which will allow you to parse any XML or HTML source even if it's fails the validation
我将扩展库中的 RTB 与 XAMLFormatter 一起使用。
创建新行时,我没有将空字符串转换为 XAML 格式。为什么只有当我将其添加到光标下方而不是最后时才会失败,我仍然不知道。但它是固定的
I was using the RTB from the extended library with an XAMLFormatter.
When creating a new row, I wasn't converting my empty string to XAML fortmat. Why it only fails when I was adding it below the cursor and not at the end, I still do not know. But it is fixed