微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Silverlight之out of Browser模式

摘要:在Silverlight中所有应用都可以以一种叫做OOB(Out of browser)模式的方式脱离浏览器运行,在OOB模式下应用程序将获得更多的信任,甚至可以和windows api通信,今天就来看一下如何构建OOB应用。

主要内容

  1. 什么是OOB
  2. 构建OOB应用
  3. 自定义OOB窗口
  4. OOB应用升级

一、什么是OOB

Silverlight out of browser从字面理解就是脱离浏览器的应用,是可以安装到本地的运行在浏览器外的应用,是一个具有独立窗口的web应用。在OOB模式下silverlight看起来更像是C/S应用,但是它却具有web应用的特性。这样一来就可以让用户像在C/S系统中一样体验绚丽、丰富的Web特性。相比浏览其中运行silverlight来讲,oob除了运行方式不同之外,还会获得更多的权限信任、更多的本地化内容,这样以来很多浏览器中很难做到甚至无法做到的事情在oob中也同样可以实现。目前很多silverlight应用都支持oob模式,例如pptv剧场版就是一个很好的案例:

PPTVOOB

二、构建OOB应用

在Visual Studio中构建OOB应用很简单,只需要简单几步设置即可完成。在此之前先看一下没有对项目做OOB设置之前的情况。

简单建一个silverlight应用,运行之后的状态如下:

NoOOB

点击右键发现当前只有一个菜单

NoOOBContextMenu

OK,现在对项目进行OOB设置:

在项目中右键Properties,进入Silverlight选项,点击Enable running application out of browser,此时下面的"Out of browser Setting"按钮将变成可用状态,点击此按钮弹出下面的设置窗口:

OOBSettingUI

然后做如下设置,这里设置了窗口大小、所需图标以及Trust权限(选中该项后安装时需要用户确认):

OOBSeted

图标:

cmjLogo16

  

cmjLogo32

 

cmjLogo48

 

cmjLogo128

设置完成后可以直接启动此项目(SilverlightApplicationOOB)进行调试而不必在对于的web项目(SilverlightApplicationOOB.Web)中运行,效果如下:

OOBModeDubug

这里为了对比不妨在浏览器中查看效果

OOBSetedInBrowser

可以看到在右键菜单中多了一个选项,点击此选项可以直接将程序安装到计算机上,安装时由于之前设置了Trust所以需要用户确认以及设置开始和桌面快捷方式:

OOBTrust

安装完成后就可以直接看OOB下的效果

OOBInStartMenu

OOBRuning

为了让用户有更好的体验,使用右键进行安装未免不够专业并且用户体验较差,一般情况下会选择使用程序进行安装控制,代码很简单:

  1. using System;  
  2. using System.Net;  
  3. using System.Windows;  
  4. using System.Windows.Controls;  
  5. using System.Windows.Documents;  
  6. using System.Windows.Ink;  
  7. using System.Windows.Input;  
  8. using System.Windows.Media;  
  9. using System.Windows.Media.Animation;  
  10. using System.Windows.Shapes;  
  11. using System.Net.networkinformation;  
  12.   
  13. namespace Cmj.MyWeb.MySilverlight.SiverlightOOB  
  14. {  
  15.     public class OOBInstall  
  16.     {  
  17.   
  18.         public bool IsInstalled()  
  19.         {  
  20.             if (Application.Current.InstallState == InstallState.Installed)  
  21.             {  
  22.                 return true;  
  23.             }  
  24.             else  
  25.             {  
  26.                 return false;  
  27.             }  
  28.         }  
  29.   
  30.         /// <summary>   
  31.         /// 程序安装   
  32.         /// </summary>   
  33.         public void Install()  
  34.         {  
  35.             if (!Application.Current.IsRunningOutOfbrowser)  
  36.             {  
  37.                 if (Application.Current.InstallState != InstallState.Installed)  
  38.                 {  
  39.                     Application.Current.Install();  
  40.                 }  
  41.                 else  
  42.                 {  
  43.                     MessageBox.Show("程序已安装!","系统提示",MessageBoxButton.OK);  
  44.                 }  
  45.                   
  46.             }  
  47.             //调用InstallStateChanged事件将安装状态传递给调用  
  48.             Application.Current.InstallStateChanged += new EventHandler(Current_InstallStateChanged);  
  49.         }  
  50.   
  51.         void Current_InstallStateChanged(object sender, EventArgs e)  
  52.         {  
  53.             //此处调用自定义事件   
  54.             OnInstalling(sender, new InstallStateArgs());  
  55.         }  
  56.   
  57.         //自定义一个事件来修改安装时状态   
  58.         public delegate void InstallingHandler(object sender,InstallStateArgs e);  
  59.         public event InstallingHandler Installing;  
  60.         public void OnInstalling(object sender, InstallStateArgs e)  
  61.         {  
  62.             if (Installing != null)  
  63.             {  
  64.                 Installing(sender,e);  
  65.             }  
  66.         }  
  67.     }  
  68.       
  69.     public class InstallStateArgs:EventArgs  
  70.     {  
  71.         public InstallState State  
  72.         {  
  73.             get  
  74.             {  
  75.                 return Application.Current.InstallState;  
  76.             }  
  77.         }  
  78.     }  
  79.   
  80. }  


在程序中通过Application.Current.Install()方法进行安装,当然在这之前最好进行状态检测看程序是不是已经在浏览器外运行。另外在安装时也可以使用Application.Current.InstallState跟踪安装状态。有了上面的代码接下来就可在界面放一个按钮,然后在不同的状态下动态给用户不同的提示

XAML代码

  1. <UserControl x:Class="SilverlightApplicationOOB.MainPage"  
  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.     xmlns:cmj="clr-namespace:Cmj.MyWeb.MySilverlight.MyUserControl;assembly=Cmj.MyWeb.MySilverlight"     
  7.     mc:Ignorable="d"  
  8.     d:DesignHeight="300" d:DesignWidth="400" Loaded="UserControl_Loaded">  
  9.   
  10.     <Grid x:Name="LayoutRoot" Background="#FFCDDBE8">  
  11.         <Grid.RowDeFinitions>  
  12.             <RowDeFinition Height="60"></RowDeFinition>  
  13.             <RowDeFinition Height="*"></RowDeFinition>  
  14.         </Grid.RowDeFinitions>  
  15.         <Grid.ColumnDeFinitions>  
  16.             <ColumnDeFinition Width="*"></ColumnDeFinition>  
  17.         </Grid.ColumnDeFinitions>  
  18.         <TextBlock Height="42" HorizontalAlignment="Center" Name="txtblkDescription" Text="Silverlight Out Of browser" VerticalAlignment="Center" Foreground="#FFF21FE1" FontSize="20" />  
  19.         <Button Content="Install" Grid.Row="1" Height="23" HorizontalAlignment="Center" Name="btnInstall" VerticalAlignment="Center" Width="75" Click="btnInstall_Click" />  
  20.     </Grid>  
  21. </UserControl>  

CS代码

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Windows;  
  6. using System.Windows.Controls;  
  7. using System.Windows.Documents;  
  8. using System.Windows.Input;  
  9. using System.Windows.Media;  
  10. using System.Windows.Media.Animation;  
  11. using System.Windows.Shapes;  
  12. using Cmj.MyWeb.MySilverlight.SiverlightOOB;  
  13.   
  14. namespace SilverlightApplicationOOB  
  15. {  
  16.     public partial class MainPage : UserControl  
  17.     {  
  18.         public MainPage()  
  19.         {  
  20.             InitializeComponent();  
  21.         }  
  22.   
  23.         OOBInstall oob = new OOBInstall();  
  24.         private void UserControl_Loaded(object sender, RoutedEventArgs e)  
  25.         {  
  26.             if (oob.IsInstalled())  
  27.             {  
  28.                 this.btnInstall.Visibility = Visibility.Collapsed;  
  29.             }  
  30.             else  
  31.             {  
  32.                 this.btnInstall.Visibility=Visibility.Visible;  
  33.             }  
  34.         }  
  35.   
  36.         private void btnInstall_Click(object sender, RoutedEventArgs e)  
  37.         {  
  38.             oob.Install();  
  39.         }  
  40.     }  
  41. }  

安装完后如何卸载呢,事实上可以按照一般C/S程序的方式卸载,或者在Web中运行此程序右键进行删除

三、自定义OOB窗口

在上面或许您已经看到了,OOB设置窗口中有以下窗口风格设置:

OOBWindowStyle

这个选项在很多时候是很有用的,上面窗口使用的是认风格(Default),窗口操作(最大化、最小化、关闭、移动、窗口大小设置)均调用windows api。当然,相应的界面也显得呆板一些,如果你的应用想要拥有炫酷的界面,那么Default或许无法满足你的需求。但是silverlight已经为您预留了接口,后面No BorderBorderless Round Corners就给你无限的遐想空间。下面就来看一下如何自定义一个OOB窗口。

自定义窗口其实就是要实现最小化、最大化、关闭、窗口移动、窗口大小设置功能,除了窗口大小设置功能之外其他功能均在标题栏实现,而窗口大小的resize操作一般是放到窗口右下角进行的,因此为了便于复用,接下来会创建两个用户控件:TitleBar和ResizeButton。

创建一个silverlight class library项目Cmj.MyWeb.MySilverlight,添加一个UserControl,命名为TitleBar。TitleBar就是标题栏部分,实现最小化、最大化、关闭、窗口移动操作。标题栏主要包括ico、标题文本、最小化按钮、最大化按钮和关闭按钮几部分,这里不妨使用Grid布局将其分为一行五列,分别对应ico、标题和三个按钮。为了使控件在使用时自适应宽度,可以将标题部分所在的列设置为"*",并且注意不要给UserControl设置Width和Height属性,只需要设置DesignWidth和DesignHeight即可。在第一列中放一个Image控件用于显示应用程序图标,在第二列中放一个TextBlock显示标题内容,其余三列放三个Button按钮用于实现窗口最小化、最大化和关闭。然后准备四个图标作为按钮的背景(最大化按钮需要最大化状态和正常状态两个图标):

minizize

 

maximizeNormal

 

maximizeMax

 

close

有了这些之后接下来就需要实现各部分功能了。最小化、最大化只需要设置MainWindow的WindowState即可;关闭调用Close方法;而拖动操作可以交给Grid来做,在Grid的MouseLeftButtonDown中调用MainWindow的DragMove方法。需要注意的是最大化按钮的图标有两种状态,需要根据不同的状态动态改变图标。

到此为止TitleBar的功能基本上已经实现,但作为一个通用控件最好把一些内容交给使用者来操作以便增加控件灵活性,这里定义Icon、Title、Background属性用户设置标题栏图标、标题内容标题栏背景。下面是TitleBar前台XAML和后台CS代码

  1. <UserControl x:Class="Cmj.MyWeb.MySilverlight.MyUserControl.TitleBar"  
  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="24" d:DesignWidth="400">  
  8.       
  9.     <Grid x:Name="LayoutRoot" Background="Gray" MouseLeftButtonDown="LayoutRoot_MouseLeftButtonDown">  
  10.         <Grid.ColumnDeFinitions>  
  11.             <ColumnDeFinition Width="Auto" />  
  12.             <ColumnDeFinition Width="*" />  
  13.             <ColumnDeFinition Width="Auto" />  
  14.             <ColumnDeFinition Width="Auto"/>  
  15.             <ColumnDeFinition Width="Auto"/>  
  16.         </Grid.ColumnDeFinitions>  
  17.         <Grid.RowDeFinitions>  
  18.             <RowDeFinition Height="22" />  
  19.         </Grid.RowDeFinitions>  
  20.         <Image Grid.Row="0" Grid.Column="0" Name="imgIcon" Width="16" Height="16" Source="/Cmj.MyWeb.MySilverlight;component/Images/cmjlogo.png" />  
  21.         <TextBlock Grid.Row="0" Grid.Column="1" Margin="5,4" Text="CMJ Title Bar Control" Name="txtblkTitle" HorizontalAlignment="Left" />  
  22.         <Button Grid.Row="0" Grid.Column="2" Height="18" HorizontalAlignment="Right" Name="btnMiniMize" VerticalAlignment="Top" Width="18" Margin="2,2,2" Click="btnMiniMize_Click">  
  23.             <Image Name="imgMinimize" Source="/Cmj.MyWeb.MySilverlight;component/Images/minizime.png" />  
  24.         </Button>  
  25.         <Button Grid.Row="0" Grid.Column="3" Height="18" HorizontalAlignment="Left" Name="btnMaxiMize" VerticalAlignment="Top" Width="18" Margin="2,2" Click="btnMaxiMize_Click">  
  26.             <Image Name="imgMaximize" Source="/Cmj.MyWeb.MySilverlight;component/Images/maximize.png" />  
  27.         </Button>  
  28.         <Button Grid.Row="0" Grid.Column="4" Height="18" HorizontalAlignment="Right" Name="btnClose" VerticalAlignment="Top" Width="18" Margin="2,5,2" Click="btnClose_Click" >  
  29.             <Image Name="imgClose" Source="/Cmj.MyWeb.MySilverlight;component/Images/close.png" />  
  30.         </Button>  
  31.     </Grid>  
  32. </UserControl>  

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Windows;  
  6. using System.Windows.Controls;  
  7. using System.Windows.Documents;  
  8. using System.Windows.Input;  
  9. using System.Windows.Media;  
  10. using System.Windows.Media.Animation;  
  11. using System.Windows.Media.Imaging;  
  12. using System.Windows.Shapes;  
  13.   
  14. namespace Cmj.MyWeb.MySilverlight.MyUserControl  
  15. {  
  16.     public partial class TitleBar : UserControl  
  17.     {  
  18.         public TitleBar()  
  19.         {  
  20.             InitializeComponent();  
  21.         }  
  22.   
  23.         private BitmapImage binormal = new BitmapImage(new Uri("../Images/maximizeMax.png", UriKind.Relative));  
  24.         private BitmapImage biMax = new BitmapImage(new Uri("../Images/maximize.png", UriKind.Relative));  
  25.   
  26.         public ImageSource Icon  
  27.         {  
  28.             get  
  29.             {  
  30.                 return this.imgIcon.source;  
  31.             }  
  32.             set  
  33.             {  
  34.                 this.imgIcon.source = value;  
  35.             }  
  36.         }  
  37.   
  38.         public string Title  
  39.         {  
  40.             get  
  41.             {  
  42.                 return this.txtblkTitle.Text;  
  43.             }  
  44.             set  
  45.             {  
  46.                 this.txtblkTitle.Text = value;  
  47.             }  
  48.         }  
  49.   
  50.         public Color BackgroundColor  
  51.         {  
  52.             get  
  53.             {  
  54.                 return ((SolidColorBrush)this.LayoutRoot.Background).Color;  
  55.             }  
  56.             set  
  57.             {  
  58.                 ((SolidColorBrush)this.LayoutRoot.Background).Color = value;  
  59.             }  
  60.         }  
  61.   
  62.         public new Brush Background  
  63.         {  
  64.             get  
  65.             {  
  66.                 return this.LayoutRoot.Background;  
  67.             }  
  68.             set  
  69.             {  
  70.                 this.LayoutRoot.Background=value;  
  71.             }  
  72.         }  
  73.   
  74.         public new double Height  
  75.         {  
  76.             get  
  77.             {  
  78.                 return this.LayoutRoot.Height;  
  79.             }  
  80.             private set  
  81.             {  
  82.                 this.LayoutRoot.Height = value;  
  83.             }  
  84.         }  
  85.   
  86.         private void btnMiniMize_Click(object sender, RoutedEventArgs e)  
  87.         {  
  88.             Application.Current.MainWindow.WindowState = WindowState.Minimized;  
  89.         }  
  90.   
  91.         private void btnMaxiMize_Click(object sender, RoutedEventArgs e)  
  92.         {  
  93.             if (Application.Current.MainWindow.WindowState == WindowState.Maximized)  
  94.             {  
  95.                 this.imgMaximize.source = biMax;  
  96.                 Application.Current.MainWindow.WindowState = WindowState.normal;  
  97.             }  
  98.             else  
  99.             {  
  100.                 this.imgMaximize.source = binormal;  
  101.                 Application.Current.MainWindow.WindowState = WindowState.Maximized;  
  102.             }  
  103.             this.Width = Application.Current.MainWindow.Width;  
  104.         }  
  105.   
  106.         private void btnClose_Click(object sender, RoutedEventArgs e)  
  107.         {  
  108.             Application.Current.MainWindow.Close();  
  109.         }  
  110.   
  111.         private void LayoutRoot_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)  
  112.         {  
  113.             Application.Current.MainWindow.DragMove();  
  114.         }  
  115.   
  116.     }  
  117. }  

TitleBar在上面已经完成接下来就来实现窗口的resize操作,在项目中再添加一个UserControl命名为ResizeButton,在ResizeButton中放置Image控件来显示拖拽图标

risize

,接下来设置UserControl的MouseLeftButtonDown事件调用MainWindow的DragResize方法实现拖拽功能,在MouseEnter和MouseLeave事件中更改鼠标指针为SizenesW和Arrow,代码如下:

  1. <UserControl x:Class="Cmj.MyWeb.MySilverlight.MyUserControl.ResizeButton"  
  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="16" d:DesignWidth="16" MouseLeftButtonDown="UserControl_MouseLeftButtonDown" MouseEnter="UserControl_MouseEnter" MouseLeave="UserControl_MouseLeave">  
  8.       
  9.     <Grid x:Name="LayoutRoot" Background="Transparent">  
  10.         <Image Name="imgResize" Width="16" Height="16" Source="/Cmj.MyWeb.MySilverlight;component/Images/resize.png" />         
  11.     </Grid>  
  12. </UserControl>  

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Windows;  
  6. using System.Windows.Controls;  
  7. using System.Windows.Documents;  
  8. using System.Windows.Input;  
  9. using System.Windows.Media;  
  10. using System.Windows.Media.Animation;  
  11. using System.Windows.Shapes;  
  12.   
  13. namespace Cmj.MyWeb.MySilverlight.MyUserControl  
  14. {  
  15.     public partial class ResizeButton : UserControl  
  16.     {  
  17.         public ResizeButton()  
  18.         {  
  19.             InitializeComponent();  
  20.         }  
  21.   
  22.         private void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)  
  23.         {  
  24.             Application.Current.MainWindow.DragResize(WindowResizeEdge.Bottomright);  
  25.         }  
  26.   
  27.         private void UserControl_MouseEnter(object sender, MouseEventArgs e)  
  28.         {  
  29.             this.Cursor = Cursors.SizenesW;  
  30.         }  
  31.   
  32.         private void UserControl_MouseLeave(object sender, MouseEventArgs e)  
  33.         {  
  34.             this.Cursor = Cursors.Arrow;  
  35.         }  
  36.     }  
  37. }  

两个控件都已经开发完毕,剩下的工作就是如何使用了。在SilverlightApplicationOOB中引用Cmj.MyWeb.MySilverlight的dll。接着在Main.Xaml中添加命名空间:xmlns:cmj="clr-namespace:Cmj.MyWeb.MySilverlight.MyUserControl;assembly=Cmj.MyWeb.MySilverlight"

(控件前缀任意命名,这里命名为cmj),然后在Main.Xaml中定义TitleBar和ResizeButton并设置相关属性

  1. <UserControl x:Class="SilverlightApplicationOOB.MainPage"  
  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.     xmlns:cmj="clr-namespace:Cmj.MyWeb.MySilverlight.MyUserControl;assembly=Cmj.MyWeb.MySilverlight"     
  7.     mc:Ignorable="d"  
  8.     d:DesignHeight="300" d:DesignWidth="400" Loaded="UserControl_Loaded">  
  9.   
  10.     <Grid x:Name="LayoutRoot" Background="#FFCDDBE8">  
  11.         <Grid.RowDeFinitions>  
  12.             <RowDeFinition Height="23"></RowDeFinition>  
  13.             <RowDeFinition Height="*"></RowDeFinition>  
  14.             <RowDeFinition Height="16"></RowDeFinition>  
  15.         </Grid.RowDeFinitions>  
  16.         <Grid.ColumnDeFinitions>  
  17.             <ColumnDeFinition Width="*"></ColumnDeFinition>  
  18.         </Grid.ColumnDeFinitions>  
  19.         <cmj:TitleBar Grid.Row="0" Title="CMJ Control" Icon="/Cmj.MyWeb.MySilverlight;component/Images/cmjlogo.png" Background="#FFCDDBE8" />  
  20.         <Grid Grid.Row="1" Name="GrdContent" Background="#FFADB9CD" Margin="3,3,0">  
  21.             <TextBlock HorizontalAlignment="Center" Name="txtblkUpdateDescription" Text="Application Update!" VerticalAlignment="Center" Foreground="#FFEF0FDD" FontSize="20"/>  
  22.         </Grid>  
  23.         <cmj:ResizeButton Grid.Row="2" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="0,0"/>  
  24.     </Grid>  
  25. </UserControl>  

下面是安装和运行效果

CustomWindow

四、OOB应用升级

上面的OOB应用看起来跟在Web中运行没有两样,又可以以类似于B/S的方式运行,但是大家都知道B/S有一个很多的弊端就是升级问题,那么OOB应用如何升级呢?总不能每次运行程序先删除再安装一次吧?事实上OOB模式下升级工作很简单,只要在程序需要升级修改AssemblyVersion版本(在项目的AssemblyInfo.cs文件中),然后在程序启动时调用Application.Current.CheckAndDownloadUpdateAsync()方法就能完成更新工作,这里将更新操作写到前面OOBInstall类中。为了便于观看效果,在loaded事件中添加更新操作,接着在浏览器中运行并将之前安装的程序删除重新安装。然后对Main.Xaml做稍许改变(在其中添加一个TextBlock),修改版本信息为,1.0.0.1

OOBInstall添加更新方法后:

  1. using System;  
  2. using System.Net;  
  3. using System.Windows;  
  4. using System.Windows.Controls;  
  5. using System.Windows.Documents;  
  6. using System.Windows.Ink;  
  7. using System.Windows.Input;  
  8. using System.Windows.Media;  
  9. using System.Windows.Media.Animation;  
  10. using System.Windows.Shapes;  
  11. using System.Net.networkinformation;  
  12.   
  13. namespace Cmj.MyWeb.MySilverlight.SiverlightOOB  
  14. {  
  15.     public class OOBInstall  
  16.     {  
  17.   
  18.         public bool IsInstalled()  
  19.         {  
  20.             if (Application.Current.InstallState == InstallState.Installed)  
  21.             {  
  22.                 return true;  
  23.             }  
  24.             else  
  25.             {  
  26.                 return false;  
  27.             }  
  28.         }  
  29.   
  30.         /// <summary>   
  31.         /// 程序安装   
  32.         /// </summary>   
  33.         public void Install()  
  34.         {  
  35.             if (!Application.Current.IsRunningOutOfbrowser)  
  36.             {  
  37.                 if (Application.Current.InstallState != InstallState.Installed)  
  38.                 {  
  39.                     Application.Current.Install();  
  40.                 }  
  41.                 else  
  42.                 {  
  43.                     MessageBox.Show("程序已安装!",MessageBoxButton.OK);  
  44.                 }  
  45.                   
  46.             }  
  47.             //调用InstallStateChanged事件将安装状态传递给调用  
  48.             Application.Current.InstallStateChanged += new EventHandler(Current_InstallStateChanged);  
  49.         }  
  50.   
  51.         void Current_InstallStateChanged(object sender, EventArgs e)  
  52.         {  
  53.             //此处调用自定义事件   
  54.             OnInstalling(sender, new InstallStateArgs());  
  55.         }  
  56.   
  57.         //自定义一个事件来修改安装时状态   
  58.         public delegate void InstallingHandler(object sender,InstallStateArgs e);  
  59.         public event InstallingHandler Installing;  
  60.         public void OnInstalling(object sender, InstallStateArgs e)  
  61.         {  
  62.             if (Installing != null)  
  63.             {  
  64.                 Installing(sender,e);  
  65.             }  
  66.         }  
  67.   
  68.         /// <summary>   
  69.         /// 程序更新(如果需要更细你可以通过修改程序Assembly Version)   
  70.         /// </summary>   
  71.         public void Update()  
  72.         {  
  73.             //网络改变时事件   
  74.             NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(NetworkChange_NetworkAddressChanged);  
  75.             //判断程序是否在oob下运行并且网络可用   
  76.             if (Application.Current.IsRunningOutOfbrowser && NetworkInterface.GetIsNetworkAvailable())  
  77.             {  
  78.                 Application.Current.CheckAndDownloadUpdateCompleted += new CheckAndDownloadUpdateCompletedEventHandler(Current_CheckAndDownloadUpdateCompleted);  
  79.                 Application.Current.CheckAndDownloadUpdateAsync();    
  80.             }  
  81.         }  
  82.   
  83.         void NetworkChange_NetworkAddressChanged(object sender, EventArgs e)  
  84.         {  
  85.             Console.WriteLine("网络发生改变,当前网络状态:{0}", NetworkInterface.GetIsNetworkAvailable().ToString());  
  86.         }  
  87.   
  88.         void Current_CheckAndDownloadUpdateCompleted(object sender, CheckAndDownloadUpdateCompletedEventArgs e)  
  89.         {  
  90.             if (e.UpdateAvailable)  
  91.             {  
  92.                 MessageBox.Show("程序已更新到最新版本,请重新启动!""系统提示", MessageBoxButton.OK);  
  93.             }  
  94.             else  
  95.             {  
  96.                 if (e.Error != null)  
  97.                 {  
  98.                     MessageBox.Show("程序更新失败,错误信息如下:"+Environment.NewLine+e.Error.Message, MessageBoxButton.OK);  
  99.                 }  
  100.             }  
  101.         }  
  102.   
  103.     }  
  104.       
  105.     public class InstallStateArgs:EventArgs  
  106.     {  
  107.         public InstallState State  
  108.         {  
  109.             get  
  110.             {  
  111.                 return Application.Current.InstallState;  
  112.             }  
  113.         }  
  114.     }  
  115.   
  116. }  

在MainPage后台代码调用

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Windows;  
  6. using System.Windows.Controls;  
  7. using System.Windows.Documents;  
  8. using System.Windows.Input;  
  9. using System.Windows.Media;  
  10. using System.Windows.Media.Animation;  
  11. using System.Windows.Shapes;  
  12. using Cmj.MyWeb.MySilverlight.SiverlightOOB;  
  13.   
  14. namespace SilverlightApplicationOOB  
  15. {  
  16.     public partial class MainPage : UserControl  
  17.     {  
  18.         public MainPage()  
  19.         {  
  20.             InitializeComponent();  
  21.         }  
  22.   
  23.         private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)  
  24.         {  
  25.   
  26.         }  
  27.   
  28.         private void UserControl_Loaded(object sender, RoutedEventArgs e)  
  29.         {  
  30.             OOBInstall oob = new OOBInstall();  
  31.             oob.Update();  
  32.         }  
  33.     }  
  34. }  

重新编译程序,再启动程序,程序会自动更新,然后提示更细成功:

UpdateSuccessTip

接着重启应用,会发现程序更新后的效果

Updated

OK,今天就到这里吧!程序下载 

download

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐