我仍在学习有关C#的方法,并尝试从XML文件填充ListView。
下面是我的ListView的图片:
当我单击UI上的按钮时,它将在此处使用以下代码(Configuration.cs)读取XML文件:
public static void LoadConfiguration(MainUI UIForm)
{
XDocument doc = XDocument.Load("E:\\InnerSpace" + "\\Scripts\\BJScripts\\MySettings.xml");
MainUI uI = new MainUI();
UIForm.addItemsToActionsListView(doc.Element("UABA").Element("Configure_Tab").Element("Actions_List").Element("ListItem_1").Attribute("Position_X").Value, doc.Element("UABA").Element("Configure_Tab").Element("Actions_List").Element("ListItem_1").Attribute("Position_Y").Value, doc.Element("UABA").Element("Configure_Tab").Element("Actions_List").Element("ListItem_1").Attribute("RGB").Value, doc.Element("UABA").Element("Configure_Tab").Element("Actions_List").Element("ListItem_1").Attribute("Is_Colour").Value, doc.Element("UABA").Element("Configure_Tab").Element("Actions_List").Element("ListItem_1").Attribute("Target").Value, doc.Element("UABA").Element("Configure_Tab").Element("Actions_List").Element("ListItem_1").Attribute("Press_Button").Value);
}
然后它在MainUI.cs中调用一个方法,并将所需的相关信息传递给它。
public partial class MainUI : Form
{
public MainUI()
{
InitializeComponent();
}
private void MainUI_FormClosing(object sender, FormClosingEventArgs e)
{
Program._bMustShutdown = true;
}
public void NewActionsConsoleMessage(string Input)
{
ActionsConsole.Items.Add(DateTime.Now.ToString("h:mm:ss tt") + ": " + Input);
ActionsConsole.SelectedIndex = (ActionsConsole.Items.Count - 1);
}
private void btnAddAction_Click(object sender, EventArgs e)
{
if (txtboxLocationX.Text.Length == 0)
{
NewActionsConsoleMessage("ERROR: Enter a value for Position X and try again.");
MessageBox.Show("Enter a value for Position X and try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (txtboxLocationY.Text.Length == 0)
{
NewActionsConsoleMessage("ERROR: Enter a value for Position Y and try again.");
MessageBox.Show("Enter a value for Position Y and try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (txtboxColourRGB.Text.Length == 0)
{
NewActionsConsoleMessage("ERROR: Enter a value for Pixel RGB and try again.");
MessageBox.Show("Enter a value for Pixel RGB and try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (!radionbtnIsColour.Checked && !radionbtnIsNotColour.Checked)
{
NewActionsConsoleMessage("ERROR: Select either tracking by colour or tracking my not colour and try again.");
MessageBox.Show("Select either tracking by colour or tracking my not colour and try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (cboxTarget.Text.Length == 0)
{
NewActionsConsoleMessage("ERROR: Select a target and try again.");
MessageBox.Show("Select a target and try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (txtboxActionBtn.Text.Length == 0)
{
NewActionsConsoleMessage("ERROR: Enter a value for Action to Take Button Press and try again.");
MessageBox.Show("Enter a value for Action to Take Button Press and try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
ListViewItem addActionsItem = new ListViewItem(txtboxLocationX.Text);
addActionsItem.SubItems.Add(txtboxLocationY.Text);
addActionsItem.SubItems.Add(txtboxColourRGB.Text);
addActionsItem.SubItems.Add(radionbtnIsColour.Checked.ToString());
addActionsItem.SubItems.Add(cboxTarget.Text);
addActionsItem.SubItems.Add(txtboxActionBtn.Text);
lvActionsList.Items.Add(addActionsItem);
}
private void btnSetPixelLocationColour_Click(object sender, EventArgs e)
{
var pixelColourPickerUI = new PixelColourPickerUI();
pixelColourPickerUI.ShowDialog();
txtboxLocationX.Text = pixelColourPickerUI._SelectedPixelMouseLocX;
txtboxLocationY.Text = pixelColourPickerUI._SelectedPixelMouseLocY;
txtboxColourRGB.Text = pixelColourPickerUI._SelectedPixelColor_R + "," + pixelColourPickerUI._SelectedPixelColor_G + "," + pixelColourPickerUI._SelectedPixelColor_B;
pnlConfigurePixelColour.BackColor = Color.FromArgb(pixelColourPickerUI._SelectedPixelColor_A, pixelColourPickerUI._SelectedPixelColor_R, pixelColourPickerUI._SelectedPixelColor_G, pixelColourPickerUI._SelectedPixelColor_B);
}
public ListView.ListViewItemCollection listViewItemCollection
{
get { return lvActionsList.Items; }
}
public void addItemsToActionsListView(string _LocX, string _LocY, string _RGB, string _IsColour, string _Target, string _ButtonPress)
{
Debug.WriteLine("_LocX: " + _LocX + "_LocY" + _LocY + "_RGB" + _RGB + "_IsColour" + _IsColour + "_Target" + _Target + "_ButtonPress" + _ButtonPress);
ListViewItem addActionsItem = new ListViewItem(_LocX);
addActionsItem.SubItems.Add(_LocY);
addActionsItem.SubItems.Add(_RGB);
addActionsItem.SubItems.Add(_IsColour);
addActionsItem.SubItems.Add(_Target);
addActionsItem.SubItems.Add(_ButtonPress);
Debug.WriteLine("Count: " + addActionsItem.SubItems.Count);
lvActionsList.Items.Add(addActionsItem);
}
private void btnSaveActionsList_Click(object sender, EventArgs e)
{
Configuration.Configuration.SaveConfiguration(items: lvActionsList.Items);
}
private void btnLoadProfile_Click(object sender, EventArgs e)
{
Configuration.Configuration.LoadConfiguration(this);
}
}
第一个Debug.WriteLine返回从Configuration.cs正确传递的变量,第二个Debug.WriteLine返回正确的6个子项计数。
但是,当查看我的ListView时,它仍然是空的。以前,我在制作最终成为XML文件信息时可以使用相同的代码(具有不同的变量)添加ListView。尝试从XML加载信息时我做错了什么?您需要查看更多代码吗?
谢谢!
答案 0 :(得分:0)
将MainUI作为参数传递给您的静态方法:
public static void LoadConfiguration(MainUI UiForm)
{
XDocument doc = XDocument.Load("E:\\InnerSpace" + "\\Scripts\\BJScripts\\MySettings.xml");
UiForm.addItemsToActionsListView(doc.Element("UABA" etc
}