本文介绍了对静态感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个C#程序。我正在观看一个目录,要求更改xml文件,当文件发生变化时,我想准备好xml并更新一个富文本框。 当我指定文件时,一切正常。它读取它并填充RTB罚款。但是当我添加代码以尝试在文件更改时自动执行时我得到错误 错误1非静态字段需要对象引用,方法,或属性'xml_reader.Form1.readXML(string)' 我已经阅读了几个这方面的例子,但我仍然没有得到它。 如果它应该触发I have a C# program. I'm watching a directory for changes to an xml file, and when the file changes I want to ready the xml and update a rich textbox.It all works when I specify the file. It reads it and populates the RTB fine. But when I add code to try to do it automatically when the files changes I get the errorError1An object reference is required for the non-static field, method, or property 'xml_reader.Form1.readXML(string)'I've read several examples on this but I still don't get it.this fires when it shouldprivate static void OnChanged(object source, FileSystemEventArgs e) { // Specify what is done when a file is changed, created, or deleted. string path = e.FullPath; Console.WriteLine("File: " + path + " " + e.ChangeType); readXML(path); } 我想打电话I want to callprivate void readXML(string Filename){ int linenumbers = 0; string stringvalue; string stringname; String title = "POS PILOT LAPS ELAPSED SEED FAST LAP \n"; richTextBox1.Text = title; XmlTextReader reader = new XmlTextReader(Filename); while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: // The node is an element. if (reader.Name == "Driver") { while (reader.MoveToNextAttribute()) // Read the attributes. { stringvalue = reader.Value; stringname = reader.Name; if (stringname == "position") { richTextBox1.Text += (" " + stringvalue + "\t"); } if (stringname == "name") { string longname = stringvalue; longname = longname.PadRight(30); int namelength = longname.Length; Console.WriteLine(longname + namelength); richTextBox1.Text += (longname); } if (stringname == "laps") { if (stringvalue == " ") stringvalue = "0"; richTextBox1.Text += (" " + stringvalue + "\t\t"); } if (stringname == "seed") { if (stringvalue == " ") stringvalue = "0.000"; richTextBox1.Text += (stringvalue + "\t\t"); } if (stringname == "elapsedTime") { if (stringvalue == " ") stringvalue = "0.000"; richTextBox1.Text += (" " + stringvalue + "\t\t"); } if (stringname == "fastLap") { if (stringvalue == " ") stringvalue = "0.000"; stringvalue = stringvalue.PadRight(8); richTextBox1.Text += (" " + stringvalue + Environment.NewLine); linenumbers++; } } } break; case XmlNodeType.Text: //Display the text in each element. break; case XmlNodeType.EndElement: //Display the end of the element. break; } } reader.Close(); colorTextbox(linenumbers);} 如果我更改功能以便 static void readXML(string Filename) 然后我在richtextbox1上出错。 最简单的修复方法是什么? 先谢谢。 我尝试过: 使被调用的例程变为静态。If I change the function so that it is "static void readXML(string Filename)"then I get errors on richtextbox1.What is the easiest way to fix it?Thanks in advance.What I have tried:making the called routine static.推荐答案private void readXML(string Filename){ int linenumbers = 0; string stringvalue; const string title = "POS PILOT LAPS ELAPSED SEED FAST LAP \n"; StringBuilder builder = new StringBuilder(title, 1024); // 1k is a fair starting "guess" ;-) using (XmlTextReader reader = new XmlTextReader(Filename)) { // The using statement ensures that reader is Closed when done. while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: // The node is an element. if (reader.Name == "Driver") { while (reader.MoveToNextAttribute()) // Read the attributes. { stringvalue = reader.Value; switch (reader.Name) { case "position": builder.Append(" ").Append(stringvalue).Append("\t"); break; case "name": string longname = stringvalue.PadRight(30); Console.WriteLine("{0} {1}", longname, longname.Length); builder.Append(longname); break; case "laps": if (string.IsNullOrWhiteSpace(stringvalue)) stringvalue = "0"; builder.Append(" ").Append(stringvalue).Append("\t\t"); break; case "seed": if (string.IsNullOrWhiteSpace(stringvalue)) stringvalue = "0.000"; builder.Append(stringvalue).Append("\t\t"); break; case "elapsedTime": if (string.IsNullOrWhiteSpace(stringvalue)) stringvalue = "0.000"; builder.Append(" ").Append(stringvalue).Append("\t\t"); break; case "fastLap": if (string.IsNullOrWhiteSpace(stringvalue)) stringvalue = "0.000"; stringvalue = stringvalue.PadRight(8); builder.Append(" ").AppendLine(stringvalue); linenumbers++; break; default: break; } } } break; case XmlNodeType.Text: //Display the text in each element. break; case XmlNodeType.EndElement: //Display the end of the element. break; } } } // Edit: Matt Heffron: 6/17/2016 // Dealing with the Cross-thread access error: // Was: richTextBox1.Text = builder.ToString(); // Change to: Action updateText = () => richTextBox1.Text = builder.ToString(); if (richTextBox1.InvokeRequired) richTextBox1.Invoke(updateText); else updateText(); // End of Edit colorTextbox(linenumbers);} 此外,使用 DataGrid 控件而不是更好地呈现这样的表格数据a RichTextBox 。In addition, tabular data like this would be better presented using a DataGrid control instead of a RichTextBox. 这篇关于对静态感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
06-21 18:33