OOB应用、OOB更新以及WebBrowser

首先学习OOB应用。

SL提供了OUT-OF-Browser的功能,下面通过一个实例来学习下这个功能,在这个例子中使用到了WebBrowser功能。

首先新建一个工程,SLWebBrower

然后建立一个Install.xaml,xaml里有一个安装button,和一个显示安装内容的textBlock,如图:

这个按钮的事件其实很简单就是判断是否安装,如果没有就安装

代码如下:

private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (App.Current.InstallState != InstallState.Installed)
            {
                App.Current.Install();
            }
        }

我们给页面添加一个安装状态的事件,在这个事件里判断当前应用程序的安装状态并在textBolck里显示相应的提示,代码如下:

 

代码

public Install()
{
this.Loaded += new RoutedEventHandler(Install_Loaded);
InitializeComponent();
App.Current.InstallStateChanged
+= new EventHandler(Current_InstallStateChanged);
}

private void UpdateInstallState()
{
switch (App.Current.InstallState)
{
case InstallState.NotInstalled:
button1.Visibility
= System.Windows.Visibility.Visible;
textBlock1.Text
= "请点击安装按钮,进行安装";
break;
case InstallState.Installing:
button1.Visibility
= System.Windows.Visibility.Collapsed;
textBlock1.Text
= "正在安装,请稍后。。。";
break;
case InstallState.InstallFailed:
button1.Visibility
= System.Windows.Visibility.Visible;
textBlock1.Text
= "安装失败,请重试";
textBlock1.Foreground
= new SolidColorBrush(Colors.Red);
break;
case InstallState.Installed:
button1.Visibility
= System.Windows.Visibility.Visible;
textBlock1.Text
= "安装完成!";
textBlock1.Foreground
= new SolidColorBrush(Colors.Green);
break;

}
}

 

 

 

做好了安装界面,我们再来看看mainpage里面的处理

在mainpage里面放一个textblock和一个WebBrowser控件,我们设定webBrowser的Source为http://localhost/Default.html

这个将会在运行时会把Default.html加载入mainpage页面来,这样用户不知道窗体的这个位置是个网页;我们在这里在屏幕的右下角弹出一个小框,告诉用户

Default.html加载完毕,让用户知道软件在做什么。

是这么实现的,在webbrowser的LoadCompleted事件里,我们通过NotificationWindow窗体来实现这个功能,代码如下:

 

代码
private void webBrowser1_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
NotificationWindow nw
= new NotificationWindow();
nw.Content
= new TextBlock { Text = "HTML加载完成!"};
nw.Show(
5000);
}

 

接下来我们要在app.cs的Application_Startup事件里添加一个判断,判断是不是OOB运行,如果不是就提示用户安装

代码如下:

 private void Application_Startup(object sender, StartupEventArgs e)
        {
            if (App.Current.IsRunningOutOfBrowser && App.Current.HasElevatedPermissions)
            {
                this.RootVisual = new MainPage();
            }
            else
            {
                this.RootVisual = new Install();
            }
        }
最后要设置项目属性,让项目在OOB下有充分的权限运行,具体设置如图(注意checkbox)
到此,这个项目就可以在OOB模式下运行,运行效果如图:
下面我们来讨论下OOB模式下的更新
更新的实现只需要在程序启动时判断是不是最新,如果不是就更新就行了
代码如下:
 public App()
        {

            if (App.Current.InstallState == System.Windows.InstallState.Installed)
            {
                App.Current.CheckAndDownloadUpdateCompleted += new CheckAndDownloadUpdateCompletedEventHandler(Current_CheckAndDownloadUpdateCompleted);
                App.Current.CheckAndDownloadUpdateAsync();
            }

            this.Startup += this.Application_Startup;
            this.Exit += this.Application_Exit;
            this.UnhandledException += this.Application_UnhandledException;

            InitializeComponent();
        }

        void Current_CheckAndDownloadUpdateCompleted(object sender, CheckAndDownloadUpdateCompletedEventArgs e)
        {
            if (e.UpdateAvailable && e.Error == null)
            {
                MessageBox.Show("应用程序已经安装,下次启动时生效");
            }
            else if (e.Error != null)
            {
                MessageBox.Show("安装失败"+e.Error.Message);
            }
        }

如果在本机执行这样就OK了,可以实现更新。如果发布就需要数字签名,这个研究了很久没搞定,学习的网站贴出来大家一起研究,谁研究明白烦请指点一二。

http://msdn.microsoft.com/library/dd550721(VS.95).aspxhttp://www.cnblogs.com/chuifeng/archive/2010/08/20/1804294.html

 

在研究OOB的时候有顺带的研究了安装离线程序到客户机

一个安装实例:

"C:\Program Files\Microsoft Silverlight\sllauncher.exe"

 /install:"SLWebBrower.xap"

/origin:"http://192.168.1.101:8088/ClientBin/SLWebBrower.xap"

 /shortcut:desktop+startmenu

  /overwrites

以下是关于本地安装silverlight的xap的参数说明

安装

  • /install:"xapFile" – 其中xapFile是文件名/文件路径.xap文件.,E.g. /install:"c:\temp\sample.xap"
  • /origin:"xapURI" – 其中xapURI是在其中的.xap文件将已经从前来的URI,如果没有安装在命令行
    • e.g. /origin:"http://example.com/sample.xap" .  这个URI将被作为原产地为安全目的的网站.例如,对于沙盒的应用,Silverlight的网络需要一个策略文件检查网络请求时域比其他源站点,起源也可作为一个应用程序的唯一的ID.该xapURI必须是绝对URI不是一个相对URI,而且必须开始为http:,通过https:或文件:.
  • /overwrite -- (可选)将覆盖任何与该xapURI以前安装的xap.

删除

  • /uninstall  – 卸载指定的应用程序 /origin. 这是相同的/origin的作用被用来安装应用程序最初.
  • /origin:"xapURI" – 相同/origin的情况下安装

 

有不对之处还请大家提出来共同进步。

 

OOB学习示例代码:https://files.cnblogs.com/Clivia/SLWebBrower.rar

posted @ 2010-11-02 23:20  星空有我  阅读(1177)  评论(0编辑  收藏  举报