关于图片和文字的显示问题

被现实掩埋的秘密 2014-08-12 02:23:41
首先,可以看看这个网络请求的地址:
http://m.weather.com.cn/data/101020100.html
这个地址是中央台的天气预报有关于上海地区的天气信息,返回的是JSON的数据:
{"weatherinfo":{"city":"上海","city_en":"shanghai","date_y":"2014年3月4日","date":"","week":"星期二","fchh":"11","cityid":"101020100"weather1":"小雨转多云","weather2":"多云转阴","weather3":"小雨","weather4":"小雨","weather5":"小雨转阴","weather6":"多云","img1":"7","img2":"1","img3":"1","img4":"2","img5":"7","img6":"99","img7":"7","img8":"99","img9":"7","img10":"2","img11":"1","img12":"99","img_single":"7","img_title1":"小雨","img_title2":"多云","img_title3":"多云","img_title4":"阴","img_title5":"小雨","img_title6":"小雨","img_title7":"小雨","img_title8":"小雨","img_title9":"小雨","img_title10":"阴","img_title11":"多云","img_title12":"多云","img_title_single":"小雨","wind1":"东北风3-4级转北风4-5级","wind2":"北风4-5级转东北风3-4级","wind3":"东风4-5级","wind4":"东风转东北风4-5级","wind5":"东北风3-4级","wind6":"东北风转东风3-4级","fx1":"东北风","fx2":"北风","fl1":"3-4级转4-5级","fl2":"4-5级转3-4级","fl3":"4-5级","fl4":"4-5级","fl5":"3-4级","fl6":"3-4级","index":"较冷","index_d":"建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。","index48":"较冷","index48_d":"建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。","index_uv":"最弱","index48_uv":"最弱","index_xc":"不宜","index_tr":"适宜","index_co":"较舒适","st1":"10","st2":"3","st3":"9","st4":"2","st5":"7","st6":"2","index_cl":"不宜","index_ls":"不宜","index_ag":"易发"}}

对于这部分数据我用了以下几个方法解析和在手机上显示:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Windows.Media.Imaging;

namespace Weather
{
public partial class MainPage : PhoneApplicationPage
{
WeatherInfo weather = null;
string[] weekMsg = { "星期一","星期二","星期三","星期四","星期五","星期六","星期日"};
// 构造函数
public MainPage()
{
InitializeComponent();
}

private void PageLoad(object sender, RoutedEventArgs e)
{
WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wb_DownloadStringCompleted);
client.DownloadStringAsync(new Uri("http://m.weather.com.cn/data/101020100.html",UriKind.Absolute));

}
//下载完成后的处理事件
void wb_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) {

//判断现在是否成功
if(e.Result.Length<=0||e.Error!=null||e.Cancelled){
MessageBox.Show("获取天气信息失败!");
return;
}
//创建解析对象
JObject json = JObject.Parse(e.Result);//解析网络请求的结果
weather = new WeatherInfo()
{
city = (string)json["weatherinfo"]["city"],
cityid = (string)json["weatherinfo"]["cityid"],
date_y = (string)json["weatherinfo"]["date_y"],
wind = (string)json["weatherinfo"]["wind1"],
info = (string)json["weatherinfo"]["info"],
temp1 = (string)json["weatherinfo"]["temp1"],
temp2 = (string)json["weatherinfo"]["temp2"],
temp3 = (string)json["weatherinfo"]["temp3"],
temp4 = (string)json["weatherinfo"]["temp4"],
temp5 = (string)json["weatherinfo"]["temp5"],
temp6 = (string)json["weatherinfo"]["temp6"],
weather1 = (string)json["weatherinfo"]["weather1"],
weather2 = (string)json["weatherinfo"]["weather2"],
weather3 = (string)json["weatherinfo"]["weather3"],
weather4 = (string)json["weatherinfo"]["weather4"],
weather5 = (string)json["weatherinfo"]["weather5"],
weather6 = (string)json["weatherinfo"]["weather6"],
week = (string)json["weatherinfo"]["week"],
};
UpdateUi();
}
//跟新UI信息
void UpdateUi()
{
day1.Temp = weather.weather2;
day2.Temp = weather.weather3;
day3.Temp = weather.weather4;
day4.Temp = weather.weather6;
today_date.Text = weather.date_y;
today_temperature.Text = weather.temp1;
today_week.Text = weather.week;
today_describle.Text = weather.info;
this.cityname.Text = weather.city;
int i;
for (i = 0; i < 7; i++)
{
if (weekMsg[i] == weather.week)
break;
}
day1.Weekday = weekMsg[(i + 1) % 7];
day2.Weekday = weekMsg[(i + 2) % 7];
day3.Weekday = weekMsg[(i + 3) % 7];
day4.Weekday = weekMsg[(i + 4) % 7];
day1.ImageUri = GetImgUri(weather.temp2);
day2.ImageUri = GetImgUri(weather.temp3);
day3.ImageUri = GetImgUri(weather.temp4);
day4.ImageUri = GetImgUri(weather.temp5);
today_image.Source = new BitmapImage(new Uri(GetImgUri(weather.temp1),UriKind.Relative));
}
//返回天气图片的Uri
String GetImgUri(string weather)
{
string uri = "/Weather;component/Images/";
if (weather == "晴")
{
return uri + "sunday.jpg";
}
else if (weather == "阴")
{
return uri + "overcast.jpg";
}
else if (weather == "雷阵雨")
{
return uri + "ThunderShower.jpg";
}
else if (weather.Contains("多云"))
{
return uri + "cloudy.jpg";
}
else if (weather.Contains("雨"))
{
return uri + "Rain.jpg";
}
else
{
return uri + "cloudy.jpg";
}
}
}
}

但是发生了很奇怪的事情,所有的图片,和文字过多的部分没有显示出来,这是运行后的结果:


这里边我要解释一下:json数据不是完整的数据,里边有些字符不允许发出来,我删掉了,如要看完整的可以把上边地址复制到浏览器。我曾经怀疑过是否是因为获得的天气信息如:小雨等,不在我的判断内,所以没有显示,为此,我在里边特地该了一个temp6这个是个多云,但是依然没有显示,而且解析部分的info 也就是json中的index_d这个键值也没有能够显示出来,不知道是什么原因?
求教了?
...全文
187 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 1 楼 vbfool 的回复:
GetImgUri这个方法,你断点一下看有没有问题,我觉得有可能有什么不对。
找到了问题所在,就是这个Uri,用这种写法的话,图片生产操作属性要改为Resorce;
  • 打赏
  • 举报
回复
引用 1 楼 vbfool 的回复:
GetImgUri这个方法,你断点一下看有没有问题,我觉得有可能有什么不对。
你说的问题我也想过,可能出现问题的就是有可能,JSON数据里边没有那几个汉字,可能是别的,为此,我还修改了部分数据,但是依然没有显示。 再者,如果是Uri的问题的话,uri = "/Weather;component/Images/",这个路径可能也会存在问题,Weather是名空间,Images是保存图片的文件夹,我想问问,在cs中引用图片的话这么写应该是没有问题的吧。
vbfool 2014-08-13
  • 打赏
  • 举报
回复
GetImgUri这个方法,你断点一下看有没有问题,我觉得有可能有什么不对。
  • 打赏
  • 举报
回复
引用 4 楼 BEYONDMA 的回复:
恭喜,问题解决,那就结贴吧.
你就是关心这个问题是吧!
beyondma 2014-08-13
  • 打赏
  • 举报
回复
恭喜,问题解决,那就结贴吧.

7,655

社区成员

发帖
与我相关
我的任务
社区描述
Windows Phone是微软发布的一款手机操作系统,它将微软旗下的Xbox LIVE游戏、Zune音乐与独特的视频体验整合至手机中。
社区管理员
  • Windows客户端开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧