C# 从函数向Listview添加项

C# 从函数向Listview添加项,c#,listview,listviewitem,C#,Listview,Listviewitem,我正在尝试在listView1中添加一行。如何在另一个函数中执行此操作 我在看《邮报》。他们想让我直接把它添加到一个按钮函数中。我不想那样做 private void按钮1\u单击(对象发送者,事件参数e) { 关键词(); } 公共静态void关键字() { 字符串country=“”; string key=“1070”; //转到GetHtmlAsync GetHtmlAsync(键,国家); } 公共静态异步void GetHtmlAsync(字符串键,字符串国家/地区) { //Get

我正在尝试在listView1中添加一行。如何在另一个函数中执行此操作

我在看《邮报》。他们想让我直接把它添加到一个按钮函数中。我不想那样做

private void按钮1\u单击(对象发送者,事件参数e)
{
关键词();
}
公共静态void关键字()
{
字符串country=“”;
string key=“1070”;
//转到GetHtmlAsync
GetHtmlAsync(键,国家);
}
公共静态异步void GetHtmlAsync(字符串键,字符串国家/地区)
{
//GetHtmlAsync
变量url=”https://www.test.com/search?county=“+country+”&q=“+key;
var httpClient=新的httpClient();
var html=await httpClient.GetStringAsync(url);
var htmlDocument=新的htmlDocument();
htmlDocument.LoadHtml(html);
//这是从HtmlDocument(列表)中获取的
var id=“58756”;
var seller=“测试”;
var product=“GTX 1070”;
var betTime=“10:10”;
var price=“100”;
var shipping=“4”;
string[]行={id,卖家,产品,betTime,价格+发货,url};
var listViewItem=新listViewItem(行);
listView1.Items.Add(listViewItem);
}
我希望它在listView1中添加一行
listView1.Items.add(listViewItem),但我收到一条错误消息

非静态字段、方法或属性“Form1.listView1”需要对象引用


因此,根据您的要求,尝试一下这个

private void按钮1\u单击(对象发送者,事件参数e)
{
关键词();
}
//非静态,以便访问ListView1
公共void关键字()
{
字符串country=“”;
string key=“1070”;
//转到GetHtmlAsync
GetHtmlAsync(键,国家);
}
//非静态
公共异步void GetHtmlAsync(字符串键,字符串国家/地区)
{
//GetHtmlAsync
变量url=”https://www.test.com/search?county=“+country+”&q=“+key;
var httpClient=新的httpClient();
var html=await httpClient.GetStringAsync(url);
var htmlDocument=新的htmlDocument();
htmlDocument.LoadHtml(html);
//这是从HtmlDocument(列表)中获取的
var id=“58756”;
var seller=“测试”;
var product=“GTX 1070”;
var betTime=“10:10”;
var price=“100”;
var shipping=“4”;
string[]行={id,卖家,产品,betTime,价格+发货,url};
var listViewItem=新listViewItem(行);
listView1.Items.Add(listViewItem);
}

因此,根据您的要求,尝试此方法

private void按钮1\u单击(对象发送者,事件参数e)
{
关键词();
}
//非静态,以便访问ListView1
公共void关键字()
{
字符串country=“”;
string key=“1070”;
//转到GetHtmlAsync
GetHtmlAsync(键,国家);
}
//非静态
公共异步void GetHtmlAsync(字符串键,字符串国家/地区)
{
//GetHtmlAsync
变量url=”https://www.test.com/search?county=“+country+”&q=“+key;
var httpClient=新的httpClient();
var html=await httpClient.GetStringAsync(url);
var htmlDocument=新的htmlDocument();
htmlDocument.LoadHtml(html);
//这是从HtmlDocument(列表)中获取的
var id=“58756”;
var seller=“测试”;
var product=“GTX 1070”;
var betTime=“10:10”;
var price=“100”;
var shipping=“4”;
string[]行={id,卖家,产品,betTime,价格+发货,url};
var listViewItem=新listViewItem(行);
listView1.Items.Add(listViewItem);
}

public static async
中删除
static
,或者删除您不需要的
static async
。。。你不会等待任何东西……我想他会添加一些与async相关的东西(正如函数名所说)@chođěxěŕThis This
string[]row={id,seller,product,betTime,price+shipping,link}
可能来自http客户端,您遇到的问题应该得到解决,但是您提到您还有另一个问题,
但是我又遇到了另一个错误,因为我需要函数中其他内容的static
。如果是这种情况,请更新您的帖子,以便我们能够更好地帮助您。从
公共静态异步
中删除
静态
,或者删除您不需要的
静态异步
。。。你不会等待任何东西……我想他会添加一些与async相关的东西(正如函数名所说)@chođěxěŕThis This
string[]row={id,seller,product,betTime,price+shipping,link}
可能来自http客户端,您遇到的问题应该得到解决,但是您提到您还有另一个问题,
但是我又遇到了另一个错误,因为我需要函数中其他内容的static
。如果是这种情况,请更新您的帖子,以便我们能够更好地帮助您。