SilverLight 初探

先新建一个Silverlight Application。

选择Silverlight Application。ok项目建好了,我们再建一个WCF Service Application以及一个Entity Data Model,如下

我们先来写这个WCF服务,代码如下

 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Runtime.Serialization;  
  5. using System.ServiceModel;  
  6. using System.ServiceModel.Web;  
  7. using System.Text;  
  8. using System.Data;  
  9. using Model;  
  10. namespace DataProvider  
  11. {  
  12.     // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.  
  13.     public class Service1 : IService1  
  14.     {  
  15.         DataEntities dataEntities = new DataEntities();  
  16.  
  17.         /// <summary>  
  18.         /// 获取实体  
  19.         /// </summary>  
  20.         /// <param name="value">主键</param>  
  21.         /// <returns></returns>  
  22.         public List<SS_ORGA> GetData()  
  23.         {  
  24.             List<SS_ORGA> ssOrgaList = dataEntities.SS_ORGA.ToList();  
  25.             return ssOrgaList;  
  26.         }  
  27.  
  28.         /// <summary>  
  29.         /// 删除数据  
  30.         /// </summary>  
  31.         /// <param name="id">主键</param>  
  32.         public void DeleteData(string id)  
  33.         {  
  34.             SS_ORGA ssOrga = dataEntities.SS_ORGA.SingleOrDefault(s => s.C_ORGA_ID == id);  
  35.             dataEntities.SS_ORGA.DeleteObject(ssOrga);  
  36.             dataEntities.SaveChanges();  
  37.         }  
  38.  
  39.         /// <summary>  
  40.         /// 修改数据  
  41.         /// </summary>  
  42.         /// <param name="ssOrga">要修改的实体对象</param>  
  43.         public void ModifyData(SS_ORGA ssOrga)  
  44.         {  
  45.             dataEntities.ApplyCurrentValues("SS_ORGA", ssOrga);  
  46.             dataEntities.SaveChanges();  
  47.         }  
  48.  
  49.         /// <summary>  
  50.         /// 添加数据  
  51.         /// </summary>  
  52.         /// <param name="ssOrga">要添加的实体对象</param>  
  53.         public void AddData(SS_ORGA ssOrga)  
  54.         {  
  55.             dataEntities.AddToSS_ORGA(ssOrga);  
  56.             dataEntities.SaveChanges();  
  57.         }  
  58.     }  
  59. }  

ok,WCF写完了,我们再来将它引用到Silverlight项目中。在DataManage项目的References上点击右键,选择Add Service Reference。弹出以下界面,我们点击Discover,找到了我们刚才写的WCF服务,我们把NameSpace改为MyService。

引用完了,我们把WCF服务发布到IIS服务器,这里我们用的端口是1933。这个发布我就不多说了,大家都会。看看Silverlight服务引用客户端配置,如下

 
  1. <configuration> 
  2.     <system.serviceModel> 
  3.         <bindings> 
  4.             <basicHttpBinding> 
  5.                 <binding name="BasicHttpBinding_IService1" maxBufferSize="2147483647" 
  6.                     maxReceivedMessageSize="2147483647"> 
  7.                     <security mode="None" /> 
  8.                 </binding> 
  9.             </basicHttpBinding> 
  10.         </bindings> 
  11.         <client> 
  12.             <endpoint address="http://localhost:1993/Service1.svc" binding="basicHttpBinding" 
  13.                 bindingConfiguration="BasicHttpBinding_IService1" contract="MyService.IService1" 
  14.                 name="BasicHttpBinding_IService1" /> 
  15.         </client> 
  16.     </system.serviceModel> 
  17. </configuration> 

 ok,我们再看看,WPF端的调用,这里我们只做一个例子,就做个删除吧。先看看前台代码

 
  1. <UserControl x:Class="DataManage.MainPage" 
  2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  4.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  5.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  6.     mc:Ignorable="d" 
  7.     d:DesignHeight="338" d:DesignWidth="828" xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" xmlns:dataInput="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.Input" xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation" xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" Loaded="UserControl_Loaded">  
  8.     <Grid x:Name="LayoutRoot" Background="White" Width="665">  
  9.         <data:DataGrid  Height="200" AutoGenerateColumns="False" HorizontalAlignment="Left" Margin="65,62,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="524" Background="#FFA0AC9E" Foreground="#FF1010FA">  
  10.             <data:DataGrid.Columns>  
  11.                 <data:DataGridTemplateColumn Header="请选择" >  
  12.                     <data:DataGridTemplateColumn.CellTemplate>  
  13.                         <DataTemplate>  
  14.                             <RadioButton Content=""  Height="16" HorizontalAlignment="Center" Margin="0,0,0,0" Name="radioButton1" VerticalAlignment="Center" />  
  15.                         </DataTemplate>  
  16.                     </data:DataGridTemplateColumn.CellTemplate>  
  17.                 </data:DataGridTemplateColumn>  
  18.                     <data:DataGridTextColumn Header="部门Id" Binding="{Binding C_ORGA_ID}"></data:DataGridTextColumn>  
  19.                 <data:DataGridTextColumn Header="部门名称" Binding="{Binding C_ORGA_NM}"></data:DataGridTextColumn>  
  20.                 <data:DataGridTextColumn Header="部门类型" Binding="{Binding C_ORGA_TP}"></data:DataGridTextColumn>  
  21.                 <data:DataGridTextColumn Header="电话" Binding="{Binding C_PHONE}"></data:DataGridTextColumn>  
  22.                 <data:DataGridTextColumn Header="部门描述" Binding="{Binding C_ORGA_DESC}"></data:DataGridTextColumn>  
  23.                 <data:DataGridTextColumn Header="电子邮箱" Binding="{Binding C_EMAIL}"></data:DataGridTextColumn>  
  24.             </data:DataGrid.Columns>  
  25.         </data:DataGrid>  
  26.         <Button Content="删除" Height="23" HorizontalAlignment="Left" Margin="446,33,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />  
  27.     </Grid>  
  28. </UserControl>  

 再看看后台代码

 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Windows;  
  6. using System.Windows.Controls;  
  7. using System.Windows.Documents;  
  8. using System.Windows.Input;  
  9. using System.Windows.Media;  
  10. using System.Windows.Media.Animation;  
  11. using System.Windows.Shapes;  
  12. using System.Net.Browser;  
  13. namespace DataManage  
  14. {  
  15.     using MyService;  
  16.     public partial class MainPage : UserControl  
  17.     {  
  18.         public MainPage()  
  19.         {  
  20.             InitializeComponent();  
  21.             /*404 not found 加入以下代码*/ 
  22.             bool registerResult = WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);  
  23.             bool httpsResult = WebRequest.RegisterPrefix("https://", WebRequestCreator.ClientHttp);  
  24.         }  
  25.  
  26.         /// <summary>  
  27.         /// 删除数据  
  28.         /// </summary>  
  29.         /// <param name="sender"></param>  
  30.         /// <param name="e"></param>  
  31.         private void button1_Click(object sender, RoutedEventArgs e)  
  32.         {  
  33.             MyService.Service1Client client1 = new Service1Client();  
  34.             client1.DeleteDataCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(ClientDeleteDataCompleted);  
  35.             client1.DeleteDataAsync(((SS_ORGA)dataGrid1.SelectedItem).C_ORGA_ID);  
  36.             UserControl_Loaded(sender,e);  
  37.         }  
  38.  
  39.         private void UserControl_Loaded(object sender, RoutedEventArgs e)  
  40.         {  
  41.             Brush br=new SolidColorBrush(Color.FromArgb(10,255,0,0));  
  42.             dataGrid1.RowBackground = br;  
  43.             dataGrid1.SelectionMode = DataGridSelectionMode.Single;  
  44.             MyService.Service1Client client1 = new Service1Client();  
  45.             client1.GetDataCompleted += new EventHandler<GetDataCompletedEventArgs>(ClientGetDataCompleted);  
  46.             client1.GetDataAsync();  
  47.         }  
  48.  
  49.         void ClientGetDataCompleted(object sender, GetDataCompletedEventArgs e)  
  50.         {  
  51.             if (e.Error == null)  
  52.             {  
  53.                 this.dataGrid1.ItemsSource = e.Result;  
  54.             }  
  55.             else 
  56.             {  
  57.                 MessageBox.Show(e.Error.Message);  
  58.             }  
  59.         }  
  60.  
  61.         void ClientDeleteDataCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)  
  62.         {  
  63.             if (e.Error == null)  
  64.             {  
  65.                 MessageBox.Show("删除成功", "提示", MessageBoxButton.OK);  
  66.             }  
  67.             else 
  68.             {  
  69.                 MessageBox.Show("删除失败", "提示", MessageBoxButton.OK);  
  70.             }  
  71.         }  
  72.     }  
  73. }  

看看运行效果

点击删除,如下

 

免费学习更多精品课程,登录乐搏学院官网http://www.learnbo.com/

或关注我们的官方微博微信,还有更多惊喜哦~

 

本文出自 “技术创造价值” 博客,请务必保留此出处http://leelei.blog.51cto.com/856755/382404

转载于:https://my.oschina.net/learnbo/blog/806930

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值