深入探索Silverlight高级特性
立即解锁
发布时间: 2025-08-25 00:27:31 阅读量: 2 订阅数: 14 


Silverlight 4企业级应用开发入门
### 深入探索Silverlight高级特性
#### 1. COM自动化
Silverlight 4的受信任的脱机(OOB)应用程序能够与主机操作系统的一些原生功能集成。不过,这种集成仅能在Windows平台上与暴露COM接口的应用程序和平台进行互操作。这为企业级(LoB)Silverlight应用程序开辟了新的可能性,比如可以与已安装的Windows应用程序(如Microsoft Word或Outlook)集成,也能与知名的会计软件(如QuickBooks)集成。
要访问注册的COM对象,可使用`AutomationFactory`类,它位于`System.Windows`程序集的`System.Runtime.InteropServices.Automation`命名空间中。在使用前,需先检查该类的静态`IsAvailable`属性,此属性仅对在Windows上的浏览器外运行的受信任应用程序返回`true`。若该属性可用,就可以使用静态的`CreateObject`或`GetObject`方法来获取对COM对象的后期绑定引用。在C#中,要将引用作为后期绑定对象使用,必须将其赋值给`dynamic`类型的变量。注意,要启用`dynamic`关键字,Silverlight应用程序必须引用`Microsoft.CSharp.dll`程序集,在Windows 7 64位环境中,该程序集位于`c:\Program Files (x86)\Microsoft SDKs\Silverlight\v4.0\Libraries\Client`。
下面通过一个示例来演示如何将`DataGrid`的内容导出到Excel和Word应用程序。
- **创建数据类**:在Silverlight项目中添加一个新类`BookInfo`,用于定义书籍对象,并创建`BookData`类的静态方法`GetBooks`来返回书籍数组对象的`IEnumerable`集合。
```csharp
//added
using System.Collections;
public class BookInfo
{
public string Title { get; set; }
public string Author { get; set; }
public string isbn { get; set; }
public string Url { get; set; }
}
public class BookData
{
public static IEnumerable GetBooks()
{
BookInfo[] books = new BookInfo[4];
books[0] = new BookInfo ();
books[0].Title = "Accelerated Silverlight 2";
books[0].Author = "Jeff Scanlon";
books[0].isbn = "978-1-4302-1076-4";
books[0].Url =
@"http://apress.com/book/view/9781430210764";
books[1] = new BookInfo ();
books[1].Title = "Accelerated Silverlight 3";
books[1].Author = "Ashish Ghoda, Jeff Scanlon";
books[1].isbn = "978-1-4302-2429-7";
books[1].Url =
@"http://apress.com/book/view/9781430224297";
books[2] = new BookInfo ();
books[2].Title = "Introducing Silverlight 4";
books[2].Author = "Ashish Ghoda";
books[2].isbn = "978-1-4302-2991-9";
books[2].Url =
@"http://apress.com/book/view/9781430229919";
books[3] = new BookInfo();
books[3].Title = "Silverlight 2 Recipes";
books[3].Author = "Jit Ghosh, Rob Cameron";
books[3].isbn = "978-1-59059-977-8";
books[3].Url =
@"http://apress.com/book/view/9781590599778";
return books;
}
}
```
- **构建用户界面**:右键单击`Views`文件夹,添加一个新的`Page`控件,命名为`COMAccessDemo.xaml`,并使用以下代码替换默认的`LayoutRoot Grid`代码,添加两个按钮用于执行Excel和Word集成。
```xml
<StackPanel x:Name="LayoutRoot" Loaded="COMAccessDemo_Loaded" >
<sdk:DataGrid x:Name="BooksGrid" HorizontalAlignment="Left"
VerticalAlignment="Top" Width="700" />
<StackPanel Orientation="Horizontal" VerticalAlignment="Top" >
<Button x:Name="btnExportExcel" Content="Export to Excel"
Click="btnExportExcel_Click"
Height="23" Width="120" Margin="10" />
<Button x:Name="btnExportWord" Content="Export to Word"
Click="btnExportWord_Click" Height="23" Width="120" />
</StackPanel>
</StackPanel>
```
注意,对于`DataGrid`,可以从工具箱中拖动到设计界面,这样会自动创建所需的XML命名空间。若手动输入,则需要手动编写该命名空间并添加对SDK dll的项目引用。
```xml
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/
xaml/presentation/sdk"
```
- **设置数据绑定**:在代码隐藏文件中,在页面的`Loaded`事件中设置`BooksGrid`的`ItemsSource`以显示添加的书籍信息。
```csharp
void COMAccessDemo_Loaded(object sender, RoutedEventArgs e)
{
BooksGrid.ItemsSource = BookData.GetBooks();
}
```
- **实现导出功能**:
- **导出到Excel**:点击“Export to Excel”按钮时,创建一个可见的Microsoft Excel应用程序实例,并将`BooksGrid`的内容填充到工作簿中。
```csharp
private void btnExportExcel_Click(object sender, RoutedEventArgs e)
{
// create an instance of excel
dynamic excel = AutomationFactory.
CreateObject("Excel.Application");
// make it visible to the user.
excel.Visible = true;
// add a workbook to the instance
dynamic workbook = excel.workbooks.Add();
// get the active sheet
dynamic sheet = excel.ActiveSheet;
dynamic cell = null;
int i = 1;
// iterate through our data source and populate
//the excel spreadsheet
foreach (BookInfo item in BooksGrid.ItemsSource)
{
cell = sheet.Cells[i, 1]; // row, column
cell.Value = item.Title;
cell.ColumnWidth = 25;
cell = sheet.Cells[i, 2];
cell.Value = item.Author;
cell = sheet.Cells[i, 3];
cell.Value = item.isbn;
cell = sheet.Cells[i, 4];
cell.Value = item.Url;
i++;
}
}
```
- **导出到Word**:点击“Export to Word”按钮时,创建一个可见的Microsoft Word应用程序实例,并将`BooksGrid`的内容填充到Word表格中。
```csharp
private void btnExportWord_Click(object sender, RoutedEventArgs e)
{
// create an instance of excel
dynamic word = AutomationFactory.
CreateObject("Word.Application");
// make it visible to the user.
word.Visible = true;
// add a new Document to the instance
dynamic wordDocument = word.Documents.Add();
//setting up some properties for the Document
dynamic range = wordDocument.Range(0, 0);
dynamic table = wordDocument.Tables.Add(range, 5, 4);
//some property setting on table
table.ApplyStyleHeadingRows = true;
table.AllowAutoFit = true;
//setting header
table.Cell(1, 1).Range.Text = "Title";
table.Cell(1, 2).Range.Text = "Author";
table.Cell(1, 3).Range.Text = "ISBN";
table.Cell(1, 4).Range.Text = "URL";
int j = 2;
foreach (BookInfo item in BooksGrid.ItemsSource)
{
table.Cell(j, 1).Range.Text= item.Title;
table.Cell(j, 2).Range.Text = item.Author;
table.Cell(j, 3).Range.Text = item.isbn;
table.Cell(j, 4).Range.Text = item.Url;
j++;
}
}
```
在编写代码时,可能不会有智能感知支持,但可以使用Visual Studio对象浏览器来查看添加的Office库的各种成员,这有助于开发COM访问的代码。
#### 2. 命令支持
在实现业务逻辑与主用户界面/表示层的分离时,可以遵循MVC(模型 - 视图 - 控制器)和MVP(模型 - 视图 - 呈现器)等标准设计模式。在Sil
0
0
复制全文
相关推荐









