我在一个新的ListView
窗口中创建了一个WPF
,还有一个在调用时填充ListView的函数。这个函数只需要我存储数据的Web服务器的URL,递增“id”并获取数据并将其存储在ListView中。因此,它使用一定数量的项填充ListView。
我面临的问题是我想添加两个按钮,ON&关闭,以编程方式填充每个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; }
}
}
答案 0 :(得分:1)
我建议你退后一步,开始认真考虑组织你的代码。我知道这不是你问的问题的答案,但 是你应该问的问题的答案。
首先,所有与从URL中检索这些项目相关的代码都应该移动到某种类中。此类应接受URL字符串作为构造函数参数并收集所有适当的数据。然后,您应该创建另一个类,您将使用该类填充每个项目的数据,然后公开此列表。当你完成时,窗口中的代码应该比以下更复杂:
var ItemsGetter = new ItemsGetter(URL);
foreach(var Item in ItemsGetter.Items)
{
// Populate the ListView
}
完成后,我建议您创建一个UserControl。在需要表示动态数量的数据实体的情况下,用户控件非常有用,每个数据实体都有自己的控件集,允许在每个控件上执行操作。您应该创建一个带有标签的UserControl和您需要的两个按钮。 UserControl的构造函数应该期望您创建的数据类型的参数代表您的每个类。从那里,您可以根据需要使用按钮操作数据类型。
最后,您可能需要一种让UserControl与Window交互的方法。比如说你的一个按钮是“删除”。操作完成后,您可能希望项目从列表中消失。不要试图通过将其作为参数或其他东西传递给Window控制。相反,请阅读Action
事件并了解如何在填充列表视图时在窗口的foreach循环中绑定的用户控件上创建事件。当UserControl完成按钮触发的删除操作时,您可以引发UserControl的事件,该事件将提示Window从列表视图中删除控件。
最后但并非最不重要的是, NAME YOUR CONTROLS 。
希望这有帮助。