
在前面已經介紹了Silverlight的[Out of Browser模式與COM的基本操作](http://www.cnblogs.com/jv9/archive/2010/07/23/1783379.html)以及[與Office COM的交互](http://www.cnblogs.com/jv9/archive/2010/07/24/1784102.html)。這篇我們將介紹更多Silverlight Out of Brwoser的COM實例。
我們將繼續使用過去的SilverlightOOBDemo項目進行簡單擴展。

實例1:演示Silverlight與DOS的交互,對于Windows API熟悉的朋友應該了解,使用[WShell](http://msdn.microsoft.com/en-us/library/ahcz2kh6(VS.85).aspx)可以運行任何Dos命令。
~~~
?private?void?dosBtn_Click(object?sender,?RoutedEventArgs?e)
?{
?????????????using?(dynamic?shell?=?AutomationFactory.CreateObject("WScript.Shell"))
?????????????{
?????????????????//shell.Run(@"cmd?/k?dir?/w?/p");
?????????????????shell.Run(@"cmd?/k?ping?www.cnblogs.com?-t");
?????????????}
}
~~~

實例2:使用[WShell API](http://msdn.microsoft.com/en-us/library/8c6yea83(VS.85).aspx)模擬用戶輸入實例。使用WShell的SendKeys可以模擬用戶輸入效果到應用程序中,并且可以模擬一些特殊鍵功能,例如,回車,Tab,Ctrl等按鍵。

其中要實現模擬輸入代碼如下:
~~~
?private?void?inputBtn_Click(object?sender,?RoutedEventArgs?e)
?{
?????using?(dynamic?shell?=?AutomationFactory.CreateObject("WScript.Shell"))
?????{
?????????shell.Run(@"c:/windows/notepad.exe");
?????????shell.SendKeys("my?blog:{Enter}jv9.cnblogs.com");
?????}
?}
~~~

實例3:Silverlight OOB應用讀取注冊表信息實例

使用Shell.Application的[RegRead方法](http://msdn.microsoft.com/en-us/library/x05fawxd(VS.85).aspx)可以讀取本地注冊表鍵值,例如,讀取“HKLM/Software/Microsoft/ASP.NET/RootVer”,.Net Framework的版本。
~~~
?private?void?regBtn_Click(object?sender,?RoutedEventArgs?e)
?{
?????????????using?(dynamic?WShell?=?AutomationFactory.CreateObject("WScript.Shell"))
?????????????{
?????????????????string?reg?=?WShell.RegRead(@"HKLM/Software/Microsoft/ASP.NET/RootVer");
?????????????????MessageBox.Show(".Net?Framework?Root?Version:?"?+?reg);
?????????????}
}
~~~
讀取結果:

實例4:使用Shell.Application的[RegWrite方法](http://msdn.microsoft.com/en-us/library/yfdfhz1b(VS.85).aspx)可以對注冊表進行寫操作。這個實例將實現添加Silverlight Out of Browser應用到Windows啟動項。

~~~
??private?void?regWriteBtn_Click(object?sender,?RoutedEventArgs?e)
??{
??????????????using?(dynamic?ShellApplication?=?AutomationFactory.CreateObject("Shell.Application"))
??????????????{
??????????????????dynamic?commonPrograms?=?ShellApplication.NameSpace(11);
?????????????????string?allUsersPath?=?commonPrograms.Self.Path; ?????????????????dynamic?directory?=?ShellApplication.NameSpace(allUsersPath?+?@"/Programs");
??????????????????dynamic?link?=?directory.ParseName(Deployment.Current.OutOfBrowserSettings.ShortName?+?".lnk");
?????????????????string?OOBLink?=?link.Path;
?????????????????using?(dynamic?WShell?=?AutomationFactory.CreateObject("WScript.Shell"))
?????????????????{
?????????????????????WShell.RegWrite(@"HKLM/Software/Microsoft/Windows/CurrentVersion/Run/"
??????????????????????????????????????????????????+?Deployment.Current.OutOfBrowserSettings.ShortName,
??????????????????????????????????????????????????OOBLink);
?????????????????????MessageBox.Show("請重啟你的機器,你的應用將被自動載入啟動列表.");
?????????????????}
????????????}
?}
~~~
當運行以上代碼后,應用會將OOB應用快捷方式寫入注冊表HKLM/Software/Microsoft/Windows/CurrentVersion/Run/
應用程序將在下次重啟后,自動啟動。
實例5:使用Windows 7 API實現鎖定應用到Windows 7任務欄

在Windows 7中使用Shell.Application類庫允許遍歷應用,檢查Verbs進行應用鎖定。
~~~
??private?void?pinBtn_Click(object?sender,?RoutedEventArgs?e)
??{
??????????????using?(dynamic?ShellApplication?=?AutomationFactory.CreateObject("Shell.Application"))
??????????????{
??????????????????dynamic?commonPrograms?=?ShellApplication.NameSpace(23);
??????????????????string?allUsersPath?=?commonPrograms.Self.Path; ?????????????????dynamic?directory?=?ShellApplication.NameSpace(allUsersPath?+?@"/Accessories");
??????????????????dynamic?link?=?directory.ParseName("Calculator.lnk");
?????????????????dynamic?verbs?=?link.Verbs();
?????????????????for?(int?i?=?0;?i?<?verbs.Count();?i++)
?????????????????{
?????????????????????dynamic?verb?=?verbs.Item(i);
?????????????????????if?(verb.Name.Replace(@"&",?string.Empty).ToLower()?==?"pin?to?taskbar")
?????????????????????{
?????????????????????????verb.DoIt();
?????????????????????}
?????????????????}
?????????????}
?}
~~~
當執行以上代碼后,獲取計算器應用快捷方式,然后執行“Pin to Taskbar”后,將應用鎖定在Windows 7任務欄。

實例6:Silverlight Out of Browser語音閱讀實例
使用Windows自帶的Speech API中的SAPI引擎SpVoice類可以實現語音閱讀功能。

~~~
?private?void?speechBtn_Click(object?sender,?RoutedEventArgs?e)
?{
?????????????using?(dynamic?ISpeechVoice?=?AutomationFactory.CreateObject("SAPI.SpVoice"))
?????????????{
?????????????????ISpeechVoice.Volume?=?100;
?????????????????ISpeechVoice.Speak("<rate?speed=/"0/"><pitch?middle=/"0/">Hello?everyone!?Welcome?to?my?blog,http://jv9.cnblogs.com");
?????????????}
?}
~~~
當運行以上代碼后,會聽到以上閱讀內容。
對于Silverlight Out of Browser的COM應用有一款開源項目COM Toolkit,該控件在OOB模式下可以對本地數據庫進行操作,[推薦大家參考學習](http://silverlightchina.net/html/works/2010/0808/1702.html)。
今天就寫到這里了,希望能對大家有所幫助。
[源代碼下載](http://www.silverlightchina.net/resource/code/SilverlightOOBDemo0808.rar)
歡迎大家加入"專注Silverlight" 技術討論群:
32679955(六群)
23413513(五群)
32679922(四群)
100844510(三群)
37891947(二群)
22308706(一群)
?
- 前言
- Out of Browser開篇
- Out of Browser配置,安裝和卸載
- Out of Browser的自定義應用
- Out of Browser存取本地文件系統
- Out of Browser與COM的交互基礎
- Out of Browser與Office的互操作
- Out of Browser的Debug和Notifications窗口
- Out of Browser音樂播放器
- Out of Browser與COM互操作實例
- Out of Browser在線更新和Silent安裝
- Validation數據驗證開篇
- Validation數據驗證基礎屬性和事件
- Validation數據驗證DataAnnotation機制和調試技巧
- Validation服務器端異步數據驗證
- Validation客戶端同步數據驗證
- Validation用戶提交數據驗證捕獲
- Datagrid,Dataform數據驗證和ValidationSummary
- 自定義擴展Validation類,驗證框架的總結和建議
- Navigation導航框架開篇
- 理解Navigation導航框架Frame類
- 理解Navigation導航框架Page類
- Navigation導航框架URI映射機制
- Navigation導航框架傳遞參數