Silverlight實例教程 - Out of Browser配置,安裝和卸載

作者: jv9  來源: 博客園  發布時間: 2010-08-11 21:14  閱讀: 3170 次  推薦: 0   原文鏈接   [收藏]  

  Silverlight 實例教程索引

  本篇將介紹Silverlight的Out of Browser在實例開發前的基礎設置以及一些開發技巧。

  在創建Silverlight項目時,默認是不支持Out of Browser模式的,所以在使用Silverilght的Out of Browser前,需要對Silverlight項目進行設置,使其支持OOB項目安裝和運行。在以下演示中,將創建一個演示例程,該例程將在后續教程中使用,由于該教程不屬于Silverilght基礎開發教程,所以這里,我不在細述如何創建Silverlight項目,

  項目名稱: SilverilghtOOBDemo

  項目環境:VS2010 + Silverlight 4

  Silverlight的Out of Browser應用設置

  在Silverlight項目中,設置項目支持Out of Browser模式非常簡單,首先右鍵選中SilverlightOOBDemo客戶端項目,選擇“Properties”屬性

  在項目屬性欄,默認情況下已經選擇了Silverlight標簽,而在右邊內容頁面中,"Enable running application out of the browser"是沒有被選中的,我們僅需要選中該選項,保存,即可設置當前Silverlight項目支持Out of Browser。

  在"Enable running application out of the browser"選項下,可以看到一個Out-of-Browser Settings...按鈕,點擊進行該按鈕,即可對Out-of-Browser進行設置:

  從上圖可以看出,開發人員可以通過這些屬性,創建個性的Out of Browser應用。以上設置屬性是保存在Visual Studio 2010中的OutOfBrowserSettings.xml文件中的,開發人員也可以通過修改該文件來設置OOB應用屬性。

1 <OutOfBrowserSettings ShortName="SilverlightOOBDemo Application" EnableGPUAcceleration="False" ShowInstallMenuItem="True">
2   <OutOfBrowserSettings.Blurb>SilverlightOOBDemo Application on your desktop; at home, at work or on the go.</OutOfBrowserSettings.Blurb>
3   <OutOfBrowserSettings.WindowSettings>
4     <WindowSettings Title="SilverlightOOBDemo Application" />
5   </OutOfBrowserSettings.WindowSettings>
6   <OutOfBrowserSettings.Icons />
7  </OutOfBrowserSettings>

  在完成以上設置后,點擊確定和保存,該項目即可支持Out of Browser模式。

  Silverlight的Out of Browser應用安裝

  Silverlight Out of Browser應用的安裝很簡單,作為開發人員我們可以通過兩種方式提供給用戶安裝OOB應用到本地。

  第一種方式是使用默認的右鍵菜單按鈕安裝應用到本地。

  這種方式是Out of Browser默認的安裝方式,但是該方式的弊端是不易與用戶體驗,每次用戶要右鍵點擊應用才能安裝應用。作為專業Out of Browser應用,通常會使用第二種方式安裝OOB應用到本地。

  第二種方式,添加控件通過Application.Current.Install()事件安裝應用到本地。

  在當前應用的MainPage下,添加安裝按鈕,通過按鈕點擊事件安裝應用到本地。

1 <Grid x:Name="LayoutRoot" Background="DimGray">
2         <Button x:Name="btInstall" Content="安裝應用到本地" Width="200" Height="50" Click="btInstall_Click"/>
3  </Grid>

 

 1 private void btInstall_Click(object sender, RoutedEventArgs e)
 2         {
 3             try 
 4             { 
 5                 Application.Current.Install(); 
 6             }
 7             catch (InvalidOperationException ex) 
 8             { 
 9                 MessageBox.Show("應用已經安裝."); 
10             }
11             catch (Exception ex) 
12             { 
13                 MessageBox.Show("應用不能被安裝,錯誤信息如下:" + Environment.NewLine + ex.Message); 
14             } 
15         }

  通過上面簡單代碼也可以達到安裝OOB應用到本地的效果。

  對于較為專業的Out of Browser應用的安裝,我們經常會添加一些代碼對當前應用安裝進行簡單的判斷,判斷該應用是否已經被安裝到了本地,如果已經安裝,將忽略不再進行安裝步驟。這是對OOB應用的一種保護措施。我們簡單修改項目代碼。

 1 public MainPage()
 2         {
 3             InitializeComponent();
 4 
 5             if (Application.Current.IsRunningOutOfBrowser)
 6             {
 7                 btInstall.Visibility = Visibility.Collapsed;
 8                 lbStatus.Text = "我正在Out of Browser下運行";
 9             }
10             else
11             {
12                 btInstall.Visibility = Visibility.Visible;
13                 lbStatus.Text = "我正在瀏覽器中運行";
14             }
15 
16             if (Application.Current.InstallState != InstallState.Installed)
17             {
18                 btInstall.IsEnabled = true;
19 
20             }
21             else
22             {
23                 btInstall.IsEnabled = false;
24                 btInstall.Content = "應用已經安裝到本地";
25             }
26 
27         }

  安裝本地前:

  安裝本地后:

  重復安裝時:

  對于安裝時所處于的狀態控制,我們可以通過InstallState進行判斷。我們可以通過添加以下代碼:

 1 private void Current_InstallStateChanged(object sender, System.EventArgs e)
 2         {
 3             switch (Application.Current.InstallState)
 4             {
 5                 case InstallState.Installing:
 6                     btInstall.IsEnabled = false;
 7                     btInstall.Content = "正在安裝...";
 8                     break;
 9 
10                 case InstallState.Installed:
11                     btInstall.IsEnabled = false;
12                     btInstall.Content = "已經安裝";
13                     MessageBox.Show("OOB應用已經安裝到本地");
14                     break;
15 
16                 case InstallState.NotInstalled:
17                     btInstall.IsEnabled = true;
18                     btInstall.Content = "點擊安裝該應用到本地";
19                     break;
20 
21                 case InstallState.InstallFailed:
22                     MessageBox.Show("OOB應用安裝失敗");
23                     btInstall.IsEnabled = false;
24                     break;
25             }
26         }

  當安裝時,用戶可以看到提示:

  以上是Silverlight Out of Browser安裝方法和一些控制技巧。

  Silverlight的Out of Browser應用卸載

  Silverlight的OOB應用卸載同樣很簡單,Silverlight沒有和安裝時候的Install API,所以我們無法通過代碼的方式控制卸載,但是可以通過以下兩種方式卸載應用:

  1. 右鍵點擊應用,選擇卸載應用選項;

  2. 通過Windows“控制面板",選擇對應應用進行卸載,這個是傳統型卸載方法,這里不再贅述。

  簡單實例

  在這個簡單實例中,我將在當前的OOB應用中添加一個簡單的網絡監測代碼,演示該應用在線和離線時的網絡狀態。在該應用,我們仍舊會使用System.Windows.Application API來判斷應用是否離線安裝,而我們還會使用System.Net.NetworkInformation API來判斷其網絡狀。簡單修改代碼如下:

 1 <UserControl x:Class="SilverlightOOBDemo.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     mc:Ignorable="d"
 7     d:DesignHeight="300" d:DesignWidth="400">
 8 
 9     <Grid x:Name="LayoutRoot" Background="DimGray">
10         <StackPanel Orientation="Vertical">
11             <Button x:Name="btInstall" Content="安裝應用到本地" Width="200" Height="50" Click="btInstall_Click"/>
12             <TextBlock x:Name="lbStatus" Foreground="White" HorizontalAlignment="Center" FontSize="18"/>
13             <TextBlock x:Name="lbNetworkStatus" Foreground="LightGreen" HorizontalAlignment="Center" FontSize="18"/>
14         </StackPanel>
15     </Grid>
16  </UserControl>
17  

 

 1 private void CheckNetworkStatus()
 2         {
 3             if (NetworkInterface.GetIsNetworkAvailable())
 4             {
 5                 lbNetworkStatus.Foreground = new SolidColorBrush(Color.FromArgb(255,90,240,90));
 6                 lbNetworkStatus.Text = "當前網絡處于連接狀態";
 7             }
 8             else
 9             {
10                 lbNetworkStatus.Foreground = new SolidColorBrush(Colors.Red);
11                 lbNetworkStatus.Text = "當前網絡處于斷線狀態";
12             }
13         }
14 
15         private void NetworkChange_NetworkAddressChanged(object sender, EventArgs e)
16         {
17             CheckNetworkStatus();
18         }

修改構造函數代碼:

 1  public MainPage()
 2         {
 3             InitializeComponent();
 4 
 5             if (Application.Current.IsRunningOutOfBrowser)
 6             {
 7                 btInstall.Visibility = Visibility.Collapsed;
 8                 lbStatus.Text = "我正在Out of Browser下運行";
 9             }
10             else
11             {
12                 btInstall.Visibility = Visibility.Visible;
13                 lbStatus.Text = "我正在瀏覽器中運行";
14             }
15 
16             if (Application.Current.InstallState != InstallState.Installed)
17             {
18                 btInstall.IsEnabled = true;
19 
20             }
21             else
22             {
23                 btInstall.IsEnabled = false;
24                 btInstall.Content = "應用已經安裝到本地";
25             }
26 
27             CheckNetworkStatus();
28 
29             Application.Current.InstallStateChanged += Current_InstallStateChanged;
30             NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(NetworkChange_NetworkAddressChanged);
31 
32         }

  運行后可以在離線狀態下,查看網絡應用狀態:

  本文主要講述Silverlight的Out of Browser應用設置,安裝和卸載,屬于Silverlight實例開發前的基礎,下一篇我將繼續介紹Silverlight的Out of Browser應用開發基礎。

  本篇代碼下載

0
0
 
 
 

文章列表

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()