我正在尝试将此TwitchTv API XML代码集成到我自己的项目中,但当我尝试在getStreamInfo
中调用button1_Click event
时,我无法使其工作。
代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Text;
using System.Xml.Linq;
namespace jsontest //change this if you want
{
public class twitchTVAPIModel
{
public string Title { get; set; }
public string ChannelOwner { get; set; }
public string ChannelUrl { get; set; }
public int ViewerCount { get; set; }
public string Status { get; set; }
public string Username { get; set; }
public string GameTitle { get; set; }
}
public class twitch
{
public List<twitchTVAPIModel> getStreamInfo(string channel)
{
string uriEndPoint = "http://api.justin.tv/api/stream/list.xml?channel=" + channel;
var xmlDocument = XDocument.Load(uriEndPoint);
var streams = (from stream in xmlDocument.Descendants("stream")
select new twitchTVAPIModel()
{
Title = stream.Element("title").Value,
ViewerCount = Convert.ToInt32(stream.Element("stream_count").Value),
Status = stream.Element("status").Value,
Username = stream.Element("channel").Element("login").Value,
ChannelUrl = stream.Element("channel").Element("channel_url").Value,
GameTitle = stream.Element("channel").Element("meta_game").Value
}).ToList<twitchTVAPIModel>();
return streams;
}
}
}
该行:
public List<twitchTVAPIModel> getStreamInfo(string channel)
我不清楚,这也是我不理解如何正确调用它的一个原因。
答案 0 :(得分:3)
该行
public List<twitchTVAPIModel> getStreamInfo(string channel)
方法标题是声明函数getStreamInfo
接受string
作为唯一参数,并将返回List
个twitchTVAPIModel
个对象
编辑:由于该函数不是静态的,因此在调用此函数之前,您需要实例化Twitch
对象
var twitch = new Twitch();
var results = twitch.getStreamInfo("someChannel");