稳扎稳打Silverlight(45) - 4.0浏览器外运行(Out Of browser)之被信任的应用程序(Trusted Application)
作者: webabcd
介绍
Silverlight 4.0 OOB 之 被信任的应用程序:
概述 访问本地文件系统 调用 COM 接口 自定义窗口样式和行为
在线DEMO
http://www.cnblogs.com/webabcd/archive/2010/08/09/1795417.html
示例
1、关于“被信任的应用程序”的简要概述
Demo.xaml
代码 < navigation:Page x:Class ="Silverlight40.TrustedApplication.Demo"
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d ="http://schemas.microsoft.com/Expression/blend/2008"
xmlns:mc ="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:navigation ="clr-namespace:System.windows.Controls;assembly=System.windows.Controls.Navigation"
Title ="Other Page" >
< GrID x:name ="LayoutRoot" >
< StackPanel HorizontalAlignment ="left" >
< TextBlock margin ="5" Text ="被信任的应用程序 - 对浏览器外应用程序提升信任级别。在 Silverlight 项目上单击右键 -> 属性 -> Silverlight -> Out-of-browser Settings -> 选中 Require elevated trust when running outsIDe the browser" />
< TextBlock margin ="5" Text ="Application.Current.HasElevatedPermissions - 判断 OOB 模式中的应用程序是否为被信任的应用程序" />
< TextBlock margin ="5" Text ="无需策略文件即可跨域通信,TCP通信端口不再限制为4502-4534" />
< TextBlock margin ="5" Text ="全屏后,按 ESC 键应用程序将不会退出全屏模式" />
< TextBlock margin ="5" Text ="应用程序签名:使用工具 SignTool.exe" />
< TextBlock margin ="5" Text ="访问本地文件系统:详见本目录下的 AccessLocalfile.xaml" />
< TextBlock margin ="5" Text ="调用 COM 接口:详见本目录下的 COMDemo..xaml" />
< TextBlock margin ="5" Text ="自定义窗口:详见本解决方案的 CustomOOBWindow 项目" />
</ StackPanel >
</ GrID >
</ navigation:Page >
2、访问本地文件系统的 Demo
AccessLocalfile.xaml 代码 < navigation:Page x:Class ="Silverlight40.TrustedApplication.AccessLocalfile"
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d ="http://schemas.microsoft.com/Expression/blend/2008"
xmlns:mc ="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:navigation ="clr-namespace:System.windows.Controls;assembly=System.windows.Controls.Navigation"
Title ="AccessLocalfile Page" >
< GrID x:name ="LayoutRoot" >
< StackPanel HorizontalAlignment ="left" >
< TextBlock Text ="在不使用 OpenfileDialog 和 SavefileDialog 的情况下,访问 Mydocuments, MyMusic, MyPictures, MyVIDeos 文件夹" />
<!-- 显示“我的文档”中的文件夹列表 -->
< ListBox name ="ListBoxDirectory" HorizontalAlignment ="left" >
< ListBox.ItemTemplate >
< DataTemplate >
< StackPanel OrIEntation ="Horizontal" >
< TextBlock Text =" {Binding name} " margin ="1" />
</ StackPanel >
</ DataTemplate >
</ ListBox.ItemTemplate >
</ ListBox >
<!-- 显示“我的文档”中的文件列表 -->
< ListBox name ="ListBoxfile" HorizontalAlignment ="left" >
< ListBox.ItemTemplate >
< DataTemplate >
< StackPanel OrIEntation ="Horizontal" >
< TextBlock Text =" {Binding name} " margin ="1" />
</ StackPanel >
</ DataTemplate >
</ ListBox.ItemTemplate >
</ ListBox >
</ StackPanel >
</ GrID >
</ navigation:Page >
AccessLocalfile.xaml.cs 代码 /*
* 访问本地文件系统的 Demo,只能访问“我的文档”,“我的音乐”,“我的图片”,“我的视频” 文件夹
*/
using System;
using System.Collections.Generic;
using System.linq;
using System.Net;
using System.windows;
using System.windows.Controls;
using System.windows.documents;
using System.windows.input;
using System.windows.Media;
using System.windows.Media.Animation;
using System.windows.Shapes;
using System.windows.Navigation;
using System.IO;
namespace Silverlight40.TrustedApplication
{
public partial class AccessLocalfile : Page
{
public AccessLocalfile()
{
InitializeComponent();
}
protected overrIDe voID OnNavigatedTo(NavigationEventArgs e)
{
if ( ! App.Current.IsRunningOutOfbrowser)
{
MessageBox.Show( " 请在OOB中运行 " );
LayoutRoot.Visibility = Visibility.Collapsed;
}
// Application.Current.HasElevatedPermissions - 判断 OOB 模式中的应用程序是否为被信任的应用程序
if (Application.Current.HasElevatedPermissions)
{
/*
* GetFolderPath(Environment.SpecialFolder folder) - 获取指定的文件夹的路径
* Environment.SpecialFolder - windows *** 作系统中特殊文件夹的枚举(在被信任程序中使用)
* Environment.SpecialFolder.Mydocuments - 我的文档。可以在被信任程序中访问
* Environment.SpecialFolder.MyMusic - 我的音乐。可以在被信任程序中访问
* Environment.SpecialFolder.MyPictures - 我的图片。可以在被信任程序中访问
* Environment.SpecialFolder.MyVIDeos - 我的视频。可以在被信任程序中访问
* Environment.SpecialFolder 还有其他很多枚举值,详见文档
* 针对文件夹 *** 作和文件 *** 的类有:Directory, DirectoryInfo, file, fileInfo, fileStream, Path, StreamReader, StreamWriter
*/
string path = Environment.GetFolderPath(Environment.SpecialFolder.Mydocuments);
DirectoryInfo root = new DirectoryInfo(path);
// 我的文档中的文件夹
ListBoxDirectory.ItemsSource = root.EnumerateDirectorIEs();
// 我的文档中的文件
ListBoxfile.ItemsSource = root.Enumeratefiles();
}
}
}
}
3、通过调用 Excel API(COM 接口)的方式,将数据导出为 Excel 格式
COMDemo.xaml 代码 < navigation:Page x:Class ="Silverlight40.TrustedApplication.COMDemo"
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d ="http://schemas.microsoft.com/Expression/blend/2008"
xmlns:mc ="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:navigation ="clr-namespace:System.windows.Controls;assembly=System.windows.Controls.Navigation"
xmlns:sdk ="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
Title ="COMDemo Page" >
< GrID x:name ="LayoutRoot" >
< StackPanel HorizontalAlignment ="left" >
< button name ="btnExport" Content ="导出到 Excel" Click ="btnExport_Click" />
< sdk:DataGrID name ="dataGrID" HorizontalAlignment ="left" VerticalAlignment ="top" WIDth ="400" Height ="300" IsReadonly ="True" autoGenerateColumns ="True" />
</ StackPanel >
</ GrID >
</ navigation:Page >
COMDemo.xaml.cs 代码 /*
* 此例用于演示:通过调用 Excel API(COM 接口)的方式,将数据导出为 Excel 格式
*/
using System;
using System.Collections.Generic;
using System.linq;
using System.Net;
using System.windows;
using System.windows.Controls;
using System.windows.documents;
using System.windows.input;
using System.windows.Media;
using System.windows.Media.Animation;
using System.windows.Shapes;
using System.windows.Navigation;
using System.Collections.ObjectModel;
using System.Runtime.InteropServices.automation;
namespace Silverlight40.TrustedApplication
{
public partial class COMDemo : Page
{
public COMDemo()
{
InitializeComponent();
}
protected overrIDe voID OnNavigatedTo(NavigationEventArgs e)
{
if ( ! App.Current.IsRunningOutOfbrowser)
{
MessageBox.Show( " 请在 OOB 中运行 " );
LayoutRoot.Visibility = Visibility.Collapsed;
}
else if ( ! Application.Current.HasElevatedPermissions)
{
MessageBox.Show( " 请在 “被信任的应用程序” 中运行 " );
LayoutRoot.Visibility = Visibility.Collapsed;
}
dataGrID.ItemsSource = GetProducts();
}
private voID btnExport_Click( object sender, RoutedEventArgs e)
{
dynamic excelApp = typeof (Object);
try
{
/*
* automationFactory.IsAvailable - 获取一个值,该值表示 automationFactory 是否可用
* automationFactory.CreateObject() - 根据编程标识符(ProgID)激活指定的组件,并返回该引用
* automationFactory.Getobject() - 根据编程标识符(ProgID)获取已激活的并且当前正在运行的组件引用
*/
if ( ! automationFactory.IsAvailable)
{
MessageBox.Show( " 无法调用 COM 接口 " );
return ;
}
// 激活并获取 Excel.Application 引用
excelApp = automationFactory.CreateObject( " Excel.Application " );
}
catch (Exception ex)
{
MessageBox.Show( " 没有安装 Excel ? " + ex.Message);
}
excelApp.Visible = true ;
// 新开一个工作簿
dynamic workbook = excelApp.Workbooks;
workbook.Add();
// 当前工作表
dynamic sheet = excelApp.ActiveSheet;
dynamic cell = null ;
int index = 1 ;
foreach (Product p in dataGrID.ItemsSource)
{
cell = sheet.Cells[index, 1 ];
cell.Value = p.ProductID;
// 获取指定了行列的单元格
cell = sheet.Cells[index, 2 ];
// 单元格内要显示的内容
cell.Value = p.name;
// 设置单元格背景颜色为红色。这里的 16 位颜色值为 BGR
cell.Interior.color = 0x0000FF ;
cell = sheet.Cells[index, 3 ];
cell.Value = p.Price;
index ++ ;
}
}
// 数据源
private ObservableCollection < Product > GetProducts()
{
return new ObservableCollection < Product >
{
new Product{ ProductID = 1 , name = " Productname01 " , Price = 100.6m },
new Product{ ProductID = 2 , name = " Productname02 " , Price = 60.8m },
new Product{ ProductID = 3 , name = " Productname03 " , Price = 189.2m },
new Product{ ProductID = 4 , name = " Productname04 " , Price = 56m},
new Product{ ProductID = 5 , name = " Productname05 " , Price = 69m},
new Product{ ProductID = 6 , name = " Productname06 " , Price = 244m},
};
}
}
// 实体类
public class Product
{
public int ProductID { get ; set ; }
public string name { get ; set ; }
public decimal Price { get ; set ; }
}
}
4、自定义窗口样式和行为的 Demo(详见 CustomOOBWindow 项目)
generic.xaml 代码 < ResourceDictionary
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local ="clr-namespace:CustomOOBWindow" >
< Style targettype ="local:MyWindow" >
< Setter Property ="Template" >
< Setter.Value >
< ControlTemplate targettype ="local:MyWindow" >
< border x:name ="windowborder" borderBrush ="Green" Background ="Red" borderThickness ="2" CornerRadius ="5" >
< GrID x:name ="layoutRoot" >
<!-- 窗口的上下左右都留出 5 个像素的边框 -->
< GrID.ColumnDeFinitions >
< ColumnDeFinition WIDth ="5" />
< ColumnDeFinition WIDth ="*" />
< ColumnDeFinition WIDth ="5" />
</ GrID.ColumnDeFinitions >
< GrID.RowDeFinitions >
< RowDeFinition Height ="5" />
< RowDeFinition Height ="*" />
< RowDeFinition Height ="5" />
</ GrID.RowDeFinitions >
<!-- 在窗口的 4 个边和 4 个角上放置透明的矩形,用于当通过鼠标改变窗口大小时,鼠标移动到边或角上时改变鼠标的样式 -->
< GrID.Resources >
< Style targettype ="Rectangle" >
< Setter Property ="Fill" Value ="transparent" />
</ Style >
</ GrID.Resources >
< Rectangle x:name ="topleftCorner" GrID.Row ="0" GrID.Column ="0" Cursor ="SiZenWSE" />
< Rectangle x:name ="topEdge" GrID.Row ="0" GrID.Column ="1" Cursor ="SiZenS" />
< Rectangle x:name ="topRightCorner" GrID.Row ="0" GrID.Column ="2" Cursor ="SizenesW" />
< Rectangle x:name ="leftEdge" GrID.Row ="1" GrID.Column ="0" Cursor ="SizeWE" />
< Rectangle x:name ="rightEdge" GrID.Row ="1" GrID.Column ="2" Cursor ="SizeWE" />
< Rectangle x:name ="bottomleftCorner" GrID.Row ="2" GrID.Column ="0" Cursor ="SizenesW" />
< Rectangle x:name ="bottomEdge" GrID.Row ="2" GrID.Column ="1" Cursor ="SiZenS" />
< Rectangle x:name ="bottomrightCorner" GrID.Row ="2" GrID.Column ="2" Cursor ="SiZenWSE" />
< GrID GrID.Row ="1" GrID.Column ="1" >
< GrID.RowDeFinitions >
< RowDeFinition Height ="auto" />
< RowDeFinition Height ="*" />
</ GrID.RowDeFinitions >
< GrID x:name ="Titlebar" margin ="0,3" >
< GrID.Resources >
< Style targettype ="button" >
< Setter Property ="WIDth" Value ="20" />
< Setter Property ="Height" Value ="20" />
< Setter Property ="padding" Value ="0" />
</ Style >
</ GrID.Resources >
< GrID.ColumnDeFinitions >
< ColumnDeFinition WIDth ="auto" />
< ColumnDeFinition WIDth ="*" />
< ColumnDeFinition WIDth ="auto" />
< ColumnDeFinition WIDth ="auto" />
< ColumnDeFinition WIDth ="auto" />
< ColumnDeFinition WIDth ="auto" />
</ GrID.ColumnDeFinitions >
<!-- 窗口的图标和标题 -->
< Image x:name ="windowIcon" GrID.Column ="0" WIDth ="16" Height ="16" />
< TextBlock x:name ="Title" GrID.Column ="1" FontSize ="12" padding ="5,0" />
<!-- 总在最前按钮,最小化按钮,最大化按钮,关闭按钮 -->
< Togglebutton x:name ="pinbutton" GrID.Column ="2" WIDth ="80" Content ="总在最前" />
< button x:name ="minbutton" GrID.Column ="3" WIDth ="80" Content ="最小化" />
< Togglebutton x:name ="maxbutton" GrID.Column ="4" WIDth ="80" Content ="最大化" />
< button x:name ="closebutton" GrID.Column ="5" WIDth ="80" Content ="关闭" />
</ GrID >
<!--
ContentPresenter - 窗口内的内容
-->
< border Background ="White" GrID.Row ="1" borderBrush ="Blue" borderThickness ="2" >
< ContentPresenter />
</ border >
</ GrID >
</ GrID >
</ border >
</ ControlTemplate >
</ Setter.Value >
</ Setter >
</ Style >
</ ResourceDictionary >
MyWindow.cs 代码 /*
* 自定义 OOB 模式下的窗口样式和行为(需要配置为被信任的应用程序)
*/
using System;
using System.Net;
using System.windows;
using System.windows.Controls;
using System.windows.documents;
using System.windows.Ink;
using System.windows.input;
using System.windows.Media;
using System.windows.Media.Animation;
using System.windows.Shapes;
using System.ComponentModel;
using System.windows.Controls.Primitives;
namespace CustomOOBWindow
{
public class MyWindow : ContentControl
{
/*
* 在 Out-of-browser Settings 可以设置 Window Style。背景不可透明,所以不能创建不规则形状的应用程序
* Default - 默认样式
* No border - 无边框
* borderless Round Corners - 无边框,带圆角,圆角的角半径为 9 个像素
*/
private GrID layoutRoot;
private border windowborder;
private FrameworkElement Titlebar;
public MyWindow()
{
if (Application.Current.IsRunningOutOfbrowser && ! DesignerPropertIEs.IsInDesignTool)
{
/*
* Application.Current.MainWindow - OOB 模式下的应用程序窗口。属性值为 System.windows.Window 类型
* Window.WIDth - 窗口的宽
* Window.Height - 窗口的高
* Window.top - 窗口距离屏幕顶部的距离
* Window.left - 窗口距离屏幕左侧的距离
* Window.windowstate - 窗口状态 [System.windows.windowstate 枚举]
* windowstate.normal - 正常大小
* windowstate.Minimized - 最小化到任务栏
* windowstate.Maximized - 最大化,全屏
* Window.topMost - 窗口是否总在最前面
* Window.Activate() - 激活窗口,使之具有焦点,并置于最前面
* Window.IsActive - 是否为激活窗口(仅 get)
* Window.DragMove() - 实现用鼠标拖动窗口的功能
* Window.DragResize(WindowResizeEdge resizeEdge) - 根据指定的 WindowResizeEdge 改变窗口的边或角的位置,也就是调整窗口大小
* left, Right, top, topleft, topRight, Bottom, Bottomleft, Bottomright
*/
Window mainWindow = Application.Current.MainWindow;
mainWindow.WIDth = 640 ;
mainWindow.Height = 480 ;
// 在 Out-of-browser Settings 选中“Set window location manually”时,这里的设置才会生效
mainWindow.top = 100 ;
mainWindow.left = 100 ;
mainWindow.windowstate = windowstate.normal;
mainWindow.topMost = false ;
}
// 设置控件的默认样式
this .DefaultStyleKey = typeof (MyWindow);
}
// UI 元素在应用程序中显示之前所调用的方法
public overrIDe voID OnApplyTemplate()
{
base .OnApplyTemplate();
// Control.GetTemplateChild() - 在实例化的 ControlTemplate 可视化树中检索已命名的元素
layoutRoot = GetTemplateChild( " layoutRoot " ) as GrID;
windowborder = GetTemplateChild( " windowborder " ) as border;
Titlebar = GetTemplateChild( " Titlebar " ) as FrameworkElement;
InitializeTitlebar();
Initializebutton();
InitializeWindowborder();
}
private voID InitializeTitlebar()
{
TextBlock Title = GetTemplateChild( " Title " ) as TextBlock;
Title.Text = Deployment.Current.OutOfbrowserSettings.windowsettings.Title;
Image windowIcon = GetTemplateChild( " windowIcon " ) as Image;
// 设置窗口左上角的图标
// Deployment.Current.OutOfbrowserSettings - 获取 OOB 的相关配置信息
if (Deployment.Current.OutOfbrowserSettings.Icons.Count > 0 )
windowIcon.source = new System.windows.Media.Imaging.BitmAPImage( new Uri( " / " + Deployment.Current.OutOfbrowserSettings.Icons[ 0 ].source.ToString(), UriKind.relative));
else
windowIcon.Visibility = Visibility.Collapsed;
Titlebar.MouseleftbuttonDown += delegate { Application.Current.MainWindow.DragMove(); };
}
private voID Initializebutton()
{
Window mainWindow = Application.Current.MainWindow;
var pinbutton = GetTemplateChild( " pinbutton " ) as Togglebutton;
var minbutton = GetTemplateChild( " minbutton " ) as button;
var maxbutton = GetTemplateChild( " maxbutton " ) as Togglebutton;
var closebutton = GetTemplateChild( " closebutton " ) as button;
pinbutton.Checked += delegate { mainWindow.topMost = true ; pinbutton.Content = " 常规窗口 " ; };
pinbutton.Unchecked += delegate { mainWindow.topMost = false ; pinbutton.Content = " 总在最前 " ; };
minbutton.Click += delegate { mainWindow.windowstate = windowstate.Minimized; };
maxbutton.Checked += delegate { mainWindow.windowstate = windowstate.Maximized; maxbutton.Content = " 正常化 " ; };
maxbutton.Unchecked += delegate { mainWindow.windowstate = windowstate.normal; maxbutton.Content = " 最大化 " ; };
closebutton.Click += delegate { mainWindow.Close(); };
}
private voID InitializeWindowborder()
{
Window mainWindow = Application.Current.MainWindow;
UIElement topleftCorner = GetTemplateChild( " topleftCorner " ) as UIElement;
UIElement topEdge = GetTemplateChild( " topEdge " ) as UIElement;
UIElement topRightCorner = GetTemplateChild( " topRightCorner " ) as UIElement;
UIElement leftEdge = GetTemplateChild( " leftEdge " ) as UIElement;
UIElement rightEdge = GetTemplateChild( " rightEdge " ) as UIElement;
UIElement bottomleftCorner = GetTemplateChild( " bottomleftCorner " ) as UIElement;
UIElement bottomEdge = GetTemplateChild( " bottomEdge " ) as UIElement;
UIElement bottomrightCorner = GetTemplateChild( " bottomrightCorner " ) as UIElement;
topleftCorner.MouseleftbuttonDown += delegate { mainWindow.DragResize(WindowResizeEdge.topleft); };
topEdge.MouseleftbuttonDown += delegate { mainWindow.DragResize(WindowResizeEdge.top); };
topRightCorner.MouseleftbuttonDown += delegate { mainWindow.DragResize(WindowResizeEdge.topRight); };
leftEdge.MouseleftbuttonDown += delegate { mainWindow.DragResize(WindowResizeEdge.left); };
rightEdge.MouseleftbuttonDown += delegate { mainWindow.DragResize(WindowResizeEdge.Right); };
bottomleftCorner.MouseleftbuttonDown += delegate { mainWindow.DragResize(WindowResizeEdge.Bottomleft); };
bottomEdge.MouseleftbuttonDown += delegate { mainWindow.DragResize(WindowResizeEdge.Bottom); };
bottomrightCorner.MouseleftbuttonDown += delegate { mainWindow.DragResize(WindowResizeEdge.Bottomright); };
}
}
}
MainPage.xaml 代码 < window:MyWindow x:Class ="CustomOOBWindow.MainPage"
xmlns:window ="clr-namespace:CustomOOBWindow;assembly=CustomOOBWindow"
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d ="http://schemas.microsoft.com/Expression/blend/2008"
xmlns:mc ="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable ="d"
d:DesignHeight ="300" d:DesignWIDth ="400" >
< GrID x:name ="LayoutRoot" Background ="White" >
< TextBlock Text ="自定义 OOB 窗口的 Demo,需要设置为被信任的应用程序" />
</ GrID >
</ window:MyWindow >
OK
[源码下载] 总结
以上是内存溢出为你收集整理的稳扎稳打Silverlight(45) - 4.0浏览器外运行(Out Of Browser)之被信任的应用程序(Trusted Application)全部内容,希望文章能够帮你解决稳扎稳打Silverlight(45) - 4.0浏览器外运行(Out Of Browser)之被信任的应用程序(Trusted Application)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)