从.DLL返回列表项并在ListBox

时间:2016-05-09 19:26:29

标签: c# wpf list dll

我需要创建一个DLL类库,该库从JSON网站获取HTTP个格式化的属性,并将其显示在List中。当我创建一个Test应用程序(可执行文件,而不是DLL)时,它可以正常工作并完成我想要的但是在编写时会出现问题。DLL并尝试从可执行应用程序调用它(这些项目应该显示在WPF ListBox中)。在我的DLL我有以下课程:

public class Definitions
{
    public double id { get; set; }
    public double uid { get; set; }
    public string user { get; set; }
    public string start { get; set; }
    public string end { get; set; }
    public double dur { get; set; }
    public string project { get; set; }

    public override string ToString()
    {
        dur = dur / 100000;
        return string.Format(
            "TimeID: {0}, UserID: {1}, Name: {2}, Project: {3}, Start: {4}, End: {5}, Duration: {6} min", 
            this.id,
            this.uid,
            this.user,
            this.project,
            Convert.ToDateTime(this.start),
            Convert.ToDateTime(this.end),
            this.dur);
    }
}

public class MainList
{
    public List<Definitions> data { get; set; }      
}

我想我不是正确地编写方法:

public static ??returntype?? GetData()
{ 
    //...
    var f = Newtonsoft.Json.JsonConvert.SerializeObject(obj, Newtonsoft.Json.Formatting.Indented);

    var allDefinitions = JsonConvert.DeserializeObject<MainList>(f);

    if (allDefinitions.data.Count == 0)
    {
        break;
    }

    return allDefinitions.data;//??
}

我不知道返回类型应该是什么。当我创建Test应用程序时,我将我的代码放入循环中并且它可以工作:

for (var i = 0; i < allDefinitions.data.Count; i++)
{
    Console.WriteLine(allDefinitions.data[i]);
}

但在这种情况下,它是EXE应用程序,代码在Main方法中。现在它应该是DLL并且应该在ListItems中显示所有这些WPF ListBox。谢谢你的帮助。

编辑: 我认为我遇到了方法本身以及Main方法中的对象实例的问题。该方法必须遍历WEB页面,因为API每个请求只允许50个项目,如果有超过50项,那么我必须转到第2页,依此类推。我遇到了2个问题,第一个是我收到的错误是not all paths return a valuepage++是无法访问的,第二个是我在Main方法List<Definitions> myList = GetData();中说的错误然后我得到Cannot implicitly convert type void into System.Collections.GenericList<Definitions>我发布了整个方法:

public static List<Definitions> GetData()
    { 
        string result = "";


        for (int page = 1; result != null; page++)
        {
            string fpage = "&page=" + page;                            
            string ApiToken = "something";
            string url = " https://someWebSite" + fpage;                          
            string userpass = ApiToken + ":api_token";
            string userpassB64 = Convert.ToBase64String(Encoding.Default.GetBytes(userpass.Trim()));
            string authHeader = "Basic " + userpassB64;

            HttpWebRequest authRequest = (HttpWebRequest)WebRequest.Create(url);
            authRequest.Headers.Add("Authorization", authHeader);
            authRequest.Method = "GET";
            authRequest.ContentType = "application/json";
            var response = (HttpWebResponse)authRequest.GetResponse();

            Stream stream = response.GetResponseStream();

                StreamReader sr = new StreamReader(stream);
                result = sr.ReadToEnd();

                var obj = Newtonsoft.Json.JsonConvert.DeserializeObject(result);
                var f = Newtonsoft.Json.JsonConvert.SerializeObject(obj, Newtonsoft.Json.Formatting.Indented);

               var allDefinitions = JsonConvert.DeserializeObject<MainList>(f);

                if (allDefinitions.data.Count == 0)
                {

                    break;
                }


                return allDefinitions.data;

            }

2 个答案:

答案 0 :(得分:0)

您可以返回'allDefinitions.data;'就像你期望的那样,然后在WPF应用程序上执行与你收到的数据相同的循环,并将它们添加到一个ObservableCollection,它绑定到listview的'itemsource'。

即。从DLL收到数据后,在WPF应用程序上

 for (var i = 0; i < allDefinitions.data.Count; i++)
            {

                myListItems.Add(allDefinitions.data[i]);

            }

请注意,myListItems是一个ObservableCollection,它在其setter上实现对INotifyPropertyChanged的加注。

答案 1 :(得分:0)

从函数GetData()返回alldefinitions.data并返回类型应为List<Definitions>

public static List<Definitions> GetData()
{ 
    var f = Newtonsoft.Json.JsonConvert.SerializeObject(obj, Newtonsoft.Json.Formatting.Indented);
    var allDefinitions = JsonConvert.DeserializeObject<MainList>(f);

    return allDefinitions.data;
}

然后在您的WPF应用程序中,您只需迭代列表并将项目添加到列表框

 List<Definitions> myList = GetData();
 for (var i = 0; i < myList.Count; i++)
 {
      ListBoxItem newItem = new ListBoxItem();
      newItem.Content = myList[i];
      YourListBox.Items.Add(newItem);
 }

修改  从您的评论看起来,您试图获得5个页面的50个定义。

public static List<Definitions> GetData()
{ 
    string result = "";
    List<Definitions> allDefinitions =  new List<Definitions>();
    for (int page = 1; page <= 5; page++)
    {
      for( int i = 0;i < 50;i++)
      {
        string fpage = "&page=" + page;                            
        string ApiToken = "something";
        string url = " https://someWebSite" + fpage;                          
        string userpass = ApiToken + ":api_token";
        string userpassB64 = Convert.ToBase64String(Encoding.Default.GetBytes(userpass.Trim()));
        string authHeader = "Basic " + userpassB64;

        HttpWebRequest authRequest = (HttpWebRequest)WebRequest.Create(url);
        authRequest.Headers.Add("Authorization", authHeader);
        authRequest.Method = "GET";
        authRequest.ContentType = "application/json";
        var response = (HttpWebResponse)authRequest.GetResponse();

        Stream stream = response.GetResponseStream();

        StreamReader sr = new StreamReader(stream);
        result = sr.ReadToEnd();

        var obj = Newtonsoft.Json.JsonConvert.DeserializeObject(result);
        var f = Newtonsoft.Json.JsonConvert.SerializeObject(obj, Newtonsoft.Json.Formatting.Indented);

        var definition = JsonConvert.DeserializeObject<Definitions>(f);
        allDefinitions.Add(definition);
      }
    }
    return allDefinitions;
 }