看大家好像对我的NParsing框架不是很感兴趣(写NParsing帖没人顶我),那就给大家来点“甜品”,换换口谓。来说说Silverlight方面的东西。
在Silverlight中数据通信只能用异步。有人会觉得写起来很麻烦,其实不然。也有很简单的写法,一句话就能搞定。哈哈,下面看代码
吧。这是一个用户登录的功能。
首先是WCF异步调用接口定义:
代码
1 usingSystem;
2 usingSystem.ServiceModel;
3 usingTest.Model;
4
5 namespaceTest.Silverlight.Client
6 {
7 [ServiceContract]
8 publicinterfaceIUserService
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 用户不存
2 usingSystem.ServiceModel;
3 usingTest.Model;
4
5 namespaceTest.Silverlight.Client
6 {
7 [ServiceContract]
8 publicinterfaceIUserService
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(stringusername, stringpassword, AsyncCallback asyncCallback, objectasyncState);
26 intEndLogin(outUser userInfo, IAsyncResult result);
27 }
28 }
22 ///4 用户未审核
23 ///</returns>
24 [OperationContract(AsyncPattern =true)]
25 IAsyncResult BeginLogin(stringusername, stringpassword, AsyncCallback asyncCallback, objectasyncState);
26 intEndLogin(outUser userInfo, IAsyncResult result);
27 }
28 }
然后是WCF客户端通信代理类:
代码
1 usingSystem;
2 usingSystem.ServiceModel;
3 usingSystem.ServiceModel.Channels;
4 usingTest.Model;
5
6 namespaceTest.Silverlight.Client
7 {
8 publicclassUserClient : ClientBase<IUserService>, IUserService
9 {
10 privatestaticreadonlyBinding binding =newBasicHttpBinding();
11 publicUserClient(EndpointAddress remoteAddress) : base(binding, remoteAddress)
12 {
13 }
14
15 publicIAsyncResult BeginLogin(stringusername, stringpassword, AsyncCallback asyncCallback, object
16
17 asyncState)
18 {
19 returnChannel.BeginLogin(username, password, asyncCallback, asyncState);
20 }
21
22 publicintEndLogin(outUser userInfo, IAsyncResult result)
23 {
24 returnChannel.EndLogin(outuserInfo, result);
25 }
26 }
27 }
2 usingSystem.ServiceModel;
3 usingSystem.ServiceModel.Channels;
4 usingTest.Model;
5
6 namespaceTest.Silverlight.Client
7 {
8 publicclassUserClient : ClientBase<IUserService>, IUserService
9 {
10 privatestaticreadonlyBinding binding =newBasicHttpBinding();
11 publicUserClient(EndpointAddress remoteAddress) : base(binding, remoteAddress)
12 {
13 }
14
15 publicIAsyncResult BeginLogin(stringusername, stringpassword, AsyncCallback asyncCallback, object
16
17 asyncState)
18 {
19 returnChannel.BeginLogin(username, password, asyncCallback, asyncState);
20 }
21
22 publicintEndLogin(outUser userInfo, IAsyncResult result)
23 {
24 returnChannel.EndLogin(outuserInfo, result);
25 }
26 }
27 }
最后就是Silverlight中怎么调用啦:
代码
1 usingSystem;
2 usingSystem.IO;
3 usingSystem.Threading;
4 usingSystem.Windows;
5 usingSystem.Windows.Controls;
6 usingSystem.Windows.Input;
7 usingSystem.Windows.Media;
8 usingSystem.Windows.Media.Imaging;
9 usingTest.Model;
10 usingTest.Silverlight.Client;
11
12 namespaceTest.SilverlightApplication
13 {
14 publicpartialclassUserLogin : UserControl
15 {
16 privatereadonlyUserClient _UserClient =newUserClient(SystemData.UserService_EndpointAddress);
17 privatereadonlySynchronizationContext syn;
18
19 publicUserLogin()
20 {
21 syn =SynchronizationContext.Current;
22 InitializeComponent();
23 }
24
25 privatevoidbtnLogin_Click(objectsender, RoutedEventArgs e)
26 {
27 _UserClient.BeginLogin(txtUsername.Text.Trim(), txtPassword.Password.Trim(),
28 ar =>
29 {
30 User userInfo;
31 intiRet =((IUserService) ar.AsyncState).EndLogin(outuserInfo, ar);
32 syn.Post(obj =>
33 {
34 switch((int) obj)
35 {
36 case0:
37 MessageBox.Show("用户登录失败。", "提示信息", MessageBoxButton.OK);
38 break;
39 case1:
40 MessageBox.Show("用户登录成功。", "提示信息", MessageBoxButton.OK);
41 break;
42 case2:
43 case3:
44 MessageBox.Show("用户不存在或密码错误。", "提示信息", MessageBoxButton.OK);
45 break;
46 case4:
47 MessageBox.Show("用户未审核,请耐心等待。", "提示信息", MessageBoxButton.OK);
48 break;
49 }
50 },
51 iRet);
52 },
53 _UserClient);
54 }
55 }
56 }
2 usingSystem.IO;
3 usingSystem.Threading;
4 usingSystem.Windows;
5 usingSystem.Windows.Controls;
6 usingSystem.Windows.Input;
7 usingSystem.Windows.Media;
8 usingSystem.Windows.Media.Imaging;
9 usingTest.Model;
10 usingTest.Silverlight.Client;
11
12 namespaceTest.SilverlightApplication
13 {
14 publicpartialclassUserLogin : UserControl
15 {
16 privatereadonlyUserClient _UserClient =newUserClient(SystemData.UserService_EndpointAddress);
17 privatereadonlySynchronizationContext syn;
18
19 publicUserLogin()
20 {
21 syn =SynchronizationContext.Current;
22 InitializeComponent();
23 }
24
25 privatevoidbtnLogin_Click(objectsender, RoutedEventArgs e)
26 {
27 _UserClient.BeginLogin(txtUsername.Text.Trim(), txtPassword.Password.Trim(),
28 ar =>
29 {
30 User userInfo;
31 intiRet =((IUserService) ar.AsyncState).EndLogin(outuserInfo, ar);
32 syn.Post(obj =>
33 {
34 switch((int) obj)
35 {
36 case0:
37 MessageBox.Show("用户登录失败。", "提示信息", MessageBoxButton.OK);
38 break;
39 case1:
40 MessageBox.Show("用户登录成功。", "提示信息", MessageBoxButton.OK);
41 break;
42 case2:
43 case3:
44 MessageBox.Show("用户不存在或密码错误。", "提示信息", MessageBoxButton.OK);
45 break;
46 case4:
47 MessageBox.Show("用户未审核,请耐心等待。", "提示信息", MessageBoxButton.OK);
48 break;
49 }
50 },
51 iRet);
52 },
53 _UserClient);
54 }
55 }
56 }
好了,完成。