一。数据库准备:
数据库:Sqlserver 2008
数据库名:Test_DB
建表语句:
CREATE TABLE [dbo].[liujh_test_user]( [UserId] [nchar](10) COLLATE Japanese_CI_AS NULL, [PassWord] [nchar](10) COLLATE Japanese_CI_AS NULL, [Mone] [nchar](10) COLLATE Japanese_CI_AS NULL ) ON [PRIMARY] 插入数据: INSERT INTO [liujh_test].[dbo].[liujh_test_user] ([UserId] ,[PassWord] ,[Mone]) VALUES ('admin1' ,'admin1' ,'admin1')
二。建WCF注意事项
1,WCF的Web.config配置
WcfService/Web.config的 wsHttpBinding->basicHttpBinding
<!--liujh 修改-->
<!--<endpoint address="" binding="wsHttpBinding" contract="WcfService.IService1">-->
<endpoint address="" binding="basicHttpBinding" contract="WcfService.IService1">
2,复制两个文件解决跨域问题:
clientaccesspolicy.xml
<?xml version="1.0" encoding="utf-8"?> <access-policy> <cross-domain-access> <policy> <allow-from http-request-headers="*"> <domain uri="*"/> </allow-from> <grant-to> <resource path="/" include-subpaths="true"/> </grant-to> </policy> </cross-domain-access> </access-policy>
crossdomain.xml
<?xml version="1.0"?> <!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd "> <cross-domain-policy> <allow-http-request-headers-from domain="*" headers="*"/> </cross-domain-policy>
三。显示忙碌控件代码
1,注意包含控件引用:
双击工具箱:BusyIndicator控件,这样会自动添加引用。
或者手动添加并引用:
xmlns:controlsToolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit",
2,控件使用代码:
<!--忙碌控件显示控件 1start--> <controlsToolkit:HeaderedContentControl Grid.RowSpan="4" > <controlsToolkit:BusyIndicator x:Name="SampleIndicator" IsBusy="{Binding}"> <!--忙碌控件显示控件 2end --> <Grid x:Name="LayoutRoot" Background="#FFE8F3F3" > <TextBox x:Name="txtuser" Margin="218,144,304,0" VerticalAlignment="Top" TextWrapping="Wrap"/> <TextBlock HorizontalAlignment="Left" Margin="128,144,0,0" VerticalAlignment="Top" Text="用户名" TextWrapping="Wrap"/> <TextBox x:Name="txtpw" Margin="218,172,304,0" VerticalAlignment="Top" TextWrapping="Wrap"/> <TextBlock HorizontalAlignment="Left" Margin="128,172,0,0" VerticalAlignment="Top" Text="密码" TextWrapping="Wrap"/> <Button x:Name="btnlogin" HorizontalAlignment="Left" Margin="184,224,0,234" Width="75" d:LayoutOverrides="Height" Content="登录"/> <Button x:Name="btnGetuser" Click="btnGetuser_Click" Margin="296,224,269,234" Content="获取值" d:LayoutOverrides="Height"/> </Grid> <!--忙碌控件显示控件 2start--> </controlsToolkit:BusyIndicator> </controlsToolkit:HeaderedContentControl> <!--忙碌控件显示控件 2start-->
四部分代码:
1,login.xaml
<navigation:Page x:Class="SilverlightApplication1.login" 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" xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation" xmlns:controlsToolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit" d:DesignWidth="640" d:DesignHeight="480" Title="login Page"> <!--忙碌控件显示控件 1start--> <controlsToolkit:HeaderedContentControl Grid.RowSpan="4" > <controlsToolkit:BusyIndicator x:Name="SampleIndicator" IsBusy="{Binding}"> <!--忙碌控件显示控件 2end --> <Grid x:Name="LayoutRoot" Background="#FFE8F3F3" > <TextBox x:Name="txtuser" Margin="218,144,304,0" VerticalAlignment="Top" TextWrapping="Wrap"/> <TextBlock HorizontalAlignment="Left" Margin="128,144,0,0" VerticalAlignment="Top" Text="用户名" TextWrapping="Wrap"/> <TextBox x:Name="txtpw" Margin="218,172,304,0" VerticalAlignment="Top" TextWrapping="Wrap"/> <TextBlock HorizontalAlignment="Left" Margin="128,172,0,0" VerticalAlignment="Top" Text="密码" TextWrapping="Wrap"/> <Button x:Name="btnlogin" HorizontalAlignment="Left" Margin="184,224,0,234" Width="75" d:LayoutOverrides="Height" Content="登录"/> <Button x:Name="btnGetuser" Click="btnGetuser_Click" Margin="296,224,269,234" Content="获取值" d:LayoutOverrides="Height"/> </Grid> <!--忙碌控件显示控件 2start--> </controlsToolkit:BusyIndicator> </controlsToolkit:HeaderedContentControl> <!--忙碌控件显示控件 2start--> </navigation:Page>
2,login.cs
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.Windows.Navigation; using SilverlightApplication1.dbcomm ; namespace SilverlightApplication1 { public partial class login : Page { public login() { InitializeComponent(); } // 当用户导航到此页面时执行。 protected override void OnNavigatedTo(NavigationEventArgs e) { } private void btnGetuser_Click(object sender, RoutedEventArgs e) { try { //开始获得值,显示忙碌 DataContext = true; //链接wcf dbcomm.Service1Client db = new SilverlightApplication1.dbcomm.Service1Client(); db.getuserAsync(); db.getuserCompleted += new EventHandler<getuserCompletedEventArgs>(client_getuserCompleted); } catch (Exception ex) { throw ex; } } /// 绑定数据 private void client_getuserCompleted(object sender, getuserCompletedEventArgs e) { try { foreach (var temp in e.Result) { this.txtuser.Text = temp.UserId; this.txtpw.Text = temp.PassWord; } //获得值后,取消忙碌 DataContext = false; } catch (Exception ex) { //忙碌控件设置 DataContext = false; throw ex; } } } }
3,IService1.cs
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace WcfService { // 注意: 如果更改此处的接口名称 "IService1",也必须更新 Web.config 中对 "IService1" 的引用。 [ServiceContract] public interface IService1 { [OperationContract] string GetData(int value); [OperationContract] CompositeType GetDataUsingDataContract(CompositeType composite); // 任务: 在此处添加服务操作 [OperationContract] List<liujh_test_user> getuser(); [OperationContract] List<myuser> getuser2(); } // 使用下面示例中说明的数据约定将复合类型添加到服务操作。 [DataContract] public class CompositeType { bool boolValue = true; string stringValue = "Hello "; [DataMember] public bool BoolValue { get { return boolValue; } set { boolValue = value; } } [DataMember] public string StringValue { get { return stringValue; } set { stringValue = value; } } } [DataContract] public class myuser { string isSelect = ""; [DataMember] public string IsSelect { get { return isSelect; } set { isSelect = value; } } string stringUser = ""; [DataMember] public string StringUser { get { return stringUser; } set { stringUser = value; } } string stringPassWord = ""; [DataMember] public string StringPassWord { get { return stringPassWord; } set { stringPassWord = value; } } string stringMone = ""; public string StringMone { get { return stringMone; } set { stringMone = value; } } } }
4,Service1.svc.cs
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace WcfService { // 注意: 如果更改此处的类名“Service1”,也必须更新 Web.config 和关联的 .svc 文件中对“Service1”的引用。 public class Service1 : IService1 { public string GetData(int value) { return string.Format("You entered: {0}", value); } public CompositeType GetDataUsingDataContract(CompositeType composite) { if (composite.BoolValue) { composite.StringValue += "Suffix"; } return composite; } public List<liujh_test_user> getuser() { var db = new DataClasses1DataContext(); //const string sringSQL = @"select '1' as IsSelect ,UserId as StringUser , PassWord as StringPassWord , Mone as StringMone from liujh_test_user"; var query = from tb in db.liujh_test_user select tb; return query .ToList(); } public List<myuser> getuser2() { var db = new List<myuser>(); //const string sringSQL = @"select '1' as IsSelect ,UserId as StringUser , PassWord as StringPassWord , Mone as StringMone from liujh_test_user"; db.Add(null); return db; } } }