今天是本人初次接触银光,以前只听说银光何其的牛叉,却一直没有机会学习。今天恰好有时间,也趁公司机子上装的是VS2010。说到这个,我就来气,我的dell 1420装不上VS2010。今天呢,本人就来个银光的数据CURD例子。别人爱说CURD,我也用这个,初次看见这个我还懵了一下,什么事CURD,原来是Create,Update,Read,Delete。废话不多说,来个现实的例子。先新建一个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(); } } } @H_960_404@
ok,WCF写完了,我们再来将它引用到Silverlight项目中。在DataManage项目的References上点击右键,选择Add Service Reference。d出以下界面,我们点击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> @H_960_404@
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" 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" 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" name="button1" VerticalAlignment="top" WIDth="75" Click="button1_Click" /> </GrID> </UserControl> @H_960_404@
再看看后台代码
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)); 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); } } } } @H_960_404@
看看运行效果
点击删除,如下
总结以上是内存溢出为你收集整理的SilverLight 初探一全部内容,希望文章能够帮你解决SilverLight 初探一所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)