
本文简介:
1.为什么要使用OOB?使用OOB的作用?
2.如何实现OOB模式
3.对OOB进行一些设置;
4.检测OOB的安装状态;
5.更新应用程序;
6.Webbrowser控件;
7.桌面通知窗口;
8.记录窗体的状态;
9.提升信任;
10.子窗体(非ChilDWindow,同时可响应的多个窗体);
11.Com组件访问;
12.在浏览器中提升信任。
1.理解Oob
实现OOB之后将有如下的作用:
(1)获得一个桌面的快捷方式;
(2)可以实现离线访问(当然要结合本地存储);
(3)获得更高的信任权限。
2.实现一个最简单的OOB应用程序
(1)右键Silverlight应用程序选择Silverlight选项卡;
(2)选中"Enable running application out of the browser"复选框;
(3)点击"Out-of-browser Settings"(OOB设置)按钮,如下:
可以在Title中输入自己想要的标题即可。
这样一个最简单的OOB程序就创建好了,赶紧运行看看吧。
3.自定义应用程序的图标
在上图中的Icon填写处可以选择一个自定义的图片来作为安装后的快捷方式的图标。
4.检测OOB的安装状态
一般我们通过Application.Current.IsRunningOutOfbrowser来判断当前的程序是否是以OOB模式运行的,当然也可以判断当前OOB安装的状态; Application.Current.InstallState表示当前OOB安装的状态,一共四个值:Not Installed(未安装),Installing(安装中),InstallFailed(安装失败),Installed(已安装)。
在APP中添加如下代码,以检测安装状态的变化:
this.InstallStateChanged += App_InstallStateChanged; 事件代码如下:
InstallPage page = this.RootVisual as InstallPage; if (page != null) { // Tell the root visual to show a message by calling a method // in InstallPage that updates the display. switch (this.InstallState) { case InstallState.InstallFailed: page.displayFailed(); break; case InstallState.Installed: page.displayInstalled(); break; } }
InstallPage为自定义安装界面,可自行实现:
InstallPage.Xaml:
<border margin="10" padding="10" Background="lightSteelBlue" borderBrush="SteelBlue" borderThickness="1" CornerRadius="3" HorizontalAlignment="left" VerticalAlignment="top" MaxWIDth="400"> <GrID x:name="LayoutRoot"> <GrID.RowDeFinitions> <RowDeFinition Height="auto"></RowDeFinition> <RowDeFinition Height="auto"></RowDeFinition> </GrID.RowDeFinitions> <TextBlock margin="5" x:name="lblMessage" textwrapPing="Wrap">You need to install this application to run it.</TextBlock> <button x:name="cmdInstall" GrID.Row="1" Content="Install" HorizontalAlignment="Center" padding="10" margin="15" Click="cmdInstall_Click"></button> </GrID> </border>
InstallPage.Xaml.cs:
public InstallPage() { InitializeComponent(); if (Application.Current.InstallState == InstallState.Installed) { lblMessage.Text = "This application is already installed. " + "You cannot use the browser to run it. " + "Instead,use the shortcut on your computer."; cmdInstall.IsEnabled = false; } } private voID cmdInstall_Click(object sender,RoutedEventArgs e) { // Make sure that the application is not already installed. if (Application.Current.InstallState != InstallState.Installed) { // Attempt to install it. bool installAccepted = Application.Current.Install(); if (!installAccepted) { lblMessage.Text = "You declined the install. Click Install to try again."; } else { cmdInstall.IsEnabled = false; lblMessage.Text = "The application is installing... "; } } } public voID displayInstalled() { lblMessage.Text = "The application installed and launched. You can close this page."; } public voID displayFailed() { lblMessage.Text = "The application Failed to install."; cmdInstall.IsEnabled = true; }
5.更新OOB应用程序
更新OOB应用程序
private voID Application_Startup(object sender,StartupEventArgs e) { if (Application.Current.IsRunningOutOfbrowser) { // Check for updates. Application.Current.CheckAndDownloadUpdateCompleted += Application_CheckAndDownloadUpdateCompleted; Application.Current.CheckAndDownloadUpdateAsync(); this.RootVisual = new MainPage(); } else { this.RootVisual = new InstallPage(); } }
上述代码中添加了CheckAndDownloadUpdateCompleted 回调函数,即检查和下载应用程序之后执行。
voID Current_CheckAndDownloadUpdateCompleted(object sender,CheckAndDownloadUpdateCompletedEventArgs e) { if (e.UpdateAvailable) { MessageBox.Show("发现了更新,请重新登陆!"); } else if (e.Error != null) { if (e.Error is PlatformNotSupportedException) { MessageBox.Show("哦,我去,出现了平台错误,需要一个新版本的Silverlight,请访问 " + " http://silverlight.net 进行更新."); } else { MessageBox.Show("发现了应用程序更新,但是更新出现了错误。请删除当前的版本,然后重新安装新版本。 "); } } }
6.Webbrowser控件的使用
只需设置Source属性即可,切记要写上http://.
<Webbrowser Source="http://www.baIDu.com"></Webbrowser>
当你在Silverlight中使用Webbrowser(请注意不是OOB),那么将会提示你Webbrowser只能在Silverlight提升了权限后才可以在浏览器内显示,那么就需要去属性中将"Require evevated trust when running in-browser"选中即可。
执行脚本方法:
browser.InvokeScript("AddTwoNumbers",new string[] { "1","2" });
调用Webbrowser的InvokeScript方法,第一个参数为函数名称,第二个参数为脚本函数的参数。
7.桌面通知窗口
即在windows桌面的右下角d出窗口,需要两步骤:
(1)创建一个自定义通知窗体即一个UserControl
<GrID> <GrID.RowDeFinitions> <RowDeFinition Height="auto"></RowDeFinition> <RowDeFinition></RowDeFinition> </GrID.RowDeFinitions> <border Background="lightGray" Height="20"> <TextBlock margin="3" FontSize="10" Text="MyApplication Notification"></TextBlock> </border> <border GrID.Row="1"> <border.Background> <linearGradIEntBrush> <linearGradIEntBrush.GradIEntStops> <GradIEntStop color="DarkOrange"></GradIEntStop> <GradIEntStop color="Crimson" Offset="0.3"></GradIEntStop> <GradIEntStop color="DeepPink" Offset="0.5"></GradIEntStop> <GradIEntStop color="DarkOrange" Offset="0.7"></GradIEntStop> <GradIEntStop color="Crimson" Offset="1"></GradIEntStop> </linearGradIEntBrush.GradIEntStops> </linearGradIEntBrush> </border.Background> <TextBlock x:name="lblMessage" margin="10" FontWeight="Bold" FontSize="16" Foreground="White" textwrapPing="Wrap" HorizontalAlignment="Center" VerticalAlignment="Center">Notification goes here.</TextBlock> </border> </GrID>
代码比较简单,就是两行,第一行为一个TextBlock,第二行为一个border,里边也是一个TextBlock,但是在后置代码中对TextBlock的Text进行了修改。
public string Message { get { return lblMessage.Text; } set { lblMessage.Text = value; } }
后置代码就这一句即可。
(2)修改App.xaml.cs代码
if (Application.Current.IsRunningOutOfbrowser == true) { CustomNotification notification = new CustomNotification(); notification.Message = "You have just been notifIEd. The time is " + DateTime.Now.TolongTimeString() + "."; NotificationWindow window = new NotificationWindow(); window.Content = notification; window.Show(5000); }
在Application_Startup中加入以上代码即可,可以看到对Message属性就行了赋值,显示当前的时间,这样就可以在右下角显示通知窗口。
8.记录窗体的状态
有时候用户希望保存下来最后窗体的状态(top,left, Height,WIDth, windowstate, topMost),那么通过以下代码即可实现保存和读取这些值,当然这里用到了独立存 储,不了解的童鞋自行去复习哦。
(1)读取配置,还是在Application_Startup事件中加入如下代码
if (Application.Current.IsRunningOutOfbrowser) { IsolatedStoragefile store = IsolatedStoragefile.GetUserStoreForApplication(); if (store.fileExists("window.Settings")) { using (IsolatedStoragefileStream fs = store.Openfile("window.Settings",fileMode.Open)) { BinaryReader r = new BinaryReader(fs); Application.Current.MainWindow.top = r.ReadDouble(); Application.Current.MainWindow.left = r.ReadDouble(); Application.Current.MainWindow.WIDth = r.ReadDouble(); Application.Current.MainWindow.Height = r.ReadDouble(); r.Close(); } } }
(2)保存配置
private voID Application_Exit(object sender,EventArgs e) { if (Application.Current.IsRunningOutOfbrowser) { // Store window state. try { IsolatedStoragefile store = IsolatedStoragefile.GetUserStoreForApplication(); using (IsolatedStoragefileStream fs = store.Createfile("window.Settings")) { BinaryWriter w = new BinaryWriter(fs); w.Write(Application.Current.MainWindow.top); w.Write(Application.Current.MainWindow.left); w.Write(Application.Current.MainWindow.WIDth); w.Write(Application.Current.MainWindow.Height); w.Close(); } } catch (Exception err) { } } }
9.提升Silverlight应用程序信任
之所以要提升信任,最主要的原因是,不提升信任的情况下OOB程序的更新会出现问题,所以微软给提升了信任的应用程序提供了这样的功能,但是当提升了信任之后,我们需要做一个签名(虽然VS提供了,但是只是Test的,so...),这就要花钱购买了哦,在此不多说。
右键Silverlight应用程序,在属性中,点击OOB设置,勾上"Require elevated trust when running outsIDe the browser"即可。
这时候在安装程序到本地的时候提示如下,可以看到发布者是未验证,也就是我们木有签名哦。
当然提升信任权限的原因不仅仅是上述的原因,提升后得到的功能如下:
(1)对文件的 *** 作,如果不提升则只能 *** 作 我的文档/我的图片/我的视频这几个文件夹,当提升后就可以像Winform那样 *** 作本地的磁盘;
(2)无限制的全屏支持,再也不用看到"Enter esc to ..";
(3)无限制的访问WebService,即不会再出现跨域的情况;
(4)更少的用户同意限制,即当访问剪切板或者访问独立存储等功能的时候更少的显示通知用户是否同意;
(5)自定义窗体,如果你不喜欢窗体的默认border这样的东西,在信任的程序中你可以自定义,下文会提到;
(6)字窗体,如果是信任的程序则可以打开第二个字窗体;
(7)Com组件交互;
(8)平台调用 (P/Invoke),可以访问windows API等。
9.1制作一个自定义的窗体
无边框的窗体,分为两步。第一步,右键Silverlight应用程序,然后属性,然后到OOB设置,在windows Style列表中选择"No border"即可,这样就可以看到一个无边框的东西哦。
第二步,可以给窗体加一个边框,在窗体的根节点(默认应该是GrID)外放一个border,如下,这样就有了一个边框。
<border x:name="windowFrame" borderBrush="DarkSlateBlue" borderThickness="5" CornerRadius="2" margin="0,1,1">
虽然看起来蛮酷的,可是发现无法对窗体进行拖动和大小的修改,所以在 *** 作中还是有点问题的。
9.2 自定义窗体进阶
第一步,给窗体制作一个工具栏
<border x:name="Titlebar" Background="lightSteelBlue" MouseleftbuttonDown="Titlebar_MouseleftbuttonDown"> <GrID> <GrID.ColumnDeFinitions> <ColumnDeFinition></ColumnDeFinition> <ColumnDeFinition WIDth="auto"></ColumnDeFinition> <ColumnDeFinition WIDth="auto"></ColumnDeFinition> <ColumnDeFinition WIDth="auto"></ColumnDeFinition> </GrID.ColumnDeFinitions> <TextBlock margin="5">Title bar</TextBlock> <button GrID.Column="1" x:name="cmdMinimize" WIDth="24" Click="cmdMinimize_Click"> <Path stroke="Black" strokeThickness="4" Data="M 1,10 L 13,10" /> </button> <button GrID.Column="2" x:name="cmdMaximize" WIDth="24" Click="cmdMaximize_Click"> <Rectangle stroke="Black" strokeThickness="3" Height="12" WIDth="12" /> </button> <button GrID.Column="3" x:name="cmdClose" WIDth="24" Click="cmdClose_Click"> <Path stroke="Black" strokeThickness="3" Data="M 2,2 L 12,12 M 12,2 L 2,12" /> </button> </GrID> </border>
private Window currentwindow; public Window Currentwindow { get { if (currentwindow != null) { // This is being used as a secondary (child) window. return currentwindow; } else { // This is being used as a main window (or as a child window,incorrectly). return Application.Current.MainWindow; } } set { currentwindow = value; } } private voID Titlebar_MouseleftbuttonDown(object sender,System.windows.input.MousebuttonEventArgs e) { Currentwindow.DragMove(); } private voID cmdMinimize_Click(object sender,System.windows.RoutedEventArgs e) { Currentwindow.windowstate = windowstate.Minimized; } private voID cmdMaximize_Click(object sender,System.windows.RoutedEventArgs e) { if (Currentwindow.windowstate == windowstate.normal) { Currentwindow.windowstate = windowstate.Maximized; } else { Currentwindow.windowstate = windowstate.normal; } } private voID cmdClose_Click(object sender,System.windows.RoutedEventArgs e) { Currentwindow.Close(); }
第二步,修改刚才我们的代码,即在border中放一个StackPanel或者放一个两行的GrID,使工具栏和内容分别位于上下位置即可,效果如下。
这样可以在工具栏上按下左键进行拖动,也可以通过三个按钮进行最小化,最大化,关闭 *** 作,此时看起来还算一个不错的自定义窗体,但是你会发现无法进行ReSize,接下来进行ReSize的处理。
上图就是一个可进行Resize效果的部件组成,在Content内容的四周均为Rectangle,当然这些Rectangle都是可以进行拉伸和缩放的,代码如下:
<border x:name="windowFrame" borderBrush="DarkSlateBlue" borderThickness="5" CornerRadius="2" margin="0,1"> <GrID x:name="resizeContainer"> <GrID.ColumnDeFinitions> <ColumnDeFinition WIDth="5"></ColumnDeFinition> <ColumnDeFinition WIDth="*"></ColumnDeFinition> <ColumnDeFinition WIDth="5"></ColumnDeFinition> </GrID.ColumnDeFinitions> <GrID.RowDeFinitions> <RowDeFinition Height="5"></RowDeFinition> <RowDeFinition Height="*"></RowDeFinition> <RowDeFinition Height="5"></RowDeFinition> </GrID.RowDeFinitions> <Rectangle x:name="rect_topleftCorner" GrID.Row="0" GrID.Column="0" Cursor="SiZenWSE" Fill="transparent" MouseleftbuttonDown="rect_Resize" /> <Rectangle x:name="rect_topEdge" GrID.Row="0" GrID.Column="1" Cursor="SiZenS" Fill="transparent" MouseleftbuttonDown="rect_Resize" /> <Rectangle x:name="rect_topRightCorner" GrID.Row="0" GrID.Column="2" Cursor="SizenesW" Fill="transparent" MouseleftbuttonDown="rect_Resize" /> <Rectangle x:name="rect_leftEdge" GrID.Row="1" GrID.Column="0" Cursor="SizeWE" Fill="transparent" MouseleftbuttonDown="rect_Resize" /> <Rectangle x:name="rect_RightEdge" GrID.Row="1" GrID.Column="2" Cursor="SizeWE" Fill="transparent" MouseleftbuttonDown="rect_Resize" /> <Rectangle x:name="rect_BottomleftCorner" GrID.Row="2" GrID.Column="0" Cursor="SizenesW" Fill="transparent" MouseleftbuttonDown="rect_Resize" /> <Rectangle x:name="rect_BottomEdge" GrID.Row="2" GrID.Column="1" Cursor="SiZenS" Fill="transparent" MouseleftbuttonDown="rect_Resize" /> <Rectangle x:name="rect_BottomrightCorner" GrID.Row="2" GrID.Column="2" Cursor="SiZenWSE" Fill="transparent" MouseleftbuttonDown="rect_Resize" /> <StackPanel GrID.Row="1" GrID.Column="1"> <border x:name="Titlebar" Background="lightSteelBlue" MouseleftbuttonDown="Titlebar_MouseleftbuttonDown"> <GrID> <GrID.ColumnDeFinitions> <ColumnDeFinition></ColumnDeFinition> <ColumnDeFinition WIDth="auto"></ColumnDeFinition> <ColumnDeFinition WIDth="auto"></ColumnDeFinition> <ColumnDeFinition WIDth="auto"></ColumnDeFinition> </GrID.ColumnDeFinitions> <TextBlock margin="5">Title bar</TextBlock> <button GrID.Column="1" x:name="cmdMinimize" WIDth="24" Click="cmdMinimize_Click"> <Path stroke="Black" strokeThickness="4" Data="M 1,10" /> </button> <button GrID.Column="2" x:name="cmdMaximize" WIDth="24" Click="cmdMaximize_Click"> <Rectangle stroke="Black" strokeThickness="3" Height="12" WIDth="12" /> </button> <button GrID.Column="3" x:name="cmdClose" WIDth="24" Click="cmdClose_Click"> <Path stroke="Black" strokeThickness="3" Data="M 2,12" /> </button> </GrID> </border> <GrID x:name="LayoutRoot" Background="White"> <GrID.RowDeFinitions> <RowDeFinition Height="*"></RowDeFinition> </GrID.RowDeFinitions> <Image Source="2.jpg"></Image> </GrID> </StackPanel> </GrID> </border>
在外围的border中有一个GrID,共为三行和三列,第一行和第三行以及第一列和第二列均为Rectangle的位置,而中间区域为内容,另外为每一个Rectangle添加了鼠标按下左键事件,代码如下:
private voID rect_Resize(object sender,MousebuttonEventArgs e) { if (sender == rect_topleftCorner) { Application.Current.MainWindow.DragResize(WindowResizeEdge.topleft); } else if (sender == rect_topEdge) { Application.Current.MainWindow.DragResize(WindowResizeEdge.top); } else if (sender == rect_topRightCorner) { Application.Current.MainWindow.DragResize(WindowResizeEdge.topRight); } else if (sender == rect_leftEdge) { Application.Current.MainWindow.DragResize(WindowResizeEdge.left); } else if (sender == rect_RightEdge) { Application.Current.MainWindow.DragResize(WindowResizeEdge.Right); } else if (sender == rect_BottomleftCorner) { Application.Current.MainWindow.DragResize(WindowResizeEdge.Bottomleft); } else if (sender == rect_BottomEdge) { Application.Current.MainWindow.DragResize(WindowResizeEdge.Bottom); } else if (sender == rect_BottomrightCorner) { Application.Current.MainWindow.DragResize(WindowResizeEdge.Bottomright); } }
效果图对比如下:
默认大小。
手动调整后的大小。
10.子窗体
在普通的Silverlight程序中我们可以通过ChilDWindow来实现子窗体,可是仍然是只有一个窗体可响应,在这里要说的子窗体是可以同时响应的,效果如下:
最下边的窗体为主窗体,顶层的两个为创建的子窗体,怎样是不是很像winform中的那样呢,开始进入代码:
主窗体代码:
<GrID x:name="LayoutRoot" Background="White"> <StackPanel margin="20"> <button x:name="btnCreateBasicWindow" Content="创建一个简单的子窗体" margin="2" Click="btnCreateBasicWindow_Click"></button> <button x:name="btnCreateFancyWindow" Content="创建一个自定义的子窗体" margin="2" Click="btnCreateFancyWindow_Click"></button> <button x:name="btnCloseWindow" Content="关闭窗体" margin="2" Click="btnCloseWindow_Click"></button> </StackPanel> </GrID>
后置代码:
private Window winSimple; private Window winFancy; private voID btnCreateBasicWindow_Click(object sender,RoutedEventArgs e) { GrID grID = new GrID(); grID.Background = new SolIDcolorBrush(colors.White); TextBlock textContent = new TextBlock(); textContent.Text = "Here's some content!"; grID.Children.Add(textContent); winSimple = new Window(); winSimple.Content = grID; winSimple.WIDth = 300; winSimple.Height = 100; winSimple.Title = "Simple Window"; winSimple.Visibility = Visibility.Visible; } private voID btnCreateFancyWindow_Click(object sender,RoutedEventArgs e) { winFancy = new Window(); MainPage userControl = new MainPage(); winFancy.WIDth = 200; winFancy.Height = 300; winFancy.Content = userControl; winFancy.Title = "Custom Window"; //winStandard.Show();和下述代码效果一样 winFancy.Visibility = Visibility.Visible; } private voID btnCloseWindow_Click(object sender,RoutedEventArgs e) { winSimple.Visibility = System.windows.Visibility.Collapsed; winFancy.Visibility = System.windows.Visibility.Collapsed; }
代码原理很简单,手动创建一个Window,然后将其Content设置为创建的Usercontrol,然后设置下高度和宽度,最后要让其Visible,这样就ok了;关闭也就是让其Collapsed。
问题来了,当你d出自定义的窗体之后,并且试图去进行Resize的时候,你会发现效果并不是之前设想的,拖动之后改变的大小是主窗体的而非自己的,这是因为我们在窗体中修改的大小是Application.Current.MainWindow,所以改变的就是主窗体了,当然我们可以进行修改,如下:
winFancy = new Window(); MainPage userControl = new MainPage(); userControl.Currentwindow = winFancy; winFancy.WIDth = 300; winFancy.Height = 200; winFancy.Content = userControl; winFancy.Title = "Custom Window"; //winStandard.Show();和下述代码效果一样 winFancy.Visibility = Visibility.Visible;
改动只有两处,一处就是上述代码中添加了"userControl.Currentwindow = winFancy;",Currentwindow,为我们在自定义窗体中定义的属性,表示当前的主窗体,将主窗体设置为外围的容器窗体,这样进行Resize就可以同步了哦。
11.Com组件的访问
下面是一个访问Word的Com组件调用方式:
using (dynamic word = automationFactory.CreateObject("Word.Application")) { dynamic document = word.documents.Add(); dynamic paragraph = document.Content.Paragraphs.Add; paragraph.Range.Text = "heading 1"; paragraph.Range.Font.Bold = true; paragraph.Format.SpaceAfter = 18; paragraph.Range.InsertParagraphAfter(); paragraph = document.Content.Paragraphs.Add; paragraph.Range.Font.Bold = false; paragraph.Range.Text = "This is some more text"; word.Visible = true; }
下面这个是访问计算器的一个Com组件:
using (dynamic shell = automationFactory.CreateObject("WScript.Shell")) { shell.Run("calc.exe"); }
12.在浏览器中提升权限
有时候我们希望在浏览器中也是可以 *** 作桌面的一些功能,比如上述中德 *** 作Com组件,那么通过提升权限也是可以做到的,并不需要一定是OOB模式。
好了,到此 OOB下的一些事情说得挺多的,当然还有很多没有提到,大家可以多搜索,多总结,多提意见和建议。
总结以上是内存溢出为你收集整理的Silverlight之OOB模式下的一些事全部内容,希望文章能够帮你解决Silverlight之OOB模式下的一些事所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)