Silverlight本身是一种RIA应用(Rich Internet applications)
OOB: Out Of Brower,成为脱离浏览器的Silverlight应用程序,就是Silverlight应用程序可以像Winform程序一样安装在本地PC上进行运行使用。
好处是
1)可以再OOB模式下访问更多的权限,比如跨域访问;本地文件访问存储。
2)用户体验提高,可以制作托盘程序,冒泡提示等。
OOB的组成:
OOB可以做到:
1) 修改Wndows窗体样式。
2) 修改窗体大小。
3) 访问剪贴板。
4) 访问用户文件夹(沙箱)。
5) 显示HTML内容
6) Notifacation Window(toast)。
7) 可宽松的跨域访问机制。
8) 可以调用com组件。
OOB实践,该OOB可以达到检查更新;安装应用;创建图标;设置窗体尺寸和位置。
NotifcationWindow 用户自定义弹出信息,可设定弹出时间。
WebBrower浏览器控件:只有在OOB模式下使用,可以指定网页或者显示指定的HTML。
Demo详解
XAML代码:
<UserControl x:Class="OOBDemo.MainPage"
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" Width="800" Height="600">
<Grid x:Name="LayoutRoot" Background="White" Width="800" Height="600">
<Grid x:Name="InstallContainer" Margin="1,1,-1,-1" d:IsHidden="True">
<Rectangle Fill="#D8696983" Height="600" Stroke="Black" VerticalAlignment="Top" Width="800"/>
<Button x:Name="InstallButton" Content="InstallApp" Margin="301,264,254,274" Click="InstallButton_Click"/>
</Grid>
<TextBox x:Name="TextrRegion" HorizontalAlignment="Left" Height="70" Margin="18,37,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="217"/>
<Button x:Name="LaunchBtn" Content="打开Word,并输入左侧字符串" Height="64" Margin="241,42,0,0" VerticalAlignment="Top" Click="LaunchBtn_Click" HorizontalAlignment="Left" Width="159"/>
<WebBrowser x:Name="WebB" Margin="28,132,154,32"/>
</Grid>
</UserControl>
这里的InstallContainer是为了显示安装时,遮挡住下面的程序页面。
这里需要注意的是,当要进行更新OOB操作的时候,右下角启动的VisualStudio的小型Web Server不要关闭,要保持运行状态。最好在VisualStudio里编译后,在Blend里打开xaml运行程序。
C#源代码:
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.Runtime.InteropServices.Automation;
namespace OOBDemo
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
//判断是否有更新
App.Current.CheckAndDownloadUpdateAsync();
//加载安装OOB模式
Loaded += new RoutedEventHandler(MainPage_Loaded);
//把OOB更新到最新版本
App.Current.CheckAndDownloadUpdateCompleted += new CheckAndDownloadUpdateCompletedEventHandler(Current_CheckAndDownloadUpdateCompleted);
//WebBrower应用:打开网页
WebB.Source = new Uri("http://www.bing.com");
//WebBrower应用:HTMLBrush功能 直接手写一个网页显示在WebBrower里
//WebB.NavigateToString("<b>Hello World</b>");
// 加载完WebBrower后,在右下角托盘显示加载完成 (必须加载外部的信息才可以,加载本地不会触发该事件)
WebB.LoadCompleted += new System.Windows.Navigation.LoadCompletedEventHandler(WebB_LoadCompleted);
}
//在右下角托盘窗口提示窗体 加载完成
void WebB_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
NotificationWindow notify = new NotificationWindow();
notify.Width = 400;
notify.Height = 100;
TextBlock tb = new TextBlock();
tb.Text = "加载完成";
tb.FontSize = 24;
notify.Content = tb;
notify.Show(3000);
}
void Current_CheckAndDownloadUpdateCompleted(object sender, CheckAndDownloadUpdateCompletedEventArgs e)
{
if (e.UpdateAvailable)
{
MessageBox.Show("现在已经更新,请退出程序,重新启动该程序,重新启动后会自动更新到最新。");
}
}
//安装OOB模式
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
if (App.Current.InstallState == InstallState.Installed)
{
InstallContainer.Visibility = Visibility.Collapsed;
}
}
private void InstallButton_Click(object sender, System.Windows.RoutedEventArgs e)
{
// TODO: Add event handler implementation here.
//安装OOB程序
App.Current.Install();
}
//在Windows系统上打开一个Word,并且输入TextBox里的字符串到Word里。调用COM组件来实现
private void LaunchBtn_Click(object sender, System.Windows.RoutedEventArgs e)
{
// TODO: Add event handler implementation here.
//需引用路径C:/PrgrameFile/Microsfot SDKs/Silverlight/v4.0/Libraries/Client -> Microsoft.CSharp.dll
dynamic word = AutomationFactory.CreateObject("Word.Application");
word.Visible = true;
dynamic doc = word.Documents.Add();
string insertText = TextrRegion.Text;
dynamic range = doc.Range(0, 0);
range.Text = insertText;
}
}
}