Silverlight - Out of Browser配置,安装和卸载
Silverlight 實(shí)例教程索引
- Silverlight 實(shí)例教程 - Out of Browser開篇
- Silverlight 實(shí)例教程 - Out of Browser配置,安裝和卸載
- Silverlight 實(shí)例教程 - Out of Browser的自定義應(yīng)用
- Silverlight 實(shí)例教程 - Out of Browser存取本地文件系統(tǒng)
- Silverlight 實(shí)例教程 - Out of Browser與COM的交互基礎(chǔ)
- Silverlight 實(shí)例教程 - Out of Browser與Office的互操作
- Silverlight 實(shí)例教程 - Out of Browser的Debug和Notifications窗口
- Silverlight 實(shí)例教程 - Out of Browser音樂播放器
- Silverlight 實(shí)例教程 - Out of Browser與COM互操作實(shí)例
- Silverlight 實(shí)例教程 - Out of Browser在線更新和Silent安裝
本篇將介紹Silverlight的Out of Browser在實(shí)例開發(fā)前的基礎(chǔ)設(shè)置以及一些開發(fā)技巧。
在創(chuàng)建Silverlight項(xiàng)目時(shí),默認(rèn)是不支持Out of Browser模式的,所以在使用Silverilght的Out of Browser前,需要對(duì)Silverlight項(xiàng)目進(jìn)行設(shè)置,使其支持OOB項(xiàng)目安裝和運(yùn)行。在以下演示中,將創(chuàng)建一個(gè)演示例程,該例程將在后續(xù)教程中使用,由于該教程不屬于Silverilght基礎(chǔ)開發(fā)教程,所以這里,我不在細(xì)述如何創(chuàng)建Silverlight項(xiàng)目,
項(xiàng)目名稱: SilverilghtOOBDemo
項(xiàng)目環(huán)境:VS2010 + Silverlight 4
Silverlight的Out of Browser應(yīng)用設(shè)置
在Silverlight項(xiàng)目中,設(shè)置項(xiàng)目支持Out of Browser模式非常簡(jiǎn)單,首先右鍵選中SilverlightOOBDemo客戶端項(xiàng)目,選擇“Properties”屬性
在項(xiàng)目屬性欄,默認(rèn)情況下已經(jīng)選擇了Silverlight標(biāo)簽,而在右邊內(nèi)容頁面中,"Enable running application out of the browser"是沒有被選中的,我們僅需要選中該選項(xiàng),保存,即可設(shè)置當(dāng)前Silverlight項(xiàng)目支持Out of Browser。
在"Enable running application out of the browser"選項(xiàng)下,可以看到一個(gè)Out-of-Browser Settings...按鈕,點(diǎn)擊進(jìn)行該按鈕,即可對(duì)Out-of-Browser進(jìn)行設(shè)置:
從上圖可以看出,開發(fā)人員可以通過這些屬性,創(chuàng)建個(gè)性的Out of Browser應(yīng)用。以上設(shè)置屬性是保存在Visual Studio 2010中的OutOfBrowserSettings.xml文件中的,開發(fā)人員也可以通過修改該文件來設(shè)置OOB應(yīng)用屬性。
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>
在完成以上設(shè)置后,點(diǎn)擊確定和保存,該項(xiàng)目即可支持Out of Browser模式。
Silverlight的Out of Browser應(yīng)用安裝
Silverlight Out of Browser應(yīng)用的安裝很簡(jiǎn)單,作為開發(fā)人員我們可以通過兩種方式提供給用戶安裝OOB應(yīng)用到本地。
第一種方式是使用默認(rèn)的右鍵菜單按鈕安裝應(yīng)用到本地。
這種方式是Out of Browser默認(rèn)的安裝方式,但是該方式的弊端是不易與用戶體驗(yàn),每次用戶要右鍵點(diǎn)擊應(yīng)用才能安裝應(yīng)用。作為專業(yè)Out of Browser應(yīng)用,通常會(huì)使用第二種方式安裝OOB應(yīng)用到本地。
第二種方式,添加控件通過Application.Current.Install()事件安裝應(yīng)用到本地。
在當(dāng)前應(yīng)用的MainPage下,添加安裝按鈕,通過按鈕點(diǎn)擊事件安裝應(yīng)用到本地。
1?<Grid?x:Name="LayoutRoot"?Background="DimGray">2?????????<Button?x:Name="btInstall"?Content="安裝應(yīng)用到本地"?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("應(yīng)用已經(jīng)安裝.");?
10?????????????}
11?????????????catch?(Exception?ex)?
12?????????????{?
13?????????????????MessageBox.Show("應(yīng)用不能被安裝,錯(cuò)誤信息如下:"?+?Environment.NewLine?+?ex.Message);?
14?????????????}?
15?????????}
通過上面簡(jiǎn)單代碼也可以達(dá)到安裝OOB應(yīng)用到本地的效果。
對(duì)于較為專業(yè)的Out of Browser應(yīng)用的安裝,我們經(jīng)常會(huì)添加一些代碼對(duì)當(dāng)前應(yīng)用安裝進(jìn)行簡(jiǎn)單的判斷,判斷該應(yīng)用是否已經(jīng)被安裝到了本地,如果已經(jīng)安裝,將忽略不再進(jìn)行安裝步驟。這是對(duì)OOB應(yīng)用的一種保護(hù)措施。我們簡(jiǎn)單修改項(xiàng)目代碼。
?1?public?MainPage()?2?????????{
?3?????????????InitializeComponent();
?4?
?5?????????????if?(Application.Current.IsRunningOutOfBrowser)
?6?????????????{
?7?????????????????btInstall.Visibility?=?Visibility.Collapsed;
?8?????????????????lbStatus.Text?=?"我正在Out?of?Browser下運(yùn)行";
?9?????????????}
10?????????????else
11?????????????{
12?????????????????btInstall.Visibility?=?Visibility.Visible;
13?????????????????lbStatus.Text?=?"我正在瀏覽器中運(yùn)行";
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?=?"應(yīng)用已經(jīng)安裝到本地";
25?????????????}
26?
27?????????}
安裝本地前:
安裝本地后:
重復(fù)安裝時(shí):
對(duì)于安裝時(shí)所處于的狀態(tài)控制,我們可以通過InstallState進(jìn)行判斷。我們可以通過添加以下代碼:
?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?=?"已經(jīng)安裝";
13?????????????????????MessageBox.Show("OOB應(yīng)用已經(jīng)安裝到本地");
14?????????????????????break;
15?
16?????????????????case?InstallState.NotInstalled:
17?????????????????????btInstall.IsEnabled?=?true;
18?????????????????????btInstall.Content?=?"點(diǎn)擊安裝該應(yīng)用到本地";
19?????????????????????break;
20?
21?????????????????case?InstallState.InstallFailed:
22?????????????????????MessageBox.Show("OOB應(yīng)用安裝失敗");
23?????????????????????btInstall.IsEnabled?=?false;
24?????????????????????break;
25?????????????}
26?????????}
當(dāng)安裝時(shí),用戶可以看到提示:
以上是Silverlight Out of Browser安裝方法和一些控制技巧。
Silverlight的Out of Browser應(yīng)用卸載
Silverlight的OOB應(yīng)用卸載同樣很簡(jiǎn)單,Silverlight沒有和安裝時(shí)候的Install API,所以我們無法通過代碼的方式控制卸載,但是可以通過以下兩種方式卸載應(yīng)用:
1. 右鍵點(diǎn)擊應(yīng)用,選擇卸載應(yīng)用選項(xiàng);
2. 通過Windows“控制面板",選擇對(duì)應(yīng)應(yīng)用進(jìn)行卸載,這個(gè)是傳統(tǒng)型卸載方法,這里不再贅述。
簡(jiǎn)單實(shí)例
在這個(gè)簡(jiǎn)單實(shí)例中,我將在當(dāng)前的OOB應(yīng)用中添加一個(gè)簡(jiǎn)單的網(wǎng)絡(luò)監(jiān)測(cè)代碼,演示該應(yīng)用在線和離線時(shí)的網(wǎng)絡(luò)狀態(tài)。在該應(yīng)用,我們?nèi)耘f會(huì)使用System.Windows.Application API來判斷應(yīng)用是否離線安裝,而我們還會(huì)使用System.Net.NetworkInformation API來判斷其網(wǎng)絡(luò)狀。簡(jiǎn)單修改代碼如下:
?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="安裝應(yīng)用到本地"?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?=?"當(dāng)前網(wǎng)絡(luò)處于連接狀態(tài)";
?7?????????????}
?8?????????????else
?9?????????????{
10?????????????????lbNetworkStatus.Foreground?=?new?SolidColorBrush(Colors.Red);
11?????????????????lbNetworkStatus.Text?=?"當(dāng)前網(wǎng)絡(luò)處于斷線狀態(tài)";
12?????????????}
13?????????}
14?
15?????????private?void?NetworkChange_NetworkAddressChanged(object?sender,?EventArgs?e)
16?????????{
17?????????????CheckNetworkStatus();
18?????????}
修改構(gòu)造函數(shù)代碼:
?1??public?MainPage()?2?????????{
?3?????????????InitializeComponent();
?4?
?5?????????????if?(Application.Current.IsRunningOutOfBrowser)
?6?????????????{
?7?????????????????btInstall.Visibility?=?Visibility.Collapsed;
?8?????????????????lbStatus.Text?=?"我正在Out?of?Browser下運(yùn)行";
?9?????????????}
10?????????????else
11?????????????{
12?????????????????btInstall.Visibility?=?Visibility.Visible;
13?????????????????lbStatus.Text?=?"我正在瀏覽器中運(yùn)行";
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?=?"應(yīng)用已經(jīng)安裝到本地";
25?????????????}
26?
27?????????????CheckNetworkStatus();
28?
29?????????????Application.Current.InstallStateChanged?+=?Current_InstallStateChanged;
30?????????????NetworkChange.NetworkAddressChanged?+=?new?NetworkAddressChangedEventHandler(NetworkChange_NetworkAddressChanged);
31?
32?????????}
運(yùn)行后可以在離線狀態(tài)下,查看網(wǎng)絡(luò)應(yīng)用狀態(tài):
本文主要講述Silverlight的Out of Browser應(yīng)用設(shè)置,安裝和卸載,屬于Silverlight實(shí)例開發(fā)前的基礎(chǔ),下一篇我將繼續(xù)介紹Silverlight的Out of Browser應(yīng)用開發(fā)基礎(chǔ)。
轉(zhuǎn)載于:https://www.cnblogs.com/jiewei915/archive/2013/01/10/2854967.html
總結(jié)
以上是生活随笔為你收集整理的Silverlight - Out of Browser配置,安装和卸载的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: WPF 用户控件的使用
- 下一篇: SuperSocket 1.5 Docu