SilverLight 初探

先新建一個Silverlight Application。express

選擇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。ide

引用完了,咱們把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端的調用,這裏咱們只作一個例子,就作個刪除吧。先看看前臺代碼this

 
  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>  

 再看看後臺代碼spa

 
  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. }  

看看運行效果.net

點擊刪除,以下code

 

免費學習更多精品課程,登陸樂搏學院官網http://www.learnbo.com/orm

或關注咱們的官方微博微信,還有更多驚喜哦~

 

本文出自 「技術創造價值」 博客,請務必保留此出處http://leelei.blog.51cto.com/856755/382404

相關文章
相關標籤/搜索