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 IWeatherService
13     {
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 : IWeatherService
13     {
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();
            }
        }

Silverlight 独立存储(IsolatedStorageFile)

时间: 2024-12-13 23:45:21

Silverlight 独立存储(IsolatedStorageFile)的相关文章

Silverlight独立存储

写 1 private void Button_Click_1(object sender, RoutedEventArgs e) 2 { 3 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication(); 4 using (Stream stream=isf.CreateFile("Weather.txt")) 5 { 6 using (StreamWriter writer=new Stream

WP8独立存储 总结

1.设置与属性的存储2.本地文件的存储 </pre><pre name="code" class="csharp">//独立存储 键值对IsolatedStorageSettings(独立本地设置): 命名空间为:System.IO.IsolatedStorage.IsolatedStorageSettings,提供了一系列的API,用来在独立存储中存储和操作键/值对.一般使用该方式来存储App设置和用户特定设置. IsolatedStora

WP8 独立存储 总结2

wp7.1APIs Isolated Storage Classes •独立存储类在System.IO.IsolatedStorage命名空间中•IsolatedStorageFile•表示包含文件和目录的独立存储区•IsolatedFileStream•公开独立存储中的文件 Saving Data代码 private void saveGameToIsolatedStorage(string message) { using(IsolatedStorageFile isf = Isolated

WP8 独立存储 总结3(应用设置)

•可在独立存储中使用ApplicationSettings对象•在独立存储中存储键/值对的Dictionary方式存储 •存储的对象将永久保存 在应用设置中保存数据 1 void saveString(string message, string name) 2 { 3 IsolatedStorageSettings.ApplicationSettings[name] = message; 4 IsolatedStorageSettings.ApplicationSettings.Save();

WP开发图片保存到独立存储并从独立存储中读取

需要添加引用命名空间 using System.IO; using System.IO.IsolatedStorage; 1.将图片保存到独立存储空间 1 using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()) 2 3 { 4 5 if (myIsolatedStorage.FileExists(App.fileName)) 6 7 { 8 9 myIsola

Windows Phone 7 中拷贝文件到独立存储

private void CopyToIsolatedStorage(){    using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())    {        string[] files = new string[] { "下雨天.mp3", "用心听.mp3", "我们的歌.mp3" }; foreach (var _fil

解决Silverlight F5刷新问题

最近在做一个SL的项目,做完后,遇到一个F5刷新的问题,本人也是第一次接触接触SL项目,记得再ASP.NET浏览器的缓存会自动保存最后一次的浏览记录的. 所以就在网上到处找资料,可惜运气不好,都没找到合适的资料.基本的解决方法都是通过再HTML页面增加JS方法,屏蔽F5刷新按钮的功能,但是这 样的需求并不是我们项目中所要的,还好在BAIDU和群里高手的帮助下,终于大体了解了SL刷新的过程和解决F5刷新返回最后次浏览页面的思想.. 1:SL刷新过程 SL本身就是HTML页面的一个插件程序,在浏览器

Prism 4 文档 ---第10章 Silverlight和WPF之间共享代码

本主题帮助你理解来自Prism的多目标和它的优缺点.多目标的代码针对两个不同的平台有大致相同的代码库.这允许同时保持代码尽可能多一样能够产生针对两种不同技术的二进制文件.在这种情况下,本节介绍的技术是WPF和Silverlight.本主题包含了一些你在使用这些技术开发多目标应用程序的时候的注意事项. 目标和有点 当在编写具有相似功能和能力的WPF和Silverlight应用程序的时候,努力使用一个代码库很有意义.尽管WPF和Silverlight平台非常相似,但他们只有有限的二进制兼容性.仅Si

[深入浅出WP8.1(Runtime)]Windows Phone 8.1和Silverlight 8.1的区别

1.2.2 Windows Phone 8.1应用程序模型 Windows Phone 8.1支持多种开发语言来开发应用程序,包括C#.VB.JavaScript和C++,那么本书的代码主要是采用C#语言来开发,部分章节采用C++.从Windows  Phone 8.1开始,如果是开发普通的应用程序可以选择的应用程序模型有:C#/XAML.VB/XAML.C++/XAML和JavaScript /HTML5.游戏开发还是采用C++的DirectX的框架.在Windows Phone 8之前如果是