silverlight+wcf+linq to sql项目实战

本文以实战一个基于silverlgiht,wcf,linq to sql的网站广告轮播器,可能并没有太大实际意义,不过相信可以体验出新一代技术的特性.我并且假定你已经有一定对这些技术的基础认识.

 

目标:
1.简单的网站嵌入单件silverlight.(嵌入到网站)
1.1.图片轮播.视频轮播.
2.网络传输使用wcf服务,使用(basicHttpBinding)
2.1.支持使用基于流的上传与下载资源(图片,视频)
3.使用sql server 2005作为数据存储.
3.1.使用linq to sql进行数据库操作.

 

A.系统规划:设想中的系统框架

 

 

 

 

 

 

 

 

 

 

 

 

 

 

B.需求分析:使用用例驱动模型设计出基础需求.

 

 

广告表:

记录广告一些基础数据

ADTable

1.       int id (主键)(自增1)

2.       int sort (0为图片广告,1为视频广告)

3.       nvarchar(MAX) title(广告标题)

4.       nvarchar(MAX) img(图片服务器文件路径)

5.       nvarchar(MAX) video(视频服务器文件路径).

6.       nvarchar(MAX) clickurl(用户点击广告后的超连接地址)

 

管理者表:

记录管理者的登陆名及登陆密码

adminTable

1.       int id(主键) (自增1)

2.       nvarchar(50) admin_name(管理员登陆名)

3.       nvarchar(50) admin_pwd(管理员密码)

 

参数设置表:

记录一些参数的设定值

settingTable

1.       int id(主键) (自增1)

2.       nvarchar(50) set_name(设定名)

3.       nvarchar(50) set_value(设定值)

 

D.建立wcf服务项目

启动vs2008->文件->新建->项目->visual c#->web->wcf服务应用程序

名称:jacService

如图:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

此时项目建立成功.如图:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

项目已自动生成一个wcf服务,并有测试的接口.我们手动把这此自动生成有代码删除.

IService1.cs变成:

  1. using System.Runtime.Serialization;
  2. using System.ServiceModel;
  3. using System.Text;
  4. namespace jacService
  5. {
  6.     [ServiceContract]
  7.     public interface IService1
  8.     {
  9.     }
  10. }

Service1.svc.cs

 添加SvcConfigEditor.exe工具

  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. namespace jacService
  8. {
  9.     public class Service1 : IService1
  10.     {
  11.     }
  12. }

E.

解决方案资源管理器右键web.config->打开方式->添加->(程序名右边的”…”按扭->C:/Program Files/Microsoft SDKs/Windows/v6.0A/Bin/ (如果你安装开发平台时使用了默认路径,将会在这个路径中找到)->选中SvcConfigEditor.exe ->打开->确定

如图:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

此时打开方式对话框会新增这个工具的打开方式:

如图:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

这里我介绍一下这个工具的作用.

Wcf是一种可以用配置文件设定大部份功能的技术,所以它的配置参数可以是相当复杂,如果我们在使用这个配置时使用纯手工方式编将会带来一些不必要的错误,所以windows sdk中自带了这个工具.你可以通过这个ScvConfigEditor.exe配置复杂的wcf配置文件,而且使用相当直观,可以为你的编写wcf配置文件时节省时间和免除不必要的错误.

关于这个工具的一些使用,我会在另写一编文章中深入说明.

 

操作继续:选中SvcConfigEditor.exe->确定

 

Binding设定为basicHttpBinding.

如图:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

添加一个新的绑定”,选中左边配置中的绑定”,点击右边的新建绑定配置…”

如图:

 

 

 

 

 

 

 

 

 

选中basicHttpBinding-> 确定

如图:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

设定,不设定readerQuotas将会导致无法以byte[]形式上传文件.

maxBufferSize="999999999" maxReceivedMessageSize="999999999"

readerQuotas

maxArrayLength="999999999" maxStringContentLength="999999999"

 

如图:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

把绑定与服务关联:

如图:

 

 

 

 

 

 

 

 

点击关闭”->保存. 到这里,web.config完成设定.

以下是完整的web.config中的wcf服务配置文件:

 

 

 

  1. <system.serviceModel>
  2.     <bindings>
  3.       <basicHttpBinding>
  4.         <binding name="NewBinding0" maxBufferSize="999999999" maxReceivedMessageSize="999999999" >
  5.           <readerQuotas maxArrayLength="999999999" maxStringContentLength="999999999"/>
  6.         </binding>
  7.       </basicHttpBinding>
  8.     </bindings>
  9.     <services>
  10.       <service behaviorConfiguration="jacService.Service1Behavior"
  11.         name="jacService.Service1">
  12.         <endpoint address="" binding="basicHttpBinding" bindingConfiguration="NewBinding0"
  13.           contract="jacService.IService1">
  14.           <identity>
  15.             <dns value="localhost" />
  16.           </identity>
  17.         </endpoint>
  18.         <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
  19.       </service>
  20.     </services>
  21.     <behaviors>
  22.       <serviceBehaviors>
  23.         <behavior name="jacService.Service1Behavior">
  24.           <!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false 并删除上面的元数据终结点-->
  25.           <serviceMetadata httpGetEnabled="true"/>
  26.           <!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息-->
  27.           <serviceDebug includeExceptionDetailInFaults="false"/>
  28.         </behavior>
  29.       </serviceBehaviors>
  30.     </behaviors>
  31.   </system.serviceModel>

F.

跨域文件制作:

本于安全原因,silverlight如果要访问不同域名wcf服务必须要在wcf 服务的宿主建立一个跨域文件,否则会提示错误.

(注意:如果你的wcf服务的宿主是IIS,那么这个文件必须在IIS的根目录,而不是这个wcf服务的根目录)

操作:添加->新建项->xml文件->命名为:clientaccesspolicy.xml(这个文件名不可更改)->内容如下:

 

.如图:

 

 

 

 

 

 

 

 

 

 

 

 

 

完成.

 

G. 数据库连接,linq to sql类的定义

1.连接数据库

服务器资源管理器->数据连接->添加连接

把你sql server连接上.如图:(并非所有人都一样)这里我把数据库命名为jacAdData)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

2.添加linq to sql

添加->新建项->Linq To Sql->命名为adData.dbml

如图:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

添加

3.       设计Linq to sql

从左边的服务器资源管理器中拖放ad,admin,setting三个表到右边的空白处.

如图:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

保存. 完成.

 

H.服务器端接口设计(为了简化复杂性,这些接口的设计没有考虑安全因数.)

 

 

  1. [ServiceContract]
  2.     public interface IService1
  3.     { 
  4.         #region 浏览端 
  5.         //返回指定文件,以byte[]类型 
  6.         [OperationContract]
  7.         byte[] getByte(string fileName); 
  8.         #endregion 
  9.         #region 返回所有AD 
  10.         //取得所有广告 
  11.         [OperationContract]
  12.         List<ad> getAD(); 
  13.         #endregion 
  14.         #region 管理端 
  15.         #region 增,删,改,查 
  16.         //新增广告 
  17.         [OperationContract]
  18.         bool addAD(ad data, bytedata ms);
  19.         //查询指定id的广告 
  20.         [OperationContract]
  21.         ad selectAD(int id);
  22.         //删除广告 
  23.         [OperationContract]
  24.         bool delete(int id);
  25.         //初始化数据库 
  26.         [OperationContract]
  27.         bool insdata(); 
  28.         #endregion 
  29.         #region 设置 
  30.         //取得所有设定 
  31.         [OperationContract]
  32.         List<setting> getsetting();
  33.         //修改设定 
  34.         [OperationContract]
  35.         bool setdata(string key, string data); 
  36.         #endregion 
  37.         #region 密码修改 
  38.         //登陆 
  39.         [OperationContract]
  40.         bool login(string userid, string pwd);
  41.         //修改密码 
  42.         [OperationContract]
  43.         bool updatepwd(string oldpwd, string pwd); 
  44.         #endregion 
  45.         #endregion 
  46. }

I.

接口实现

 

接口实现

 

 

接口实现

 

以下是Service1.svc.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. using System.IO;
  8. using System.Web.Hosting;
  9. using System.ServiceModel.Activation;
  10. namespace jacService
  11. {
  12.     
  13.     public class Service1 : IService1
  14.     {
  15.         //实例化linq to sql类 
  16.         adDataDataContext dc = new adDataDataContext();
  17.         //定义图片和视频文件的路径 
  18.         string filedir = HostingEnvironment.ApplicationPhysicalPath + @"data/"
  19.         #region IService1 成员 
  20.         public byte[] getByte(string fileName)
  21.         {
  22.             //返回指定文件,以byte[] 
  23.             return File.ReadAllBytes(filedir + fileName);
  24.         }
  25.         public List<ad> getAD()
  26.         {
  27.             //取得所有广告 
  28.             return dc.GetTable<ad>().ToList();
  29.         }
  30.         //添加广告 
  31.         public bool addAD(ad data, bytedata ms)
  32.         {
  33.             add(data, ms);
  34.             return true;
  35.         }
  36.         //查询 
  37.         public ad selectAD(int id)
  38.         {
  39.             return dc.ad.Single(d => d.id == id);
  40.         }
  41.         //删除 
  42.         public bool delete(int id)
  43.         {
  44.             //判断是否存在此广告,使用linq表达式 
  45.             if ((from d in dc.ad where d.id == id select d).Count() > 0)
  46.             {
  47.                 var data = dc.ad.Single(d => d.id == id);
  48.                 //删除文件 
  49.                 if (File.Exists(filedir + data.video))
  50.                 {
  51.                     File.Delete(filedir + data.video);
  52.                 }
  53.                 if (File.Exists(filedir + data.img))
  54.                 {
  55.                     File.Delete(filedir + data.img);
  56.                 }
  57.                 //删除数据 
  58.                 dc.ad.DeleteOnSubmit(data);
  59.                 dc.SubmitChanges();
  60.             }
  61.             return true;
  62.         }
  63.         //初始化数据库,及文件夹 
  64.         public bool insdata()
  65.         {
  66.             try
  67.             {
  68.                 var myad = dc.GetTable<ad>();
  69.                 foreach (var mad in myad)
  70.                 {
  71.                     dc.ad.DeleteOnSubmit(mad);
  72.                 }
  73.                 dc.SubmitChanges();
  74.                 Directory.Delete(HostingEnvironment.ApplicationPhysicalPath + "data",true);
  75.                 Directory.CreateDirectory(HostingEnvironment.ApplicationPhysicalPath + "data");
  76.                 return true;
  77.             }
  78.             catch
  79.             {
  80.                 return false;
  81.             }
  82.         }
  83.         //取得所有设定 
  84.         public List<setting> getsetting()
  85.         {
  86.             return dc.GetTable<setting>().ToList();
  87.         }
  88.         //设定参数 
  89.         public bool setdata(string key, string data)
  90.         {
  91.             //查询指定记录,使用lambda表达式 
  92.             var ndata = dc.setting.Single(d => d.set_name == key);
  93.             ndata.set_value = data;
  94.             dc.SubmitChanges();
  95.             return true;
  96.         }
  97.         //管理员登陆 
  98.         public bool login(string userid, string pwd)
  99.         {
  100.             if ((from i in dc.admin where i.admin_name==userid select i).Count() >0)
  101.             {
  102.                 admin data = dc.admin.Single(d => d.admin_name == userid);
  103.                 if (data.admin_pwd == pwd)
  104.                 {
  105.                     return true;
  106.                 }
  107.                 else
  108.                 {
  109.                     return false;
  110.                 }
  111.             }
  112.             else
  113.             {
  114.                 return false;
  115.             }
  116.         }
  117.         //管理员修改密码 
  118.         public bool updatepwd(string oldpwd, string pwd)
  119.         {
  120.             var user = dc.admin.Single(d => d.admin_name == "admin");
  121.             if (oldpwd.Equals(user.admin_pwd))
  122.             {
  123.                 user.admin_pwd = pwd;
  124.                 dc.SubmitChanges();
  125.                 return true;
  126.             }
  127.             else
  128.             {
  129.                 return false;
  130.             }
  131.         } 
  132.         #endregion 
  133.         //新增操作 
  134.         void add(ad data, bytedata ms)
  135.         {
  136.             if (data.sort == 0)
  137.             {
  138.                 StreamToFile(ms.filedata, filedir + data.img);
  139.                 dc.ad.InsertOnSubmit(data);
  140.                 dc.SubmitChanges();
  141.             }
  142.             else if (data.sort == 1)
  143.             {
  144.                 StreamToFile(ms.filedata, filedir + data.video);
  145.                 dc.ad.InsertOnSubmit(data);
  146.                 dc.SubmitChanges();
  147.             }
  148.         }
  149.         //将流写为文件 
  150.         void StreamToFile(byte[] bytes, string fileName)
  151.         {
  152.             FileStream fs = new FileStream(fileName, FileMode.Create);
  153.             BinaryWriter bw = new BinaryWriter(fs);
  154.             bw.Write(bytes);
  155.             bw.Close();
  156.             fs.Close();
  157.         }
  158.     }
  159.     //数据契约 
  160.     [DataContract]
  161.     public class bytedata
  162.     {
  163.         [DataMember]
  164.         public byte[] filedata { getset; }
  165.     }
  166. }

J.

1.打开adData.designer.cs文件:如图:

 

 

 

 

 

 

 

 

数据契约说明:在wcf开发中,当我们需要传输一些我们自定义的数据类型时,这些数据类型都必须为可序列化和返序列化,wcf为我们提供了十方便的使用方式,只要在数据定义时加上DataContract"数据契约"即可,这样自定义数据在传输过程中会自动序列化和反序列化,而DataMember即为数据契约的成员,如果数据契约成员没有被标识为DataMember,那么wcf会视此成员为不进行序列化和反序列化.以下是一个简单的例子:

  1.     //数据契约
  2.     [DataContract]
  3.     public class mydata
  4.     {
  5.         //数据成员
  6.         [DataMember]
  7.         public string userid { getset; }
  8.         //数据成员
  9.         [DataMember]
  10.         public int age { getset; }
  11.         //非数据成员
  12.         public string address { getset; }
  13.     }

2.添加System.Runtime引用.

以下是整个adData.designer.cs文件修改后的内容,(注意using System.Runtime.Serialization;

[DataContract],[DataMember]和定义.)

  1. namespace jacService
  2. {
  3.     using System.Data.Linq;
  4.     using System.Data.Linq.Mapping;
  5.     using System.Data;
  6.     using System.Collections.Generic;
  7.     using System.Reflection;
  8.     using System.Linq;
  9.     using System.Linq.Expressions;
  10.     using System.ComponentModel;
  11.     using System;
  12.     using System.Runtime.Serialization;
  13.     
  14.     
  15.     [System.Data.Linq.Mapping.DatabaseAttribute(Name="jacADdata")]
  16.     public partial class adDataDataContext : System.Data.Linq.DataContext
  17.     {
  18.         
  19.         private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource(); 
  20.         
  21.     #region Extensibility Method Definitions 
  22.     partial void OnCreated();
  23.     partial void Insertad(ad instance);
  24.     partial void Updatead(ad instance);
  25.     partial void Deletead(ad instance);
  26.     partial void Insertadmin(admin instance);
  27.     partial void Updateadmin(admin instance);
  28.     partial void Deleteadmin(admin instance);
  29.     partial void Insertsetting(setting instance);
  30.     partial void Updatesetting(setting instance);
  31.     partial void Deletesetting(setting instance); 
  32.     #endregion 
  33.         
  34.         public adDataDataContext() : 
  35.                 base(global::System.Configuration.ConfigurationManager.ConnectionStrings["jacADdataConnectionString"].ConnectionString, mappingSource)
  36.         {
  37.             OnCreated();
  38.         }
  39.         
  40.         public adDataDataContext(string connection) : 
  41.                 base(connection, mappingSource)
  42.         {
  43.             OnCreated();
  44.         }
  45.         
  46.         public adDataDataContext(System.Data.IDbConnection connection) : 
  47.                 base(connection, mappingSource)
  48.         {
  49.             OnCreated();
  50.         }
  51.         
  52.         public adDataDataContext(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) : 
  53.                 base(connection, mappingSource)
  54.         {
  55.             OnCreated();
  56.         }
  57.         
  58.         public adDataDataContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) : 
  59.                 base(connection, mappingSource)
  60.         {
  61.             OnCreated();
  62.         }
  63.         
  64.         public System.Data.Linq.Table<ad> ad
  65.         {
  66.             get
  67.             {
  68.                 return this.GetTable<ad>();
  69.             }
  70.         }
  71.         
  72.         public System.Data.Linq.Table<admin> admin
  73.         {
  74.             get
  75.             {
  76.                 return this.GetTable<admin>();
  77.             }
  78.         }
  79.         
  80.         public System.Data.Linq.Table<setting> setting
  81.         {
  82.             get
  83.             {
  84.                 return this.GetTable<setting>();
  85.             }
  86.         }
  87.     }
  88.     [DataContract]
  89.     [Table(Name="dbo.ad")]
  90.     public partial class ad : INotifyPropertyChanging, INotifyPropertyChanged
  91.     {
  92.         
  93.         private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
  94.         
  95.         private int _id;
  96.         
  97.         private int _sort;
  98.         
  99.         private string _title;
  100.         
  101.         private string _img;
  102.         
  103.         private string _video;
  104.         
  105.         private string _clickurl; 
  106.         
  107.     #region Extensibility Method Definitions 
  108.     partial void OnLoaded();
  109.     partial void OnValidate(System.Data.Linq.ChangeAction action);
  110.     partial void OnCreated();
  111.     partial void OnidChanging(int value);
  112.     partial void OnidChanged();
  113.     partial void OnsortChanging(int value);
  114.     partial void OnsortChanged();
  115.     partial void OntitleChanging(string value);
  116.     partial void OntitleChanged();
  117.     partial void OnimgChanging(string value);
  118.     partial void OnimgChanged();
  119.     partial void OnvideoChanging(string value);
  120.     partial void OnvideoChanged();
  121.     partial void OnclickurlChanging(string value);
  122.     partial void OnclickurlChanged(); 
  123.     #endregion 
  124.         
  125.         public ad()
  126.         {
  127.             OnCreated();
  128.         }
  129.         [DataMember(Order=1)]
  130.         [Column(Storage="_id", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
  131.         public int id
  132.         {
  133.             get
  134.             {
  135.                 return this._id;
  136.             }
  137.             set
  138.             {
  139.                 if ((this._id != value))
  140.                 {
  141.                     this.OnidChanging(value);
  142.                     this.SendPropertyChanging();
  143.                     this._id = value;
  144.                     this.SendPropertyChanged("id");
  145.                     this.OnidChanged();
  146.                 }
  147.             }
  148.         }
  149.         [DataMember(Order = 2)]
  150.         [Column(Storage="_sort", DbType="Int NOT NULL")]
  151.         public int sort
  152.         {
  153.             get
  154.             {
  155.                 return this._sort;
  156.             }
  157.             set
  158.             {
  159.                 if ((this._sort != value))
  160.                 {
  161.                     this.OnsortChanging(value);
  162.                     this.SendPropertyChanging();
  163.                     this._sort = value;
  164.                     this.SendPropertyChanged("sort");
  165.                     this.OnsortChanged();
  166.                 }
  167.             }
  168.         }
  169.         [DataMember(Order = 3)]
  170.         [Column(Storage="_title", DbType="NVarChar(MAX) NOT NULL", CanBeNull=false)]
  171.         public string title
  172.         {
  173.             get
  174.             {
  175.                 return this._title;
  176.             }
  177.             set
  178.             {
  179.                 if ((this._title != value))
  180.                 {
  181.                     this.OntitleChanging(value);
  182.                     this.SendPropertyChanging();
  183.                     this._title = value;
  184.                     this.SendPropertyChanged("title");
  185.                     this.OntitleChanged();
  186.                 }
  187.             }
  188.         }
  189.         [DataMember(Order = 4)]
  190.         [Column(Storage="_img", DbType="NVarChar(MAX)")]
  191.         public string img
  192.         {
  193.             get
  194.             {
  195.                 return this._img;
  196.             }
  197.             set
  198.             {
  199.                 if ((this._img != value))
  200.                 {
  201.                     this.OnimgChanging(value);
  202.                     this.SendPropertyChanging();
  203.                     this._img = value;
  204.                     this.SendPropertyChanged("img");
  205.                     this.OnimgChanged();
  206.                 }
  207.             }
  208.         }
  209.         [DataMember(Order = 5)]
  210.         [Column(Storage="_video", DbType="NVarChar(MAX)")]
  211.         public string video
  212.         {
  213.             get
  214.             {
  215.                 return this._video;
  216.             }
  217.             set
  218.             {
  219.                 if ((this._video != value))
  220.                 {
  221.                     this.OnvideoChanging(value);
  222.                     this.SendPropertyChanging();
  223.                     this._video = value;
  224.                     this.SendPropertyChanged("video");
  225.                     this.OnvideoChanged();
  226.                 }
  227.             }
  228.         }
  229.         [DataMember(Order = 6)]
  230.         [Column(Storage="_clickurl", DbType="NVarChar(MAX) NOT NULL", CanBeNull=false)]
  231.         public string clickurl
  232.         {
  233.             get
  234.             {
  235.                 return this._clickurl;
  236.             }
  237.             set
  238.             {
  239.                 if ((this._clickurl != value))
  240.                 {
  241.                     this.OnclickurlChanging(value);
  242.                     this.SendPropertyChanging();
  243.                     this._clickurl = value;
  244.                     this.SendPropertyChanged("clickurl");
  245.                     this.OnclickurlChanged();
  246.                 }
  247.             }
  248.         }
  249.         
  250.         public event PropertyChangingEventHandler PropertyChanging;
  251.         
  252.         public event PropertyChangedEventHandler PropertyChanged;
  253.         
  254.         protected virtual void SendPropertyChanging()
  255.         {
  256.             if ((this.PropertyChanging != null))
  257.             {
  258.                 this.PropertyChanging(this, emptyChangingEventArgs);
  259.             }
  260.         }
  261.         
  262.         protected virtual void SendPropertyChanged(String propertyName)
  263.         {
  264.             if ((this.PropertyChanged != null))
  265.             {
  266.                 this.PropertyChanged(thisnew PropertyChangedEventArgs(propertyName));
  267.             }
  268.         }
  269.     }
  270.     [DataContract]
  271.     [Table(Name="dbo.admin")]
  272.     public partial class admin : INotifyPropertyChanging, INotifyPropertyChanged
  273.     {
  274.         
  275.         private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
  276.         
  277.         private int _id;
  278.         
  279.         private string _admin_name;
  280.         
  281.         private string _admin_pwd; 
  282.         
  283.     #region Extensibility Method Definitions 
  284.     partial void OnLoaded();
  285.     partial void OnValidate(System.Data.Linq.ChangeAction action);
  286.     partial void OnCreated();
  287.     partial void OnidChanging(int value);
  288.     partial void OnidChanged();
  289.     partial void Onadmin_nameChanging(string value);
  290.     partial void Onadmin_nameChanged();
  291.     partial void Onadmin_pwdChanging(string value);
  292.     partial void Onadmin_pwdChanged(); 
  293.     #endregion 
  294.         
  295.         public admin()
  296.         {
  297.             OnCreated();
  298.         }
  299.         [DataMember(Order = 1)]
  300.         [Column(Storage="_id", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
  301.         public int id
  302.         {
  303.             get
  304.             {
  305.                 return this._id;
  306.             }
  307.             set
  308.             {
  309.                 if ((this._id != value))
  310.                 {
  311.                     this.OnidChanging(value);
  312.                     this.SendPropertyChanging();
  313.                     this._id = value;
  314.                     this.SendPropertyChanged("id");
  315.                     this.OnidChanged();
  316.                 }
  317.             }
  318.         }
  319.         [DataMember(Order = 2)]
  320.         [Column(Storage="_admin_name", DbType="NVarChar(50)")]
  321.         public string admin_name
  322.         {
  323.             get
  324.             {
  325.                 return this._admin_name;
  326.             }
  327.             set
  328.             {
  329.                 if ((this._admin_name != value))
  330.                 {
  331.                     this.Onadmin_nameChanging(value);
  332.                     this.SendPropertyChanging();
  333.                     this._admin_name = value;
  334.                     this.SendPropertyChanged("admin_name");
  335.                     this.Onadmin_nameChanged();
  336.                 }
  337.             }
  338.         }
  339.         [DataMember(Order = 3)]
  340.         [Column(Storage="_admin_pwd", DbType="NVarChar(50)")]
  341.         public string admin_pwd
  342.         {
  343.             get
  344.             {
  345.                 return this._admin_pwd;
  346.             }
  347.             set
  348.             {
  349.                 if ((this._admin_pwd != value))
  350.                 {
  351.                     this.Onadmin_pwdChanging(value);
  352.                     this.SendPropertyChanging();
  353.                     this._admin_pwd = value;
  354.                     this.SendPropertyChanged("admin_pwd");
  355.                     this.Onadmin_pwdChanged();
  356.                 }
  357.             }
  358.         }
  359.         
  360.         public event PropertyChangingEventHandler PropertyChanging;
  361.         
  362.         public event PropertyChangedEventHandler PropertyChanged;
  363.         
  364.         protected virtual void SendPropertyChanging()
  365.         {
  366.             if ((this.PropertyChanging != null))
  367.             {
  368.                 this.PropertyChanging(this, emptyChangingEventArgs);
  369.             }
  370.         }
  371.         
  372.         protected virtual void SendPropertyChanged(String propertyName)
  373.         {
  374.             if ((this.PropertyChanged != null))
  375.             {
  376.                 this.PropertyChanged(thisnew PropertyChangedEventArgs(propertyName));
  377.             }
  378.         }
  379.     }
  380.     [DataContract]
  381.     [Table(Name="dbo.setting")]
  382.     public partial class setting : INotifyPropertyChanging, INotifyPropertyChanged
  383.     {
  384.         
  385.         private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
  386.         
  387.         private int _id;
  388.         
  389.         private string _set_name;
  390.         
  391.         private string _set_value; 
  392.         
  393.     #region Extensibility Method Definitions 
  394.     partial void OnLoaded();
  395.     partial void OnValidate(System.Data.Linq.ChangeAction action);
  396.     partial void OnCreated();
  397.     partial void OnidChanging(int value);
  398.     partial void OnidChanged();
  399.     partial void Onset_nameChanging(string value);
  400.     partial void Onset_nameChanged();
  401.     partial void Onset_valueChanging(string value);
  402.     partial void Onset_valueChanged(); 
  403.     #endregion 
  404.         
  405.         public setting()
  406.         {
  407.             OnCreated();
  408.         }
  409.         [DataMember(Order = 1)]
  410.         [Column(Storage="_id", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
  411.         public int id
  412.         {
  413.             get
  414.             {
  415.                 return this._id;
  416.             }
  417.             set
  418.             {
  419.                 if ((this._id != value))
  420.                 {
  421.                     this.OnidChanging(value);
  422.                     this.SendPropertyChanging();
  423.                     this._id = value;
  424.                     this.SendPropertyChanged("id");
  425.                     this.OnidChanged();
  426.                 }
  427.             }
  428.         }
  429.         [DataMember(Order = 2)]
  430.         [Column(Storage="_set_name", DbType="NVarChar(50)")]
  431.         public string set_name
  432.         {
  433.             get
  434.             {
  435.                 return this._set_name;
  436.             }
  437.             set
  438.             {
  439.                 if ((this._set_name != value))
  440.                 {
  441.                     this.Onset_nameChanging(value);
  442.                     this.SendPropertyChanging();
  443.                     this._set_name = value;
  444.                     this.SendPropertyChanged("set_name");
  445.                     this.Onset_nameChanged();
  446.                 }
  447.             }
  448.         }
  449.         [DataMember(Order = 3)]
  450.         [Column(Storage="_set_value", DbType="NVarChar(50)")]
  451.         public string set_value
  452.         {
  453.             get
  454.             {
  455.                 return this._set_value;
  456.             }
  457.             set
  458.             {
  459.                 if ((this._set_value != value))
  460.                 {
  461.                     this.Onset_valueChanging(value);
  462.                     this.SendPropertyChanging();
  463.                     this._set_value = value;
  464.                     this.SendPropertyChanged("set_value");
  465.                     this.Onset_valueChanged();
  466.                 }
  467.             }
  468.         }
  469.         
  470.         public event PropertyChangingEventHandler PropertyChanging;
  471.         
  472.         public event PropertyChangedEventHandler PropertyChanged;
  473.         
  474.         protected virtual void SendPropertyChanging()
  475.         {
  476.             if ((this.PropertyChanging != null))
  477.             {
  478.                 this.PropertyChanging(this, emptyChangingEventArgs);
  479.             }
  480.         }
  481.         
  482.         protected virtual void SendPropertyChanged(String propertyName)
  483.         {
  484.             if ((this.PropertyChanged != null))
  485.             {
  486.                 this.PropertyChanged(thisnew PropertyChangedEventArgs(propertyName));
  487.             }
  488.         }
  489.     }
  490. }

到此. 服务器端已经设计完成.

 

运行后如图:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

下一步客户端开发.

首先,我先行介绍一下一个有有到的工具软件.Expression Blend.此工具是专为wpf而开发UI设计工具,

可以很直观并快速的开发出理想的软件UI,也是程式员与UI工程师的分散协同工作工作,程序员可以用vs2008编写代码,UI工程师可以使用Blend开发UI,(Expression studio还有其他的一些工具,包括,design,encoder,media等,这里不祥说)

 

要想可以开发silverlight我们必须安装silverlight的开发扩展,目前是rc1,运行时是RTW.

安装地址(http://silverlight.net/GetStarted/)

 

 

现在我假定你的开发平台已经安装好所有开发工具(vs2008+silverlight Rc1扩展,silverlight RTW runtim,Expression blend)

 

 

K.建立AD客户端(silverlight)

1.解决方案->添加->新建项目->visual c#->silverlight->silverlight应用程序

名称:Persenter->确定->在生成时自动生成测试页以承载silverlgiht(G)

如图:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

2.再用相同的方法添加管理者的silverlight客户端名称为(admin).此时项目资管理器中会多两个silvelight项目.如图:

 

 

 

 

 

L.引用wcf服务.(这里介绍如何在silverlight中引用wcf服务)

1.扩开Persenter项目:

右键引用->添加服务引用->地址->http://localhost:3725/Service1.svc(此为wcf服务地址,取得方法就是通过运行服务项目得到,如上服务设计完成图中的IE的url地址.)->确定->命名空间我不作修改使用ServiceReference1.此时项目会自动生成客户端的代理文件.

 

2.设计广告轮播器.

Persenter->右键page.xaml->在Expression Blend中打开->进入到blend中.

设计成如下:

  1. <UserControl x:Class="Persenter.Page"
  2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="Auto" Height="Auto">
  4.     <Grid x:Name="LayoutRoot" Background="#FFD0F284" Opacity="1">
  5.         <Image Margin="0,0.5,0,37" x:Name="img1" Stretch="UniformToFill"/>
  6.         <TextBlock Height="25" Margin="26,0,26,8" VerticalAlignment="Bottom" TextWrapping="Wrap" x:Name="tbk_title" Text="Title" TextAlignment="Center" Foreground="#FF4F4F39" HorizontalAlignment="Stretch" Cursor="Hand" Width="350" FontSize="14"/>
  7.         <TextBlock Height="25" HorizontalAlignment="Left" Margin="8,0,0,8" VerticalAlignment="Bottom" Width="14" Text="<" TextWrapping="Wrap" Foreground="#FFEC2B2B" x:Name="left"/>
  8.         <TextBlock Height="25" HorizontalAlignment="Right" Margin="0,0,8,8" VerticalAlignment="Bottom" Width="14" Text=">" TextWrapping="Wrap" x:Name="right" Foreground="#FFEC2B2B"/>
  9.         <MediaElement x:Name="video" Opacity="1" Volume="1" Margin="0,0,0,37"/>
  10.     </Grid>
  11. </UserControl>

3.设计app.xaml.cs文件,让silverlight可以接受外来参数的传入.

  1. namespace Persenter
  2. {
  3.     public partial class App : Application
  4.     {
  5.         public IDictionary<stringstring> inputdata;
  6.         private void Application_Startup(object sender, StartupEventArgs e)
  7.         {
  8.             this.RootVisual = new Page();
  9.             inputdata = e.InitParams;
  10.         }

这样我们可以通过设定silverlgiht所在的asp.net页中的控件定义参数为行数据传入.

例子:

  1.             <asp:Silverlight ID="Xaml1" runat="server" Source="~/ClientBin/Persenter.xap"
  2.              MinimumVersion="2.0.30523" Width="100%" Height="100%"
  3.              InitParameters="参数1名=参数1值,参数2名=参数2值"/>

使用时:

  1.                 App myapp = App.Current as App;
  2.                 var url = myapp.inputdata.Single(d => d.Key.Equals("参数1名"));
  3.                 var Ivalue = url.Value;

4.添加一个新类以管理取得从外边传入的wcf服务地址.类名为(serverManager.cs)

这里实例了一个wcf服务的客户端代理.命名为sc.

而且定义了一个方法返回一个使用了传入服务地址的代理命名为getpox()

  1. namespace Persenter
  2. {
  3.     internal class serverManager
  4.     {
  5.         private static ServiceReference1.Service1Client sc = new Persenter.ServiceReference1.Service1Client();
  6.         internal static ServiceReference1.Service1Client getPox()
  7.         {
  8.             if (sc.State== System.ServiceModel.CommunicationState.Created)
  9.             {
  10.                 App myapp = App.Current as App;
  11.                 var url = myapp.inputdata.Single(d => d.Key.Equals("server"));
  12.                 sc.Endpoint.Address = new System.ServiceModel.EndpointAddress(url.Value);
  13.                 return sc;
  14.             }
  15.             else
  16.             {
  17.                 return sc;
  18.             }
  19.         }
  20.     }
  21. }

5.page.xaml.cs代码(这里是实现广告的核心)

  1. namespace Persenter
  2. {
  3.     public partial class Page : UserControl
  4.     {
  5.         //自定义的timer,这里我使用了Storyboard,利用Storyboard的conpleted事件不断重复. 
  6.         Storyboard looper;
  7.         public Page()
  8.         {
  9.             InitializeComponent();
  10.             //实例化自定义timer 
  11.             looper = new Storyboard();
  12.             //设定重复时间,这里默认值设为5秒 
  13.             looper.Duration = new Duration(TimeSpan.FromSeconds(5));
  14.             //注册completed事件 
  15.             looper.Completed += new EventHandler(looper_Completed);
  16.             //注册初始化事件 
  17.             this.Loaded += new RoutedEventHandler(Page_Loaded);
  18.             //注册上翻事件 
  19.             this.left.MouseLeftButtonDown += left_MouseLeftButtonDown;
  20.             //注册下翻事件 
  21.             this.right.MouseLeftButtonDown += right_MouseLeftButtonDown;
  22.         }
  23.         //下翻时件 
  24.         void right_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  25.         {
  26.             if (sumer < counter)
  27.             {
  28.                 sumer += 1;
  29.             }
  30.             showad(ads[sumer]);
  31.             looper.Stop();
  32.             looper.Begin();
  33.         }
  34.         //上翻事件 
  35.         void left_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  36.         {
  37.             if (sumer > 0)
  38.             {
  39.                 sumer -= 1;
  40.             }
  41.             showad(ads[sumer]);
  42.             looper.Stop();
  43.             looper.Begin();
  44.         }
  45.         //timer重复事件 
  46.         void looper_Completed(object sender, EventArgs e)
  47.         {
  48.             if (sumer < counter)
  49.             {
  50.                 sumer += 1;
  51.                 showad(ads[sumer]);
  52.             }
  53.             else if (sumer == counter) 
  54.             {
  55.                 sumer = -1;  
  56.             }
  57.             looper.Begin();
  58.         }
  59.         //运算子 
  60.         int sumer = 0;
  61.         int counter = 0;
  62.         //显示广告方法 
  63.         void beginadshow(List<ad> data)
  64.         {
  65.             counter = data.Count() - 1;
  66.             showad(data[sumer]);
  67.             looper.Begin();
  68.         }
  69.         //wcf服务的客户端代理 
  70.         Service1Client sc;
  71.         void Page_Loaded(object sender, RoutedEventArgs e)
  72.         {
  73.             //加载服务 
  74.             sc = serverManager.getPox();
  75.             //注册控件的鼠标事件 
  76.             tbk_title.MouseLeftButtonDown += tbk_title_MouseLeftButtonDown;
  77.             img1.MouseLeftButtonDown += tbk_title_MouseLeftButtonDown;
  78.             video.MouseLeftButtonDown += tbk_title_MouseLeftButtonDown;
  79.             //注册服务异步完成事件 
  80.             sc.getByteCompleted += sc_getByteCompleted;
  81.             sc.getsettingCompleted += sc_getsettingCompleted;
  82.             sc.getADCompleted += sc_getADCompleted;
  83.             //异步调用wcf服务的getsetting和getad方法 
  84.             sc.getsettingAsync();
  85.             sc.getADAsync();
  86.         }
  87.         //getsetting完成后的操作 
  88.         void sc_getsettingCompleted(object sender, getsettingCompletedEventArgs e)
  89.         {
  90.             //取得返回结果(服务器端必须把setting定义为 [DataContract]才能在这里还原数据. 
  91.             List<setting> set = new List<setting>(e.Result);
  92.             //取得setting后把属性设定 
  93.             this.Width = double.Parse(set.Single(d => d.set_name == "宽度").set_value);
  94.             this.Height = double.Parse(set.Single(d => d.set_name == "高度").set_value);
  95.             this.tbk_title.FontSize = double.Parse(set.Single(d => d.set_name == "字体大小").set_value);
  96.             this.video.Volume = double.Parse(set.Single(d => d.set_name == "视频音量").set_value);
  97.             clickmode = int.Parse(set.Single(d => d.set_name == "连接方式").set_value);
  98.         }
  99.         //getad完成后的操作 
  100.         void sc_getADCompleted(object sender, getADCompletedEventArgs e)
  101.         {
  102.             //取得广告集合 
  103.             ads = new List<ad>(e.Result);
  104.             beginadshow(ads);
  105.         }
  106.         //用户点击后的超连接 
  107.         Uri clickurl;
  108.         //广告集合定义 
  109.         List<ad> ads;
  110.         //用户点击后的超连接方式,1:在当前页打开,0:从新页个打开. 
  111.         int clickmode = 1;
  112.         //广告显示 
  113.         void showad(ad d)
  114.         {
  115.             //image的souce属性初始化 
  116.             img1.ClearValue(Image.SourceProperty);
  117.             //title显不 
  118.             tbk_title.Text = d.title;
  119.             //取得超连接 
  120.             clickurl = new Uri(d.clickurl);
  121.             //图片或视频的运算子,0:图片,1:视频 
  122.             adsort = d.sort;
  123.             //判断并设定图片或视频到控件. 
  124.             if (d.sort == 0)
  125.             {
  126.                 video.Opacity = 0;
  127.                 video.Stop();
  128.                 //向服务器申请图片内容byte[] 
  129.                 sc.getByteAsync(d.img);
  130.             }
  131.             else if (d.sort == 1)
  132.             {
  133.                 video.Opacity = 1;
  134.                 //向服务器申请视频内容byte[] 
  135.                 sc.getByteAsync(d.video);
  136.             }
  137.         }
  138.         //定义图片或视频的运算子,0:图片,1:视频 
  139.         int adsort = 0;
  140.         //向服务器申请视频内容byte[]完成 
  141.         void sc_getByteCompleted(object sender, getByteCompletedEventArgs e)
  142.         {
  143.             //还原byte[]到图片或视频播控件 
  144.             BitmapImage bi = new BitmapImage();
  145.             MemoryStream data = new MemoryStream(e.Result);
  146.             if (adsort == 0)
  147.             {
  148.                 bi.SetSource(data);
  149.                 img1.Source = bi;
  150.             }
  151.             else if (adsort == 1)
  152.             {
  153.                 video.SetSource(data);
  154.                 video.Play();
  155.             }
  156.         }
  157.         //鼠标点击事件 
  158.         void tbk_title_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  159.         {
  160.             if (clickmode == 1)
  161.             {
  162.                 //以新页方式打开超连接 
  163.                 HtmlPage.Window.Navigate(clickurl, "blend");
  164.             }
  165.             else
  166.             {
  167.                 //以本页方式打开 
  168.                 HtmlPage.Window.Navigate(clickurl);
  169.             }
  170.         }
  171.     }
  172. }

到此广告轮播展示器完成,接下来我们开发管理者的silverlight端.

 

项目文件下载地址:http://www.codeplex.com/jacad

 

本文禁止转载!本人保留一切法律权利!  by Jacob Lai

 

设定linq to sql为数据契约

变成:

 

评论 27
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值