先新建一個Silverlight Application。express
選擇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。ide
引用完了,咱們把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端的調用,這裏咱們只作一個例子,就作個刪除吧。先看看前臺代碼this
- <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>
再看看後臺代碼spa
- 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);
- }
- }
- }
- }
看看運行效果.net
點擊刪除,以下code
免費學習更多精品課程,登陸樂搏學院官網http://www.learnbo.com/orm
或關注咱們的官方微博微信,還有更多驚喜哦~
本文出自 「技術創造價值」 博客,請務必保留此出處http://leelei.blog.51cto.com/856755/382404