看大家好像对我的NParsing框架不是很感兴趣(写NParsing帖没人顶我),那就给大家来点“甜品”,换换口谓。来说说Silverlight方面的东西。

在Silverlight中数据通信只能用异步。有人会觉得写起来很麻烦,其实不然。也有很简单的写法,一句话就能搞定。哈哈,下面看代码

吧。这是一个用户登录的功能。

 

首先是WCF异步调用接口定义:

复制代码
代码
 1 using System;
 2 using System.ServiceModel;
 3 using Test.Model;
 4 
 5 namespace Test.Silverlight.Client
 6 {
 7     [ServiceContract]
 8     public interface IUserService
 9     {
10         /// <summary>
11         /// 用户登录
12         /// </summary>
13         /// <param name="username">用户名</param>
14         /// <param name="password">密码</param>
15         /// <param name="asyncCallback"></param>
16         /// <param name="asyncState"></param>
17         /// <returns>
18         /// 0 登录失败
19         /// 1 登录成功
20         /// 2 用户不存
21         /// 3 密码错误
22         /// 4 用户未审核
23         /// </returns>
24         [OperationContract(AsyncPattern = true)]
25         IAsyncResult BeginLogin(string username, string password, AsyncCallback asyncCallback, object asyncState);
26         int EndLogin(out User userInfo, IAsyncResult result);
27     }
28 }
复制代码

 

然后是WCF客户端通信代理类:

复制代码
代码
 1 using System;
 2 using System.ServiceModel;
 3 using System.ServiceModel.Channels;
 4 using Test.Model;
 5 
 6 namespace Test.Silverlight.Client
 7 {
 8     public class UserClient : ClientBase<IUserService>, IUserService
 9     {
10         private static readonly Binding binding = new BasicHttpBinding();
11         public UserClient(EndpointAddress remoteAddress) : base(binding, remoteAddress)
12         {
13         }
14 
15         public IAsyncResult BeginLogin(string username, string password, AsyncCallback asyncCallback, object 
16 
17 asyncState)
18         {
19             return Channel.BeginLogin(username, password, asyncCallback, asyncState);
20         }
21 
22         public int EndLogin(out User userInfo, IAsyncResult result)
23         {
24             return Channel.EndLogin(out userInfo, result);
25         }
26     }
27 }
复制代码

 

最后就是Silverlight中怎么调用啦:

复制代码
代码
 1 using System;
 2 using System.IO;
 3 using System.Threading;
 4 using System.Windows;
 5 using System.Windows.Controls;
 6 using System.Windows.Input;
 7 using System.Windows.Media;
 8 using System.Windows.Media.Imaging;
 9 using Test.Model;
10 using Test.Silverlight.Client;
11 
12 namespace Test.SilverlightApplication
13 {
14     public partial class UserLogin : UserControl
15     {
16         private readonly UserClient _UserClient = new UserClient(SystemData.UserService_EndpointAddress);
17         private readonly SynchronizationContext syn;
18 
19         public UserLogin()
20         {
21             syn = SynchronizationContext.Current;
22             InitializeComponent();
23         }
24 
25         private void btnLogin_Click(object sender, RoutedEventArgs e)
26         {
27             _UserClient.BeginLogin(txtUsername.Text.Trim(), txtPassword.Password.Trim(),
28                                    ar =>
29                                        {
30                                            User userInfo;
31                                            int iRet = ((IUserService) ar.AsyncState).EndLogin(out userInfo, ar);
32                                            syn.Post(obj =>
33                                                         {
34                                                             switch ((int) obj)
35                                                             {
36                                                                 case 0:
37                                                                     MessageBox.Show("用户登录失败。""提示信息", MessageBoxButton.OK);
38                                                                     break;
39                                                                 case 1:
40                                                                     MessageBox.Show("用户登录成功。""提示信息", MessageBoxButton.OK);
41                                                                     break;
42                                                                 case 2:
43                                                                 case 3:
44                                                                     MessageBox.Show("用户不存在或密码错误。""提示信息", MessageBoxButton.OK);
45                                                                     break;
46                                                                 case 4:
47                                                                     MessageBox.Show("用户未审核,请耐心等待。""提示信息", MessageBoxButton.OK);
48                                                                     break;
49                                                             }
50                                                         },
51                                                     iRet);
52                                        },
53                                    _UserClient);
54         }
55     }
56 }
复制代码

 

好了,完成。

posted on 2012-08-03 20:49  裴银祥的博客园  阅读(976)  评论(0编辑  收藏  举报