2018年01月10日 22:52
原创作品,转载时请务必以超链接形式标明文章原始出处,否则将追究法律责任。

今天是本人初次接触银光,以前只听说银光何其的牛叉,却一直没有机会学习。今天恰好有时间,也趁公司机子上装的是VS2010。说到这个,我就来气,我的dell 1420装不上VS2010。今天呢,本人就来个银光的数据CURD例子。别人爱说CURD,我也用这个,初次看见这个我还懵了一下,什么事CURD,原来是Create,Update,Read,Delete。废话不多说,来个现实的例子。先新建一个Silverlight Application。

183719681.png

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

104359863.png

我们先来写这个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。

110259380.png

引用完了,我们把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);  
            }  
        }  
    }  
}

153408686.png

点击删除

193241694.png

发表评论
匿名  
用户评论
暂无评论