我对C#,silverlight和整个数据绑定范例都比较陌生。我一直在研究一个小测试应用程序,它使用Json.Net通过API从reddit获取数据。无论如何,我将数据输入到我的应用程序中就好了,但现在我无法将数据推送到用户界面。我尝试了几种不同的配置无济于事。无论如何,代码是在这里:DataBinding反序列化的json数组
public partial class MainPage : PhoneApplicationPage
{
string json = "";
RootObject topic { get; set; }
public MainPage()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
textBlock1.Text = "Retrieving...";
string url = @"http://www.reddit.com/r/all.json";
HttpWebRequest hWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
hWebRequest.Method = "GET";
hWebRequest.BeginGetResponse(Response_Completed, hWebRequest);
}
public void Response_Completed(IAsyncResult result)
{
HttpWebRequest request = (HttpWebRequest)result.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result);
using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
{
json = streamReader.ReadToEnd();
topic = JsonConvert.DeserializeObject<RootObject>(json);
}
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
this.DataContext = topic.data.children[0].data.title;
textBlock1.Text = "Done.";
});
}
这是我的代码的主要部分。其余的类都在这里,它们用于反编译API提供的JSON的反序列化。
public class MediaEmbed
{
public string content { get; set; }
public int? width { get; set; }
public bool? scrolling { get; set; }
public int? height { get; set; }
}
public class Oembed
{
public string provider_url { get; set; }
public string description { get; set; }
public string title { get; set; }
public string url { get; set; }
public string author_name { get; set; }
public int height { get; set; }
public int width { get; set; }
public string html { get; set; }
public int thumbnail_width { get; set; }
public string version { get; set; }
public string provider_name { get; set; }
public string thumbnail_url { get; set; }
public string type { get; set; }
public int thumbnail_height { get; set; }
public string author_url { get; set; }
}
public class Media
{
public string type { get; set; }
public Oembed oembed { get; set; }
}
public class Data2
{
public string domain { get; set; }
public MediaEmbed media_embed { get; set; }
public object levenshtein { get; set; }
public string subreddit { get; set; }
public string selftext_html { get; set; }
public string selftext { get; set; }
public object likes { get; set; }
public bool saved { get; set; }
public string id { get; set; }
public bool clicked { get; set; }
public string title { get; set; }
public Media media { get; set; }
public int score { get; set; }
public bool over_18 { get; set; }
public bool hidden { get; set; }
public string thumbnail { get; set; }
public string subreddit_id { get; set; }
public string author_flair_css_class { get; set; }
public int downs { get; set; }
public bool is_self { get; set; }
public string permalink { get; set; }
public string name { get; set; }
public double created { get; set; }
public string url { get; set; }
public string author_flair_text { get; set; }
public string author { get; set; }
public double created_utc { get; set; }
public int num_comments { get; set; }
public int ups { get; set; }
}
public class Child
{
public string kind { get; set; }
public Data2 data { get; set; }
}
public class Data
{
public string modhash { get; set; }
public Child[] children { get; set; }
public string after { get; set; }
public object before { get; set; }
}
public class RootObject
{
public string kind { get; set; }
public Data data { get; set; }
}
}
假设的XAML UI的东西看起来像这样
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock x:Name="TitleInfo" />
<TextBlock x:Name="AuthorInfo" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
的标题包含RootObject实例的内部调用话题。因此,要抢冠军的方式是
topic.data.children[0].data.title;
不过,我几乎没有任何想法我怎么能结合,对这些文本框或listbox..I知道一个DataContext必须设置这些项目可以绑定通过代码,而不是XAML,但我无法找出任何优雅的方式来做到这一点。任何帮助?万分感谢。
这是非常正是我一直在寻找。我试图定义数据上下文的方式并不意味着在这里发布,它更像是在黑暗中刺中,我知道可能不会工作,并忘记删除。无论如何,我研究过寻找这些类型的例子的数据绑定,但什么都没发现。它完美的作品。谢谢。 – Wilcoholic 2011-12-20 09:23:51