Silverlight Com组件支持全解析
從4月份Mix 10大會微軟發(fā)布 Silverlight 4 至今,SL的第四個4版本的推出已經(jīng)歷時半年之久, 昨日在SBlakeMore.Com上看到一組關(guān)于采用Silverlight Com組件實現(xiàn)圖像采集和數(shù)據(jù)實時分析運用相關(guān)討論.? 微軟在定義Silverlight前景規(guī)劃時 眾所周知 SL一直走的跨平臺 跨瀏覽器這條路線. 相比在前面的3個版本. 在第四個版本中,微軟處理了8000多個客戶功能請求,其中之一就是添加COM組件支持. 隨著這個功能作為新特性在Sl 4版本中推出, 對微軟宣揚的SL? 一直保持跨平臺的爭論也就出現(xiàn)了.
原因很簡單:COM對象僅僅能夠運行在Windows平臺的IE或Firefox瀏覽器中,蘋果的Mac OS X或Linux并不支持COM, 而對于COM組件支持是否會破壞Silverlight 4的跨平臺兼容性這個問題,微軟也做出相關(guān)聲明: 雖然Mac并不支持COM組件 但微軟正在研究如何使得某些 COM組件能夠訪問Mac版的Silverlight, 緊接著Linux的發(fā)言人Miguel de Icaza表示,為了更好地配合微軟的Silverlight 4功能,該公司正在試圖添加各項組件支持.但對于Linux團隊來講,這也是一個富有挑戰(zhàn)性的工作.
目前關(guān)于Silverlight 4Com對跨平臺的爭議在很多SL開發(fā)者中常常能看到相關(guān)爭論. 這里不再提及. 如下我會演示W(wǎng)indows 7系統(tǒng)關(guān)于Silverlight 4Com組件實用幾個應(yīng)用場景.
<1>Silverlight 4 Com組件應(yīng)用實例和場景
COM component(COM組件)是微軟公司為了計算機工業(yè)的軟件生產(chǎn)更加符合人類的行為方式開發(fā)的一種新的軟件開發(fā)技術(shù)。在COM構(gòu)架下,人們可以開發(fā)出各種各樣的功能專一的組件,然后將它們按照需要組合起來,構(gòu)成復雜的應(yīng)用系統(tǒng), Silverlight 4完全是在滿足更多企業(yè)級用戶對Com組件在Windows平臺運用微軟才加以支持的.
類似我們常在項目實用Silverlight 數(shù)據(jù)的導入導出基本操作需求是極為常見的.? 如下我會展示利用Com組件方式 實現(xiàn)Silverlight在OOB模式下導出到Notepad[記事本]和World 07文檔.
創(chuàng)建一個定義一個簡單頁面 XAML[源碼]:?
代碼 ?1?<Grid?x:Name="LayoutRoot"?Background="White">?2?????????<Grid.RowDefinitions>
?3?????????????<RowDefinition?Height="Auto"/>
?4?????????????<RowDefinition?Height="*"/>
?5?????????</Grid.RowDefinitions>
?6?????????<StackPanel>
?7?????????????<Button?Content="Export?data?to?Notepad"?Click="TextExport_Click"/>
?8?????????????<Button?Content="Export?data?to?Word"?Click="WordExport_Click">
?9?????????????????<Button.ContentTemplate>
10?????????????????????<DataTemplate>
11?????????????????????????<StackPanel?Orientation="Horizontal">
12?????????????????????????????<TextBlock?Text="{Binding}"/>
13?????????????????????????????<TextBlock?Text="?("/>
14?????????????????????????????<CheckBox?IsTabStop="False"
15???????????????????????????????????????Checked="CheckBox_StateChanged"
16???????????????????????????????????????Unchecked="CheckBox_StateChanged"
17???????????????????????????????????????Content="Print?directly?with?default?printer"/>
18?????????????????????????????<TextBlock?Text=")"/>
19?????????????????????????</StackPanel>
20?????????????????????</DataTemplate>
21?????????????????</Button.ContentTemplate>
22?????????????</Button>
23?????????</StackPanel>
24?????????<sdk:DataGrid?Grid.Row="1"?Name="dataGrid1"?AutoGenerateColumns="True"/>
25?</Grid>
在頁面Grid中簡單模擬多條數(shù)據(jù) 定義一個實體:
1: /// <summary> 2: /// 定義一個數(shù)據(jù)載體實體Entity 3: /// Sign by chenkai Date:2010年11月8日21:46:29 4: /// </summary> 5: public class PersonEntity 6: { 7: public string Name { set; get; } 8: public string Gender { set; get; } 9: public int Age { set; get; } 10: }在Mainpage后臺頁面使用Com時需要引入命名空間:
1: using System.Runtime.InteropServices.Automation; 2: using System.Collections; 3: using System.Threading;頁面加載初始化DataGrid中填充相關(guān)的數(shù)據(jù):
1: /// <summary> 2: /// 加載數(shù)據(jù) 3: /// </summary> 4: void MainPage_Loaded(object sender, RoutedEventArgs e) 5: { 6: var list = new List<PersonEntity>(); 7: var rand = new Random(); 8: for (int i = 0; i < 9; i++) 9: list.Add(new PersonEntity 10: { 11: Name = "Person:" + i, 12: Age = rand.Next(20), 13: Gender = (i % 2 == 0 ? "Male" : "Female"), 14: }); 15: ? 16: //Bind 17: dataGrid1.ItemsSource = list; 18: }利用C# 新特性實現(xiàn)DataGrid數(shù)據(jù)導出到NotePad[記事本中]:
1: /// <summary> 2: /// 核心操作 導出數(shù)據(jù)到記事本. 3: /// Sign by chenki Date:2010年11月8日21:51:18 4: /// </summary> 5: private void TextExport_Click(object sender, RoutedEventArgs e) 6: { 7: // Check if using AutomationFactory is allowed. 8: if (!AutomationFactory.IsAvailable) 9: { 10: MessageBox.Show("This function need the silverlight application running at evaluated OOB mode."); 11: } 12: else 13: { 14: // Use shell to open notepad application. 15: using (dynamic shell = AutomationFactory.CreateObject("WScript.Shell")) 16: { 17: shell.Run(@"%windir%\notepad", 5); 18: Thread.Sleep(100); 20: shell.SendKeys("Name{Tab}Age{Tab}Gender{Enter}"); 21: foreach (PersonEntity item in dataGrid1.ItemsSource as IEnumerable) 22: shell.SendKeys(item.Name + "{Tab}" + item.Age + "{Tab}" + item.Gender + "{Enter}"); 23: } 24: } 25: }注意所有的導出操作全部OOB模式下執(zhí)行才能有權(quán)限實現(xiàn),這個稍微我會詳細解釋:
當運行起程序后安裝本地桌面:
在OOB模式下運行起來:
導出到記事本效果:
實現(xiàn)World就相對麻煩很多.在World導出對應(yīng)OFFices套件必須是07 版本或是更高版本. 如下實現(xiàn):
1: /// <summary> 2: /// 核心操作 數(shù)據(jù)導出到World中 3: /// sign by chenkai DAte:2010年11月8日22:06:15 4: /// </summary> 5: private void WordExport_Click(object sender, RoutedEventArgs e) 6: { 7: // Check if using AutomationFactory is allowed. 8: if (!AutomationFactory.IsAvailable) 9: { 10: MessageBox.Show("This function need the silverlight application running at evaluated OOB mode."); 11: } 12: else 13: { 14: // Create Word automation object. 15: dynamic word = AutomationFactory.CreateObject("Word.Application"); 16: word.Visible = true; 17: ? 18: // Create a new word document. 19: dynamic doc = word.Documents.Add(); 20: ? 21: // Write title 22: dynamic range1 = doc.Paragraphs[1].Range; 23: range1.Text = "Silverlight4 Word Automation Sample\n"; 24: range1.Font.Size = 24; 25: range1.Font.Bold = true; 26: ? 27: var list = dataGrid1.ItemsSource as List<PersonEntity>; 28: ? 29: dynamic range2 = doc.Paragraphs[2].Range; 30: range2.Font.Size = 12; 31: range2.Font.Bold = false; 32: ? 33: // Create table 34: doc.Tables.Add(range2, list.Count+1, 3, null, null); 35: ? 36: dynamic cell = doc.Tables[1].Cell(1, 1); 37: cell.Range.Text = "Name"; 38: cell.Range.Font.Bold = true; 39: ? 40: cell = doc.Tables[1].Cell(1, 2); 41: cell.Range.Text = "Age"; 42: cell.Range.Font.Bold = true; 43: ? 44: cell = doc.Tables[1].Cell(1, 3); 45: cell.Range.Text = "Gender"; 46: cell.Range.Font.Bold = true; 47: ? 48: // Fill data to table cells 49: for (int i = 0; i < list.Count; i++) 50: { 51: cell = doc.Tables[1].Cell(i + 2, 1); 52: cell.Range.Text = list[i].Name; 53: ? 54: cell = doc.Tables[1].Cell(i + 2, 2); 55: cell.Range.Text = list[i].Age; 56: ? 57: cell = doc.Tables[1].Cell(i + 2, 3); 58: cell.Range.Text = list[i].Gender; 59: } 60: ? 61: if (_isprint) 62: { 63: // Print the word directly without preview. 64: doc.PrintOut(); 65: } 66: ? 67: } 68: }Word 07版導出效果:
有些人為何要為對于數(shù)據(jù)導出 類似文件操作等為何要在OOB模式運行.? 這一切都源自Silverlight的為了運行環(huán)境和部署的安全上做的沙箱設(shè)計.COM組件是客戶端運行的,OOB模式獲得高級權(quán)限跳出沙箱設(shè)計對本地系統(tǒng)文件操作權(quán)限的限制. 如果在安全沙盒模型下的,那就無法調(diào)用COM組件. 關(guān)于Silverlight的沙箱設(shè)計相比做過WP7嵌入式數(shù)據(jù)庫本地訪問應(yīng)該是對這點深有體會. 雖然完整保證Silverlight安全但卻本地系統(tǒng)交互上做了諸多限制對開發(fā)產(chǎn)生諸多不便.
<2>關(guān)于dynamic關(guān)鍵字
在Visual C# 2010中引入了一種新的dynamic類型,該類型是一個靜態(tài)的(static)類型,但是一個dynamic類型的對象會繞過靜態(tài)類型檢查。在大多數(shù)情況下dynamic和object類型有些相似,但是在編譯時,dynamic類型被假定為支持任何操作,也就是說dynamic類型的對象可以是一個Office對象,可以是一個COM對象或者是DOM對象,而如果在運行時發(fā)現(xiàn)該對象不是期望的對象則會拋出一個運行時異常.
從上面代碼可以看出大量使用了Dynamic類型.? 注意我們開始編碼引入空間System.Runtime.InteropServices.Automation下AutomationFactory 類提供對已注冊的自動化服務(wù)器的訪問.
自動化是應(yīng)用程序用于向腳本撰寫工具和其他應(yīng)用程序公開功能的一種基于 Windows 的技術(shù)。例如,可以使用自動化將 Office 功能添加到基于 Silverlight 的應(yīng)用程序中 如上操作就是.
應(yīng)用程序或公開功能的組件稱作自動化服務(wù)器,而訪問功能的應(yīng)用程序稱作自動化客戶端。因為必須預先安裝并在完全信任下運行自動化服務(wù)器,所以 Silverlight 僅將受信任的應(yīng)用程序作為自動化客戶端, 這也是為什么要在OOB模式下一個重要原因.
如上對NotePad導出操作:
1: using (dynamic shell = AutomationFactory.CreateObject("WScript.Shell")) 2: { 3: shell.Run(@"%windir%\notepad", 5); 4: Thread.Sleep(100); 5: ? 6: shell.SendKeys("Name{Tab}Age{Tab}Gender{Enter}"); 7: foreach (PersonEntity item in dataGrid1.ItemsSource as IEnumerable) 8: shell.SendKeys(item.Name + "{Tab}" + item.Age + "{Tab}" + item.Gender + "{Enter}"); 9: }利用Dynamic對象封裝Shell腳本來執(zhí)行數(shù)據(jù)導出. 而作為OFFice套件之一的World則完全不同.采用直接動態(tài)創(chuàng)建一個對象方式CreateObject 或 GetObject 方法檢索對自動化服務(wù)器的后期綁定引用,若要將引用用作后期綁定對象,必須將其分配給 Object 類型(在 Visual Basic 中)或dynamic 類型(在 C# 中)的變量. 同理對于outLook操作也是同樣道理
Silverlight 只能使用已安裝的自動化服務(wù)器;因此,當它們找不到要求的進程 ID 時,CreateObject 和 GetObject 方法將引發(fā)異常。
下表顯示了幾個常見的 progID 值:
-
辦公自動化:Outlook.Application、Excel.Application、PowerPoint.Application
-
系統(tǒng)自動化:Scripting.FileSystemObject、WScript.Shell、Shell.Application
-
Windows 管理規(guī)范:WbemScripting.SWbemLocator
一般情況下,這些字符串映射到自動化服務(wù)器的特定版本。如果服務(wù)器有多個版本,則該服務(wù)器通常會添加版本編號到該進程 ID
類似操作OutLook就極為簡單:
1: private dynamic outlook; 2: ? 3: private bool InitializeOutlook() 4: { 5: try 6: { 7: // If GetObject throws an exception, then Outlook is 8: // either not running or is not available. 9: outlook = AutomationFactory.GetObject("Outlook.Application"); 10: return true; 11: } 12: catch (Exception) 13: { 14: try 15: { 16: // Start Outlook and display the Inbox, but minimize 17: // it to avoid hiding the Silverlight application. 18: outlook = 19: AutomationFactory.CreateObject("Outlook.Application"); 20: outlook.Session.GetDefaultFolder(6 /* Inbox */).Display(); 21: outlook.ActiveWindow.WindowState = 1; // minimized 22: return true; 23: } 24: catch (Exception) 25: { 26: // Outlook is unavailable. 27: return false; 28: } 29: } 30: }如上關(guān)于Silverlight 4使用Com組件最為簡短的分析. 使用范圍主要涉及到我們常用的OFFice套件中.
轉(zhuǎn)載于:https://www.cnblogs.com/chenkai/archive/2010/11/08/1872210.html
總結(jié)
以上是生活随笔為你收集整理的Silverlight Com组件支持全解析的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《JavaScript 高级程序设计》
- 下一篇: DXperience-8.2.6 注册