简体   繁体   English

c#从XML文件填充ListView

[英]c# Populating a ListView from an XML File

I am still learning my way around c# and am trying to populate my ListView from an XML file. 我仍在学习有关C#的方法,并尝试从XML文件填充ListView。

Below is a picture of my ListView: 下面是我的ListView的图片:

ListView 列表显示

When I click a button on my UI, it reads the XML file using this code (Configuration.cs) here: 当我单击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);
}

Then it calls a method in my MainUI.cs and passes the relevant information I need to it. 然后,它在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);
    }
}

The first Debug.WriteLine returns the proper passed variables from Configuration.cs and the second Debug.WriteLine returns the proper count of 6 SubItems. 第一个Debug.WriteLine返回从Configuration.cs正确传递的变量,第二个Debug.WriteLine返回正确的6个子项计数。

However, when viewing my ListView, it is still empty. 但是,当查看我的ListView时,它仍然是空的。 Previously, I was able to add the the ListView using identical code (with different variables) when I was making what eventually became the XML file information. 以前,我在制作最终成为XML文件信息时可以使用相同的代码(具有不同的变量)添加ListView。 What am I doing wrong when trying to load the information from the XML? 尝试从XML加载信息时我做错了什么? Do you need to see more code? 您需要查看更多代码吗?

Thanks in advance! 提前致谢!

Pass in the MainUI as argument to your static method: 将MainUI作为参数传递给您的静态方法:

public static void LoadConfiguration(MainUI UiForm)
{
    XDocument doc = XDocument.Load("E:\\InnerSpace" + "\\Scripts\\BJScripts\\MySettings.xml");

    UiForm.addItemsToActionsListView(doc.Element("UABA" etc
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM