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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/379441.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/379441.shtml
英文地址,請注明出處:http://en.pswp.cn/news/379441.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

我博客主頁的搜索功能怎么不好用

用博客里面的搜索功能&#xff0c;“找找看”&#xff0c;搜索我博客里面的關鍵字&#xff0c;但是不能出現結果。但是我在別人的主頁上能夠搜索該人的內容&#xff0c;能夠查詢到記錄&#xff0c;難道博客園對每個博客的信息要先排序&#xff1f;目前我的還不在他的搜索數據庫…

小議SqlMapConfig.xml配置文件

①、mybatis-3-config.dtd 主要用于mybatis的核心配文件sqlMapConfig.xml的約束 sqlMapConfig.xml代碼如下&#xff1a; <?xml version"1.0" encoding"UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN&q…

ffmepg 命令提取音視頻數據

原文件&#xff1a; 1&#xff1a; 原音頻數據提取&#xff08;保留還是mp4的封裝格式的&#xff09;&#xff1a; ffmpeg -i test_1920x1080.mp4 -acodec copy -vn audio.mp4 -vn 就是沒有視頻&#xff0c; -acodec copy 音頻拷貝不進行任何轉碼 原視頻數據提取&#xff0…

Java BigInteger類| modInverse()方法與示例

BigInteger類modInverse()方法 (BigInteger Class modInverse() method) modInverse() method is available in java.math package. modInverse()方法在java.math包中可用。 modInverse() method is used to calculate the mod inverse by using the inverse of (this BigInteg…

【7】jQuery學習——入門jQuery選擇器之過濾選擇器-可見性過濾選擇器

這篇什么都不說&#xff0c;看標題就知道了&#xff0c;很簡單&#xff0c;就2個選擇器&#xff0c;嘿嘿 選擇器描述返回$("Element:hidden")選取所有不可見的元素集合元素$("Element:visible")選取所有可見元素集合元素這篇很簡單吧&#xff0c;就2個&…

Creating an undraggable TitleWindow container in Flex (轉載)

The following examples show how you can create an undraggable TitleWindow container by setting the isPopUp property to false on the TitleWindow instance. <?xml version"1.0" encoding"utf-8"?><!-- http://blog.flexexamples.com/2…

匯編語言-003(LAHF_SAHF 、XCHG、FLAGS、 OFFSET、ALIGN、PTR、LENGTHOF、SIZEOF)

1&#xff1a;LAHF將EFLAGS符號寄存器低8位字節復制到AH&#xff0c;SAHF將AH復制到EFLAGS符號寄存器低8位字節 .386 .model flat,stdcall.stack 4096 ExitProcess PROTO,dwExitCode:DWORD.data saveflags BYTE ?.code main PROClahfmov saveflags ,ahmov ah,saveflagssahfIN…

Mybatis中的核心配置文件SqlMapConfig.xml詳細介紹

一、properties&#xff08;屬性&#xff09; 可以引用java屬性文件中的配置信息如下 jdbc.properties代碼如下&#xff1a; jdbc.drivercom.mysql.jdbc.Driver jdbc.urljdbc:mysql://localhost:3306/mybatis?characterEncodingutf-8 jdbc.usernameroot jdbc.passwordbeyond…

用Kotlin開發您的第一個應用程序| Android與Kotlin

In the previous article, we learned how to setup Kotlin in the android studio? Now moving to journey ahead we are going to develop our first app with Kotlin. It is the basic app, but it will let you know the structure of the program. 在上一篇文章中&#x…

數據結構與算法分析-第一章Java類(02)

編寫一個名為Person的類&#xff0c;它包含分別表示人的名字與年齡的兩個數據域。要求此類包含對其中任何一個數據域進行設置與獲取的方法。還要求包含可進行下列測試的方法&#xff1a; 兩個Person對象是否相等--即是否有相同的名稱與年齡一個人是否比另一個人年長 最后&#…

asp.net對于長篇文章進行分頁

對于文章篇幅比較長的&#xff0c;就必須采用分頁顯示。在.net中對長篇文章分頁一般有2種方法&#xff0c;第一種就是先計算好一頁的文字長度是多少&#xff0c;然后把文章總的長度除設置好的單頁文字長度及可&#xff0c;用這方法可以減少認為進行分頁的繁瑣&#xff0c;但是這…

匯編語言-004(LABEL 、間接尋址、變址操作數、指針使用、TypeDef、LOOP、DWORD變量交換高位低位字)

1&#xff1a; LABEL : 為一個標號定義大小屬性&#xff0c;但不分配內存與下一個變量共用內存&#xff0c;與C中UNION類似 .386 .model flat,stdcall.stack 4096 ExitProcess PROTO,dwExitCoed:DWORD.data val16 LABEL WORD val32 DWORD 12345678hLongValue LABEL DWORD val1…

(只需挨個復制粘貼命令即可部署)在Centos7下搭建文件服務器(VSFTPD)

觀看北京尚學堂-百戰程序員筆記一、VSFTPD簡介 Linux的組件&#xff08;一款軟件&#xff09;&#xff0c;安裝到Linux后可以通過java代碼&#xff08;FtpClient&#xff09;實現文件的上傳。基于FTP協議。 由于VSFTPD是基于FTP協議&#xff0c;客戶端瀏覽器是需要通過http協議…

POJ 2421 Constructing Roads MST kruskal

最近剛學的并查集所以用kruskal來試試最小生成樹~ kruskal其實用幾句話就能說完~ 1.貪心所有邊的權值,從小到大取值 2.取值時~將邊權非0的兩個頂點~進行并查操作~如果兩個點的祖先不同...邊權加入最小生成樹...并且將兩個點納入同一個集合中 3.判斷是否所有點都在同一個集合中…

c# 聲明類的時候初始化類_使用C#初始化的列表聲明

c# 聲明類的時候初始化類The task is to create/declare a list with an initializer list in C#. 任務是在C&#xff03;中使用初始化列表創建/聲明一個列表 。 C&#xff03;清單 (C# List) A list is used to represent the list of the objects, it is represented as Lis…

編寫程序計算所輸日期是當年的第幾天

/* 1.輸入年月日&#xff0c;編寫程序計算所輸日期是當年的第幾天 *//* 2.已知列車隔日發車&#xff0c;且1/1/2006不發車(無ticket),如果所輸入數據在此日期之后&#xff0c;則輸出有沒有車票&#xff0c;否則僅輸出上一步結果。*/ /* month/date/year is which day of the ye…

匯編語言-005(XCHG、標志位操作、算術操作、比例因子的變址尋址、多個常用運算符運用、大端轉小端、數組操作)

1: 用不超過3條XCHG指令對4個8位寄存器的值重新排序&#xff0c;A,B,C,D調整為D,C,B,A .386 .model flat,stdcall.stack 4096 ExitProcess PROTO,dwExitCode:DWORD.data.code main PROCmov al,Amov bl,Bmov cl,Cmov dl,Dxchg al,dlxchg bl,clINVOKE ExitProcess,0 main ENDP E…

bcd碼二進制轉十進制_二進制編碼的十進制(BCD碼)及其加法

bcd碼二進制轉十進制Prerequisite: Number systems 先決條件&#xff1a; 數字系統 BCD Code (8421 Code): In BCD 8421 code, each decimal digit is represented using a 4-bit binary number. The 4-bit binary numbers have their weights attached as 8, 4, 2, 1 from MS…

SVN服務器部署

一、SVN版本控制器 Subversion就是一款實現版本控制的工具軟件&#xff0c;通常也成為版本控制器&#xff0c;簡稱SVN。 Subversion是Apache軟件基金會組織下的一個項目 SVN基本操作&#xff1a; checkout&#xff08;檢出&#xff09;&#xff1a;將一個服務端創建好的項目…

rtmp流\http流測試地址

測試方式&#xff1a;ffplay rtmp://58.200.131.2:1935/livetv/cctv1 rtmp&#xff1a; CCTV-1綜合:rtmp://58.200.131.2:1935/livetv/cctv1 CCTV-2財經:rtmp://58.200.131.2:1935/livetv/cctv2 CCTV-3綜藝:rtmp://58.200.131.2:1935/livetv/cctv3 CCTV-4中文國際:rtmp://58.2…