日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Silverlight + WCF异步调用 例子

發布時間:2023/12/1 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Silverlight + WCF异步调用 例子 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

看大家好像對我的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?用戶不存 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?}

?

然后是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?}

?

最后就是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?}

?

好了,完成。

轉載于:https://www.cnblogs.com/yinxiangpei/articles/2622245.html

總結

以上是生活随笔為你收集整理的Silverlight + WCF异步调用 例子的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。