在前文,我们讲述了Silverlight Out of browser的基础以及自定义模式应用。本篇,我们将讲述
Silverlight Out of browser应用的重点 - 创建可信任应用,也称为Trusted Application. 早在Silverlight 3,Silverlight Out of browser的功能由于权限的限制无法很好的满足用户的正常存取需求,仅能实现将Web应用脱离浏览器。而在Silverlight 4中,通过提升应用信任权限,大大增强了Silverlight Out of browser的功能,在权限允许的情况下,用户可以自由有访问本地目录,也可以执行本地应用程序,另外通过调用COM组件,实现更多更强大的本地应用操作。下面我们将实例讲述Silverlight Out of browser可信任应用 - 存取本地文件系统。
本篇中,我们将基于上篇教程提供的项目SilverlightOOBDemo进行演示操作。
Silverlight 4对于本地文件夹的存取,并非代表存取所有本地磁盘目录,目前为止,Silverlight 4 API仅支持存取“我的文档”,“我的音乐”,“我的图片”和“我的视频”目录以及“Program Files”和“Cookies”目录,而如果想对所有磁盘目录进行访问,则需要使用COM功能进行操作,我们将在下篇讲述,本篇将着重讲述Silverlight 4 API对“我的”系列目录的操作方法。
1
<
UserControl
x:Class
="SilverlightOOBDemo.ThumbImage"
2 xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d ="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc ="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 mc:Ignorable ="d" 7 d:DesignHeight ="300" d:DesignWidth ="400" > 8 9 Grid x:Name ="LayoutRoot" Margin ="10" 10 Image ="ThumbnailImage" Height ="145" Width ="225" /> 11 </ Grid 12 UserControl >
2 xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d ="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc ="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 mc:Ignorable ="d" 7 d:DesignHeight ="300" d:DesignWidth ="400" > 8 9 Grid x:Name ="LayoutRoot" Margin ="10" 10 Image ="ThumbnailImage" Height ="145" Width ="225" /> 11 </ Grid 12 UserControl >
namespace
SilverlightOOBDemo
2 {
3 public partial class ThumbImage : UserControl
4 {
5 private Image _OriginalImage;
6 Image OriginalImage
7 {
get
{
return _OriginalImage;
}
set 13 14 this ._OriginalImage = value;
15 ThumbnailImage.source new WriteableBitmap(_OriginalImage, null );
16 17 }
18 19 ThumbImage()
20 {
21 InitializeComponent();
22 23 }
24 }
2 {
3 public partial class ThumbImage : UserControl
4 {
5 private Image _OriginalImage;
6 Image OriginalImage
7 {
get
{
return _OriginalImage;
}
set 13 14 this ._OriginalImage = value;
15 ThumbnailImage.source new WriteableBitmap(_OriginalImage, null );
16 17 }
18 19 ThumbImage()
20 {
21 InitializeComponent();
22 23 }
24 }
为了能够激活存取事件,我们需要
在OutofbrowserMainpage主窗口页面添加按钮控件,其样式调用自资源文件Resources.xaml,对于资源样式调用,这里不再赘述,如果不明白的,请看“Expression Blend实例中文教程系列文章”
StackPanel
="toolbar"
Background
="#FF3A3A3A"
Grid.ColumnSpan
="2"
Orientation
="Horizontal"
="0,1"
Grid.Row
="0"
Border
BorderBrush
="
{StaticResource GlossyBlack_strokeGradient}
"
BorderThickness
="1"
CornerRadius
Padding
StackPanel
Orientation
="Horizontal"
Button
Width
="56"
="80"
Style
{StaticResource BlackGlossyButton}
="1,0"
Foreground
="White"
x:Name
="openFileBtn"
Click
="openFileBtn_Click"
Button.Content
VerticalAlignment
="Top"
HorizontalAlignment
="Center"
Source
="/SilverlightOOBDemo;component/Images/Open.png"
Stretch
="None"
TextBlock
="Bottom"
Text
="浏览"
textwrapping
="Wrap"
Button
Border
IsTabStop
="False"
x:Name
="aboutBtn"
=""
="?"
FontSize
="20"
FontWeight
="Bold"
="DarkCyan"
Margin
25
="帮助"
26
27
28
29
30
31
32
同时,为了能够载入本地“我的图片”目录中的图片文件,我们需要在OutofbrowserMainpage中使用一个ListBox控件,载入上面我们创建的ThumbImage控件来显示,所有图片略缩图列表,
1
<
ListBox
Grid.Column
="0"
x:Name
="lsMyPictures"
SelectionMode
="Single"
Style
="
{StaticResource GlossyBlackListBox}
"
ItemContainerStyle
{StaticResource ListBoxItemStyle}
BorderBrush
="Transparent"
Background
ScrollViewer.HorizontalScrollBarVisibility
="Auto"
ScrollViewer.VerticalScrollBarVisibility
Margin
="3,10,30"
></
ListBox
>
void
openFileBtn_Click(
object
sender, RoutedEventArgs e)
if (Application.Current.HasElevatedPermissions)
var imageFiles Directory.EnumerateFiles(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), " *.jpg " , SearchOption.AllDirectories);
foreach (var imagePath in imageFiles)
{
AddImagetoList( FileInfo(imagePath));
}
}
}
if (Application.Current.HasElevatedPermissions)
var imageFiles Directory.EnumerateFiles(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), " *.jpg " , SearchOption.AllDirectories);
foreach (var imagePath in imageFiles)
{
AddImagetoList( FileInfo(imagePath));
}
}
}