0

ListView在新WPF窗口中创建了一个,并且还创建了一个在调用时填充 ListView 的函数。该函数只获取存储数据的 Web 服务器的 URL,递增“id”并获取数据并将其存储在 ListView 中。因此,它用一定数量的项目填充 ListView。

我面临的问题是我想为每个 ListView 项目添加两个按钮,打开和关闭,因为它以编程方式填充。即,如果添加 16 个项目,我希望每个项目有 2 个按钮,如果是 12 个项目,则类似的过程。这是我的代码:

namespace user_login
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>

    public partial class MainWindow : Window
    {
        Window1 W = new Window1();

        public MainWindow()
        {
            InitializeComponent();    
        }

        public void populate()
        {
            int i;
            int num = 16;

            for (i = 1; i <= num; i++)
            {
                string val = Convert.ToString(i);
                string currentUrl = "http://xpleria.com/devices.php?query=dev&id=";
                string newUrlWithChangedSort = ReplaceQueryStringParam(currentUrl, "id", val);
                string result = getcontent(newUrlWithChangedSort);    
                W.list1.Items.Add(result);
            }
        }

        public string getcontent(string URL)
        {
            string content = "";

            // Get HTML data
            WebClient client = new WebClient();

            try
            {
                content = client.DownloadString(URL);
            }
            catch (Exception)
            {
                System.Windows.Forms.MessageBox.Show("No Connection detected!!!");
            }
            return content;
        }

        public static string ReplaceQueryStringParam(string currentPageUrl, string paramToReplace, string newValue)
        {
            string urlWithoutQuery = currentPageUrl.IndexOf('?') >= 0
                ? currentPageUrl.Substring(0, currentPageUrl.IndexOf('?'))
                : currentPageUrl;

            string queryString = currentPageUrl.IndexOf('?') >= 0
                ? currentPageUrl.Substring(currentPageUrl.IndexOf('?'))
                : null;

            var queryParamList = queryString != null
                ? HttpUtility.ParseQueryString(queryString)
                : HttpUtility.ParseQueryString(string.Empty);

            if (queryParamList[paramToReplace] != null)
            {
                queryParamList[paramToReplace] = newValue;
            }
            else
            {
                queryParamList.Add(paramToReplace, newValue);
            }

            return String.Format("{0}?{1}", urlWithoutQuery, queryParamList);
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            string user = textbox1.Text;
            string password = textbox2.Password;
            string currentUrl = "http://xpleria.com/login.php?query=login&user=wcam&pass=wireless";
            string newUrlWithChangedSort = ReplaceQueryStringParam(currentUrl, "user", user);
            string newUrl = newUrlWithChangedSort;
            string FinalUrl = ReplaceQueryStringParam(newUrl, "pass", password);
            string result= getcontent(FinalUrl);
            string value = result.Substring(0, 8);

            string invalid = "xpleria0";
            string valid = "xpleria1";

            if (value.Equals(invalid))
            {
                System.Windows.MessageBox.Show("The Username and/or Password you have entered is invalid, please try again");
            }
            else if (value.Equals(valid))
            {
                string sessionID = result.Substring(8, 32);
                System.Windows.MessageBox.Show("HI, WELCOME CLETA");
                this.Close();

                using (new user_login.loading.PleaseWait(this.Location))
                {
                    W.Show();
                    populate();
                }
            }
        }

        public System.Drawing.Point Location { get; set; }
    }
}
4

1 回答 1

1

I'm going to recommend you take a step back and start giving some serious consideration to organizing your code. I realize this isn't an answer to the question you asked but it is the answer to the question you should be asking.

First of all, all code relating to the retrieval of these items from the URL should be moved into a class of some kind. This class should accept the URL string as a constructor parameter and gather all the appropriate data. You should then create another class which you will use to populate with the data for each individual item and then expose this list. By the time you're done the code in your window should little more complex than:

var ItemsGetter = new ItemsGetter(URL);
foreach(var Item in ItemsGetter.Items)
{
    // Populate the ListView
}

Once you're done with that I recommend you create a UserControl. User controls are extremely useful in situations where you need to represent a dynamic number of data entities each with their own set of controls which allow operations to be performed on each one. You should create a UserControl with a label and the two buttons you need. The UserControl's constructor should expect a parameter of the data type you created to represent each one of your classes. From there you can have the buttons operate on the data type as necessary.

Finally, you'll probably need a way to have the UserControl interact with the Window. Say for example one of your buttons is "Delete". You'd probably want the item to disappear from the list once the operation is complete. Don't be tempted to tie in your control with the Window by passing it as a parameter or something. Instead, read up on Action events and learn how you can create an event on the user control which you bind in the foreach loop of the Window when you're populating the list view. When the UserControl has completed the delete operation triggered by the button you can raise the UserControl's event which will prompt the Window to remove the control from the List View.

Last but not least, NAME YOUR CONTROLS.

Hopefully this helps.

于 2013-05-15T04:37:57.807 回答