你的分享就是我们的动力 ---﹥

Silverlight中如何为浏览器外应用程序实现脱机支持

时间:2011-10-24 09:55来源:www.chengxuyuans.com 点击:

本主题演示如何为 Silverlight 浏览器外应用程序实现以下常见方案:

确定应用程序是否已安装在浏览器之外或在浏览器外运行。这样,您可以执行显示品牌等此类通常应 使用 HTML 显示的内容。本主题中的实例使用此信息来隐藏安装按钮或显示网络状态指示灯。

确定网络连接是否可用。这样,您可以禁用依赖于网络的功能,或向基于网络或浏览器的资源提供备 选方案。本主题中的示例使用此信息来更新网络连接指示灯以及显示或隐藏更新按钮。

确定是否提供了应用程序更新。如果连接可用,则可以检查并下载应用程序的新版本,并提示用户重 新启动。本主题中的示例提供了此功能的基本演示。

当应用程序在浏览器外运行时,您可以任意更改其外观和行为。然而,在大多数情况下,应尽量少更 改应用程序。这可确保用户无需学习新的用户界面。但是,如果应用程序使用网络资源,则始终应在用户 界面中显示网络连接状态。

在本例中,XAML 指定一个用户界面,该用户界面显示网络状态指示器、安装按钮和更新按钮以及若干 TextBlock 控件。文本块绑定到浏览器外配置设置,本主题末尾的 XML 中说明了这些设置。您可以将这 些设置复制到 Visual Studio 的"浏览器外设置"对话框。此外,您可以在 Visual Studio 中启用浏览器 外支持,然后将 XML 复制到项目的 OutOfBrowserSettings.xml 文件中。有关更多信息,请参见如何为 浏览器外支持配置应用程序。

代码隐藏设置由绑定使用的 DataContext,并处理更新用户界面所需的各种事件。

当您运行此示例时,请单击安装按钮或使用右键单击菜单选项进行安装。请注意,安装后,安装按钮 将消失。您可以右键单击浏览器外应用程序或基于 Web 的应用程序以进行卸载。但是,仅当从基于 Web 的应用程序中卸载时,安装按钮才重新显示。这是因为浏览器外应用程序与基于 Web 的版本之间不再具 有任何连接。如果您从浏览器外应用程序进行卸载,则安装按钮不会重新显示,直至您刷新网页。

您可以通过从网络断开连接并重新连接,来测试浏览器外应用程序中的网络连接指示器。请注意,当 连接不可用时,更新按钮将消失。

为了测试更新按钮,应在 Silverlight 解决方案中包含 Web 应用程序项目。在安装应用程序之后, 在 Visual Studio 中修改和重新生成解决方案,然后单击安装按钮以下载更新。

运行此示例:http://go.microsoft.com/fwlink/?LinkId=139798&sref=OutOfBrowser

示例

XAML

<UserControl x:Class="OutOfBrowser.MainPage"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <StackPanel x:Name="LayoutRoot" Background="White" Margin="20">

   <TextBlock x:Name="networkIndicator" FontWeight="Bold" FontSize="20"/>

   <Button x:Name="installButton" Content="install"
    HorizontalAlignment="Left" Margin="0,10" Padding="10,5"
    Click="installButton_Click"/>
   <Button x:Name="updateButton" Content="check for update"
    HorizontalAlignment="Left" Margin="0,10" Padding="10,5"
    Click="updateButton_Click"/>

   <StackPanel Orientation="Horizontal">
    <TextBlock Text="ShortName = " FontWeight="Bold"/>
    <TextBlock Text="{Binding ShortName}"/>
   </StackPanel>

   <StackPanel Orientation="Horizontal">
    <TextBlock Text="Blurb = " FontWeight="Bold"/>
    <TextBlock Text="{Binding Blurb}"/>
   </StackPanel>

   <StackPanel Orientation="Horizontal">
    <TextBlock Text="WindowSettings.Title = " FontWeight="Bold"/>
    <TextBlock Text="{Binding WindowSettings.Title}"/>
   </StackPanel>

   <StackPanel Orientation="Horizontal">
    <TextBlock Text="WindowSettings.Height = " FontWeight="Bold"/>
    <TextBlock Text="{Binding WindowSettings.Height}"/>
   </StackPanel>

   <StackPanel Orientation="Horizontal">
    <TextBlock Text="WindowSettings.Width = " FontWeight="Bold"/>
    <TextBlock Text="{Binding WindowSettings.Width}"/>
   </StackPanel>

   <StackPanel Orientation="Horizontal">
     <TextBlock Text="IsRunningOutOfBrowser = " FontWeight="Bold"/>
     <TextBlock x:Name="isRunningOutOfBrowserTextBlock"/>
   </StackPanel>

   <StackPanel Orientation="Horizontal">
     <TextBlock Text="InstallState = " FontWeight="Bold"/>
     <TextBlock x:Name="installStateTextBlock"/>
   </StackPanel>

  </StackPanel>
</UserControl>

VB

Imports System
Imports System.Net.NetworkInformation
Imports System.Windows
Imports System.Windows.Controls

Partial Public Class MainPage
   Inherits UserControl

   Private WithEvents app As Application = Application.Current

   Public Sub New()

     InitializeComponent()
     LayoutRoot.DataContext = Deployment.Current.OutOfBrowserSettings
     UpdateUI()

     AddHandler NetworkChange.NetworkAddressChanged, _
       AddressOf UpdateNetworkIndicator

   End Sub

   Private Sub App_InstallStateChanged(ByVal sender As Object, _
     ByVal e As EventArgs) Handles app.InstallStateChanged

     UpdateUI()

   End Sub

   Private Sub UpdateUI()

     UpdateNetworkIndicator()

     If app.InstallState = InstallState.NotInstalled Then
       installButton.Visibility = Visibility.Visible
     Else
       installButton.Visibility = Visibility.Collapsed
     End If

     If app.IsRunningOutOfBrowser Then
       updateButton.Visibility = Visibility.Visible
     Else
       updateButton.Visibility = Visibility.Collapsed
     End If

     isRunningOutOfBrowserTextBlock.Text = _
       app.IsRunningOutOfBrowser.ToString()

     installStateTextBlock.Text = app.InstallState.ToString()

   End Sub

   Private Sub UpdateNetworkIndicator()

     If app.IsRunningOutOfBrowser Then
       networkIndicator.Visibility = Visibility.Visible
     Else
       networkIndicator.Visibility = Visibility.Collapsed
     End If

     Dim online As Boolean = NetworkInterface.GetIsNetworkAvailable()

     If online Then
       networkIndicator.Text = "ONLINE"

       updateButton.Visibility = Visibility.Visible
     Else
       networkIndicator.Text = "OFFLINE"
       updateButton.Visibility = Visibility.Collapsed
     End If

   End Sub

   Private Sub installButton_Click(ByVal sender As Object, _
     ByVal e As RoutedEventArgs)

     Try 
       app.Install()
     Catch ex As InvalidOperationException
       MessageBox.Show("The application is already installed.")
     End Try 

   End Sub

   Private Sub updateButton_Click(ByVal sender As Object, _
     ByVal e As RoutedEventArgs)

     app.CheckAndDownloadUpdateAsync()

   End Sub

   Private Sub App_CheckAndDownloadUpdateCompleted(ByVal sender As Object, _
     ByVal e As CheckAndDownloadUpdateCompletedEventArgs) _
     Handles app.CheckAndDownloadUpdateCompleted

     If e.UpdateAvailable Then

       MessageBox.Show("An application update has been downloaded. " &  _
         "Restart the application to run the new version.")

     ElseIf e.Error IsNot Nothing AndAlso _
       TypeOf e.Error Is PlatformNotSupportedException Then

       MessageBox.Show("An application update is available, " & _
         "but it requires a new version of Silverlight. " & _
         "Visit the application home page to upgrade.")
     Else
       MessageBox.Show("There is no update available.")
     End If

   End Sub

End Class

C#

using System;
using System.Net.NetworkInformation;
using System.Windows;
using System.Windows.Controls;
namespace OutOfBrowser
{
public partial class MainPage : UserControl
{
Application app = Application.Current;
public MainPage()
{
InitializeComponent();
LayoutRoot.DataContext = Deployment.Current.OutOfBrowserSettings;
UpdateUI ();
app.CheckAndDownloadUpdateCompleted +=
App_CheckAndDownloadUpdateCompleted;
app.InstallStateChanged += (s,e) => UpdateUI();
NetworkChange.NetworkAddressChanged +=
(s, e) => UpdateNetworkIndicator();
}
private void UpdateUI()
{
UpdateNetworkIndicator();
installButton.Visibility =
app.InstallState == InstallState.NotInstalled ?
Visibility.Visible : Visibility.Collapsed;
updateButton.Visibility =
app.IsRunningOutOfBrowser ?
Visibility.Visible : Visibility.Collapsed;
isRunningOutOfBrowserTextBlock.Text =
app.IsRunningOutOfBrowser.ToString();
installStateTextBlock.Text = app.InstallState.ToString();
}
private void UpdateNetworkIndicator()
{
networkIndicator.Visibility =
app.IsRunningOutOfBrowser ?
Visibility.Visible : Visibility.Collapsed;
bool online = NetworkInterface.GetIsNetworkAvailable ();
networkIndicator.Text = online ?
"ONLINE" : "OFFLINE";
updateButton.Visibility = online ?
Visibility.Visible : Visibility.Collapsed;
}
private void installButton_Click(object sender, RoutedEventArgs e)
{
try
{
app.Install();
}
catch (InvalidOperationException)
{
MessageBox.Show("The application is already installed.");
}
}
private void updateButton_Click(object sender, RoutedEventArgs e)
{
app.CheckAndDownloadUpdateAsync();
}
private void App_CheckAndDownloadUpdateCompleted(object sender,
CheckAndDownloadUpdateCompletedEventArgs e)
{
if (e.UpdateAvailable)
{
MessageBox.Show("An application update has been downloaded. " +
"Restart the application to run the new version.");
}
else if (e.Error != null &&
e.Error is PlatformNotSupportedException)
{
MessageBox.Show("An application update is available, " +
"but it requires a new version of Silverlight. " +
"Visit the application home page to upgrade.");
}
else
{
MessageBox.Show("There is no update available.");
}
}
}
}

other

<OutOfBrowserSettings ShortName="Out-of-Browser Demo"
  EnableGPUAcceleration="False" ShowInstallMenuItem="True">
  <OutOfBrowserSettings.Blurb>
   This application demonstrates Silverlight's out-of-browser support.
  </OutOfBrowserSettings.Blurb>
  <OutOfBrowserSettings.WindowSettings>
   <WindowSettings Title="Silverlight Out-of-Browser Demonstration"
    Height="250" Width="500" />
  </OutOfBrowserSettings.WindowSettings>
  <OutOfBrowserSettings.Icons />
</OutOfBrowserSettings>

转载注明地址:http://www.chengxuyuans.com/slverlight/28286.html