首页 > 代码库 > Silverlight 独立存储(IsolatedStorageFile)
Silverlight 独立存储(IsolatedStorageFile)
1.在Web中添加天气服务引用地址
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl
2.在Web中添加Wcf服务接口IWeatherService.cs
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Runtime.Serialization; 5 using System.ServiceModel; 6 using System.Text; 7 8 namespace EasySL.Web.WCF 9 {10 // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IWeatherService”。11 [ServiceContract]12 public interface IWeatherService13 {14 [OperationContract]15 string[] GetCityWeather(string CityName);16 }17 }
3.在Web中实现服务接口
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Runtime.Serialization; 5 using System.ServiceModel; 6 using System.Text; 7 8 namespace EasySL.Web.WCF 9 {10 // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“WeatherService”。11 // 注意: 为了启动 WCF 测试客户端以测试此服务,请在解决方案资源管理器中选择 WeatherService.svc 或 WeatherService.svc.cs,然后开始调试。12 public class WeatherService : IWeatherService13 {14 public string[] GetCityWeather(string CityName)15 {16 ServiceRefWeather.WeatherWebServiceSoapClient client = new ServiceRefWeather.WeatherWebServiceSoapClient("WeatherWebServiceSoap");17 string[] cityNameWeather = client.getWeatherbyCityName(CityName);18 return cityNameWeather;19 }20 }21 }
4.在client添加自己创建的Wcf服务地址、调用服务接口
/// <summary> /// 通过wcf获取天气数据信息 /// </summary> private void InitDataWeather() { try { ServiceReference1.WeatherServiceClient client = new ServiceReference1.WeatherServiceClient(); client.GetCityWeatherCompleted += (s, e) => { try { if (e.Error == null) { string[] cityWeather = new string[e.Result.Count]; 讲e.Result结果显示在页面中就ok了,然后我们将e.Result进行数据格式处理,运用独立存储讲结果保存起来 } else { lbltitle1.Content = e.Error.Message; } } catch (Exception ex) { lbltitle1.Content = ex.Message; } }; client.GetCityWeatherAsync("北京"); } catch (Exception ex) { //lbltitle1.Content = ex.Message; } }
5.将数据结果存放到本地(运用独立存储技术)
1 private void WeatherWriterIsolatedStorageFile(string str) 2 { 3 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication(); 4 if (isf.FileExists("Weather.txt")) 5 { 6 isf.DeleteFile("Weather.txt"); 7 } 8 using (Stream stream = isf.CreateFile("Weather.txt")) 9 {10 using (StreamWriter writer = new StreamWriter(stream))11 {12 writer.WriteLine(str);13 }14 }15 }
6.读取存放的数据结果(独立存储)
private void ReaderWeatherIsolatedStorageFile() { IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication(); if (isf.FileExists("AppConfig/Weather.txt")) { using (Stream stream = isf.OpenFile("Weather.txt", FileMode.Open)) { using (StreamReader reader = new StreamReader(stream)) { string[] line = reader.ReadToEnd().Split(‘|‘); } } isf.DeleteFile("Weather.txt"); } else { InitDataWeather(); } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。