Silverlight實例教程 - Out of Browser的Debug和Notifications窗口
Silverlight 實例教程索引
- Silverlight 實例教程 - Out of Browser開篇
- Silverlight 實例教程 - Out of Browser配置,安裝和卸載
- Silverlight 實例教程 - Out of Browser的自定義應用
- Silverlight 實例教程 - Out of Browser存取本地文件系統
- Silverlight 實例教程 - Out of Browser與COM的交互基礎
- Silverlight 實例教程 - Out of Browser與Office的互操作
- Silverlight 實例教程 - Out of Browser的Debug和Notifications窗口
- Silverlight 實例教程 - Out of Browser音樂播放器
- Silverlight 實例教程 - Out of Browser與COM互操作實例
- Silverlight 實例教程 - Out of Browser在線更新和Silent安裝
熟悉Silverlight的朋友應該知道,Silverlight從1.0版本到現在的4.0版本,其功能性越來越強大,從下圖我們可以看出,Silverlight的應用模型的一個轉變過程,從Javascript到現在Trusted應用,我們目睹了Silverlight坎坷的演變過程,盡管現在仍舊存在不足之處,但是有了更多開發人員的支持和幫助,Silverlight一定會更好更強大。
在前幾篇中,我們通過簡單的實例詳細介紹了Silverlight Out of Browser應用開發基礎。為了下一篇的實例做準備,本篇,我們將補充介紹一些Silverlight Out of Browser應用開發知識點。
1. 回顧Silverlight Out of Browser存取本地目錄API;
2. 學習Silverlight Out of Browser應用Debug調試;
3. 學習Silverlight Out of Browser的消息通知窗口API;
回顧Silverlight Out of Browser存取本地目錄API,還記得在前面的文章我們已經介紹,Silverlight提供有現成的API,允許應用在OOB模式下訪問My...系列目錄,API調用方法很簡單,而OOB模式下文件訪問,應用可以支持System.IO引用空間中的操作類,例如File類,Directory類,Environment類,等等。
2 {
3 var path = Environment.GetFolderPath(Environment.SpecialFolder.MyMusic);
4 lsMyMusics.Items.Clear();
5 DirectoryInfo myDirectory = new DirectoryInfo(path);
6 foreach (FileInfo file in myDirectory.EnumerateFiles())
7 {
8 lsMyMusics.Items.Add(file);
9 }
10 }
在下文中,我們將用到Silverlight默認API,讀取My Music目錄的音樂文件作為默認播放目錄。其代碼與上相似。
Silverlight Out of Browser應用的調試方法(Debug)
在使用Silverlight開發應用時,Debug是最常用的Visual Studio工具之一。大家可能對Silverlight基于Web瀏覽器的調試方法并不陌生,終歸ASP.NET應用開發調試已經為Silverlight打下了良好的基礎。而對于Silverlight的OOB是否通常支持Debug呢?答案是肯定的。在創建項目時,默認的設置,是支持基于瀏覽器的應用的Debug。如果需要OOB應用支持脫離瀏覽器進行Debug,需要按照以下幾個步驟設置:
1. 首先需要設置Silverlight客戶端應用為“開始項目”。
2. 然后選擇客戶端應用“Properties”屬性欄,設置“Start Action”中的“Installed out-of-browser application”。
3. 點擊保存后,重新F5調試應用,即可發現,應用將直接作為OOB模式啟動,不再進入Web瀏覽器中提示用戶安裝,這時就可以在代碼中設置斷點進行Debug了。
學習Silverlight Out of Browser的消息通知窗口API(Toast Notifications Windows)
Toast Notifications Windows,又稱為Silverlight消息通知窗口,是Silverlight 4的一個新特性,該窗口目前僅限于Out of Browser應用使用。該消息窗口主要是提供臨時消息提示,在應用中可以起到讓用戶注意警示的作用。現在很多Windows應用都喜歡使用該窗口模式顯示廣告,更新提示等功能。
Silverlight的Notifications Windows目前有以下限制:
1. 窗口最大尺寸限制,最大僅支持寬400,高100的提示窗口;
2. 目前不支持Transparency窗口特效,WPF可以支持;
3. 為了區別其他窗口應用,Notifications Windows無窗口邊框;
在明白以上限制后,使用Silverlight API很輕松就能創建一個Toast Notifications窗口。首先,在SilverlightOOBDemo中創建一個NotificationControl控件。
編輯NotificationControl控件,我們簡單的對該控件進行美化,使其看起來更加友好。
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="White">
10 <Border x:Name="Frame" Width="300" Height="100" Background="LightYellow">
11 <StackPanel Orientation="Vertical">
12 <Border Width="290" Height="24" CornerRadius="4" Margin="2,4,2,4">
13 <Border.Background>
14 <LinearGradientBrush StartPoint="0.5,0.0"
15 EndPoint="0.5,1.0">
16 <GradientStop Offset="0.2" Color="#FF1C68A0" />
17 <GradientStop Offset="1.0" Color="#FF54A7E2" />
18 </LinearGradientBrush>
19 </Border.Background>
20 <Border.Effect>
21 <DropShadowEffect BlurRadius="4" ShadowDepth="4"
22 Opacity="0.4" />
23 </Border.Effect>
24 <TextBlock Text="友情提示" FontSize="12"
25 FontWeight="Bold" Foreground="White" Margin="4" />
26 </Border>
27 <StackPanel Orientation="Horizontal">
28 <Image Source="/SilverlightOOBDemo;component/Images/Update.png" Width="48" Height="48"
29 Stretch="Fill" Margin="4" VerticalAlignment="Top" />
30 <TextBlock Width="240" Text="檢測到新的音樂文件,已經更新到播放列表。小廣告:我的博客http://jv9.cnblogs.com"
31 FontSize="11" Foreground="#FF202020" TextWrapping="Wrap"
32 Margin="4" />
33 </StackPanel>
34 </StackPanel>
35 </Border>
36 </Grid>
37 </UserControl>
38
然后回到OutofBrowserMainPage頁面,這里,我們在“關于”按鈕上,添加Click事件響應,使其被點擊后,彈出Notifications窗口。
首先創建notifyWindow實例。
2 Window OOBWindow = Application.Current.MainWindow;
3 NotificationWindow notifyWindow = null;
4 #endregion
5
6 #region Constructor
7 public OutofBrowserMainPage()
8 {
9 InitializeComponent();
10 notifyWindow = new NotificationWindow();
11
12
13 }
14 #endregion
然后在Click事件中進行窗口激活:
2 {
3 if (null == notifyWindow)
4 MessageBox.Show("通告窗口僅能運行在OOB模式下,請安裝Silverlight應用到本地。");
5
6 if (true == App.Current.IsRunningOutOfBrowser)
7 {
8 if (notifyWindow.Visibility == Visibility.Visible)
9 notifyWindow.Close();
10
11 NotificationControl myNotify = new NotificationControl();
12 notifyWindow.Width = 300;
13 notifyWindow.Height = 100;
14 notifyWindow.Content = myNotify;
15 notifyWindow.Show(10000);
16 }
17
18 }
在上面代碼中,我們創建了一個新的Notification窗口實例,然后使用Show(毫秒),使其顯示在客戶端,最終顯示效果如下:
今天的內容暫時到這里了,下一篇,我們將綜合使用這些Silverlight OOB應用開發技巧實現一個完整應用實例, Silverlight Out of Browser音樂播放器。