先新建一个Silverlight Application。
选择Silverlight Application。ok项目建好了,我们再建一个WCF Service Application以及一个Entity Data Model,如下
我们先来写这个WCF服务,代码如下
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.ServiceModel;
- using System.ServiceModel.Web;
- using System.Text;
- using System.Data;
- using Model;
- namespace DataProvider
- {
- // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
- public class Service1 : IService1
- {
- DataEntities dataEntities = new DataEntities();
- /// <summary>
- /// 获取实体
- /// </summary>
- /// <param name="value">主键</param>
- /// <returns></returns>
- public List<SS_ORGA> GetData()
- {
- List<SS_ORGA> ssOrgaList = dataEntities.SS_ORGA.ToList();
- return ssOrgaList;
- }
- /// <summary>
- /// 删除数据
- /// </summary>
- /// <param name="id">主键</param>
- public void DeleteData(string id)
- {
- SS_ORGA ssOrga = dataEntities.SS_ORGA.SingleOrDefault(s => s.C_ORGA_ID == id);
- dataEntities.SS_ORGA.DeleteObject(ssOrga);
- dataEntities.SaveChanges();
- }
- /// <summary>
- /// 修改数据
- /// </summary>
- /// <param name="ssOrga">要修改的实体对象</param>
- public void ModifyData(SS_ORGA ssOrga)
- {
- dataEntities.ApplyCurrentValues("SS_ORGA", ssOrga);
- dataEntities.SaveChanges();
- }
- /// <summary>
- /// 添加数据
- /// </summary>
- /// <param name="ssOrga">要添加的实体对象</param>
- public void AddData(SS_ORGA ssOrga)
- {
- dataEntities.AddToSS_ORGA(ssOrga);
- dataEntities.SaveChanges();
- }
- }
- }
ok,WCF写完了,我们再来将它引用到Silverlight项目中。在DataManage项目的References上点击右键,选择Add Service Reference。弹出以下界面,我们点击Discover,找到了我们刚才写的WCF服务,我们把NameSpace改为MyService。
引用完了,我们把WCF服务发布到IIS服务器,这里我们用的端口是1933。这个发布我就不多说了,大家都会。看看Silverlight服务引用客户端配置,如下
- <configuration>
- <system.serviceModel>
- <bindings>
- <basicHttpBinding>
- <binding name="BasicHttpBinding_IService1" maxBufferSize="2147483647"
- maxReceivedMessageSize="2147483647">
- <security mode="None" />
- </binding>
- </basicHttpBinding>
- </bindings>
- <client>
- <endpoint address="http://localhost:1993/Service1.svc" binding="basicHttpBinding"
- bindingConfiguration="BasicHttpBinding_IService1" contract="MyService.IService1"
- name="BasicHttpBinding_IService1" />
- </client>
- </system.serviceModel>
- </configuration>
ok,我们再看看,WPF端的调用,这里我们只做一个例子,就做个删除吧。先看看前台代码
- <UserControl x:Class="DataManage.MainPage"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- mc:Ignorable="d"
- 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">
- <Grid x:Name="LayoutRoot" Background="White" Width="665">
- <data:DataGrid Height="200" AutoGenerateColumns="False" HorizontalAlignment="Left" Margin="65,62,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="524" Background="#FFA0AC9E" Foreground="#FF1010FA">
- <data:DataGrid.Columns>
- <data:DataGridTemplateColumn Header="请选择" >
- <data:DataGridTemplateColumn.CellTemplate>
- <DataTemplate>
- <RadioButton Content="" Height="16" HorizontalAlignment="Center" Margin="0,0,0,0" Name="radioButton1" VerticalAlignment="Center" />
- </DataTemplate>
- </data:DataGridTemplateColumn.CellTemplate>
- </data:DataGridTemplateColumn>
- <data:DataGridTextColumn Header="部门Id" Binding="{Binding C_ORGA_ID}"></data:DataGridTextColumn>
- <data:DataGridTextColumn Header="部门名称" Binding="{Binding C_ORGA_NM}"></data:DataGridTextColumn>
- <data:DataGridTextColumn Header="部门类型" Binding="{Binding C_ORGA_TP}"></data:DataGridTextColumn>
- <data:DataGridTextColumn Header="电话" Binding="{Binding C_PHONE}"></data:DataGridTextColumn>
- <data:DataGridTextColumn Header="部门描述" Binding="{Binding C_ORGA_DESC}"></data:DataGridTextColumn>
- <data:DataGridTextColumn Header="电子邮箱" Binding="{Binding C_EMAIL}"></data:DataGridTextColumn>
- </data:DataGrid.Columns>
- </data:DataGrid>
- <Button Content="删除" Height="23" HorizontalAlignment="Left" Margin="446,33,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
- </Grid>
- </UserControl>
再看看后台代码
- 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 System.Net.Browser;
- namespace DataManage
- {
- using MyService;
- public partial class MainPage : UserControl
- {
- public MainPage()
- {
- InitializeComponent();
- /*404 not found 加入以下代码*/
- bool registerResult = WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);
- bool httpsResult = WebRequest.RegisterPrefix("https://", WebRequestCreator.ClientHttp);
- }
- /// <summary>
- /// 删除数据
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button1_Click(object sender, RoutedEventArgs e)
- {
- MyService.Service1Client client1 = new Service1Client();
- client1.DeleteDataCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(ClientDeleteDataCompleted);
- client1.DeleteDataAsync(((SS_ORGA)dataGrid1.SelectedItem).C_ORGA_ID);
- UserControl_Loaded(sender,e);
- }
- private void UserControl_Loaded(object sender, RoutedEventArgs e)
- {
- Brush br=new SolidColorBrush(Color.FromArgb(10,255,0,0));
- dataGrid1.RowBackground = br;
- dataGrid1.SelectionMode = DataGridSelectionMode.Single;
- MyService.Service1Client client1 = new Service1Client();
- client1.GetDataCompleted += new EventHandler<GetDataCompletedEventArgs>(ClientGetDataCompleted);
- client1.GetDataAsync();
- }
- void ClientGetDataCompleted(object sender, GetDataCompletedEventArgs e)
- {
- if (e.Error == null)
- {
- this.dataGrid1.ItemsSource = e.Result;
- }
- else
- {
- MessageBox.Show(e.Error.Message);
- }
- }
- void ClientDeleteDataCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
- {
- if (e.Error == null)
- {
- MessageBox.Show("删除成功", "提示", MessageBoxButton.OK);
- }
- else
- {
- MessageBox.Show("删除失败", "提示", MessageBoxButton.OK);
- }
- }
- }
- }
点击删除,如下
免费学习更多精品课程,登录乐搏学院官网http://www.learnbo.com/
或关注我们的官方微博微信,还有更多惊喜哦~
本文出自 “技术创造价值” 博客,请务必保留此出处http://leelei.blog.51cto.com/856755/382404