[源碼下載下傳]
穩紮穩打Silverlight(44) - 4.0浏覽器外運作(Out Of Browser)之OOB的增強及其新增的NotificationWindow
作者: webabcd
介紹
Silverlight 4.0 OOB 模式的新特性:
- 新增了 Closing 事件
- 實作程式在 OOB 模式下的自動更新
- NotificationWindow - 在 OOB 模式下顯示通知視窗,也就是 toast
- 實作自定義的 NotificationWindow
線上DEMO
http://www.cnblogs.com/webabcd/archive/2010/08/09/1795417.html
示例
1、OOB(Out Of Browser)模式的複習,以及其新增的 Closing 事件
Demo.xaml
代碼 < navigation:Page x:Class ="Silverlight40.OutOfBrowser.Demo"
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"
xmlns:navigation ="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
Title ="Other Page" >
< Grid x:Name ="LayoutRoot" >
< StackPanel HorizontalAlignment ="Left" >
< Button Name ="btnOutOfBrowser" Margin ="50" Click ="btnOutOfBrowser_Click" />
< Button Name ="btnClose" Margin ="50" Content ="關閉" Click ="btnClose_Click" />
< TextBlock Text ="OOB 模式下網絡狀況的檢測,以及程式的更新詳見本目錄下的 AutoUpdateDemo.xaml" Margin ="5" />
< TextBlock Text ="OOB 模式的 toast(以及與 Popup 的對比)詳見本目錄下的 NotificationWindowDemo.xaml" Margin ="5" />
< TextBlock Text ="OOB 模式下自定義 NotificationWindow 的内容及樣式詳見本目錄下的 CustomNotificationWindowDemo.xaml" Margin ="5" />
</ StackPanel >
</ Grid >
</ navigation:Page >
Demo.xaml.cs
代碼
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.Windows.Navigation;
namespace Silverlight40.OutOfBrowser
{
public partial class Demo : Page
{
public Demo()
{
InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (App.Current.IsRunningOutOfBrowser)
btnOutOfBrowser.Content = " 解除安裝 " ;
else
btnOutOfBrowser.Content = " 安裝 " ;
Init();
}
private void btnOutOfBrowser_Click( object sender, RoutedEventArgs e)
{
if ( ! App.Current.IsRunningOutOfBrowser && App.Current.InstallState == InstallState.NotInstalled)
App.Current.Install();
else
MessageBox.Show( " 已經安裝,使用右鍵解除安裝 " );
}
void Init()
{
if (Application.Current.IsRunningOutOfBrowser)
Application.Current.MainWindow.Closing += new EventHandler < System.ComponentModel.ClosingEventArgs > (MainWindow_Closing);
}
void MainWindow_Closing( object sender, System.ComponentModel.ClosingEventArgs e)
{
if (MessageBox.Show( " 确認關閉嗎? " , " 确認 " , MessageBoxButton.OKCancel) == MessageBoxResult.OK)
{
}
else
{
if (e.IsCancelable)
e.Cancel = true ;
}
}
private void btnClose_Click( object sender, RoutedEventArgs e)
{
if (Application.Current.IsRunningOutOfBrowser)
Application.Current.MainWindow.Close();
}
}
}
2、示範如何實作程式在 OOB 模式下的自動更新
AutoUpdateDemo.xaml 代碼 < navigation:Page x:Class ="Silverlight40.OutOfBrowser.AutoUpdateDemo"
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"
xmlns:navigation ="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
Title ="AutoUpdateDemo Page" >
< Grid x:Name ="LayoutRoot" >
< StackPanel >
< Button Name ="btnOutOfBrowser" Margin ="50" Click ="btnOutOfBrowser_Click" />
< TextBlock Name ="lblMsg" />
</ StackPanel >
</ Grid >
</ navigation:Page >
AutoUpdateDemo.xaml.cs 代碼
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.Windows.Navigation;
using System.Net.NetworkInformation;
namespace Silverlight40.OutOfBrowser
{
public partial class AutoUpdateDemo : Page
{
public AutoUpdateDemo()
{
InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (App.Current.IsRunningOutOfBrowser)
btnOutOfBrowser.Content = " 解除安裝 " ;
else
btnOutOfBrowser.Content = " 安裝 " ;
Init();
}
private void btnOutOfBrowser_Click( object sender, RoutedEventArgs e)
{
if ( ! App.Current.IsRunningOutOfBrowser && App.Current.InstallState == InstallState.NotInstalled)
App.Current.Install();
else
MessageBox.Show( " 已經安裝,使用右鍵解除安裝 " );
}
private void Init()
{
lblMsg.Text = string .Format( " 網絡狀态:{0} " , NetworkInterface.GetIsNetworkAvailable().ToString());
// 網絡狀态發生改變時所觸發的事件,可以拔網線看效果
NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(NetworkChange_NetworkAddressChanged);
if (App.Current.IsRunningOutOfBrowser && NetworkInterface.GetIsNetworkAvailable())
{
App.Current.CheckAndDownloadUpdateCompleted += new CheckAndDownloadUpdateCompletedEventHandler(Current_CheckAndDownloadUpdateCompleted);
App.Current.CheckAndDownloadUpdateAsync();
lblMsg.Text += " /n檢查更新。。。 " ;
}
}
void NetworkChange_NetworkAddressChanged( object sender, EventArgs e)
{
lblMsg.Text += " /n " ;
lblMsg.Text += string .Format( " 網絡狀态:{0} " , NetworkInterface.GetIsNetworkAvailable().ToString());
}
void Current_CheckAndDownloadUpdateCompleted( object sender, CheckAndDownloadUpdateCompletedEventArgs e)
{
if (e.UpdateAvailable)
{
lblMsg.Text += " /n更新完畢,請重新開機程式 " ;
}
else
{
if (e.Error == null )
lblMsg.Text += " /n程式無更新(如果有新更新要修改 Assembly Version) " ;
else
lblMsg.Text += " /n " + e.Error.ToString();
}
}
}
}
3、示範 NotificationWindow(toast) 的效果,以及其和 Popup 的對比
NotificationWindowDemo.xaml 代碼 < navigation:Page x:Class ="Silverlight40.OutOfBrowser.NotificationWindowDemo"
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"
xmlns:navigation ="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
Title ="NotificationWindowDemo Page" >
< Grid x:Name ="LayoutRoot" >
< StackPanel HorizontalAlignment ="Left" >
< Button Name ="btnOutOfBrowser" Margin ="50" Click ="btnOutOfBrowser_Click" />
<!-- Popup 的 Demo -->
< Button Name ="btnShowPopup" Margin ="5" Content ="彈出 Popup" Click ="btnShowPopup_Click" />
<!-- NotificationWindow 的 Demo -->
< Button Name ="btnShowNotificationWindow" Margin ="5" Content ="彈出 NotificationWindow" Click ="btnShowNotificationWindow_Click" />
</ StackPanel >
</ Grid >
</ navigation:Page >
NotificationWindowDemo.xaml.cs 代碼
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.Windows.Navigation;
using System.Windows.Controls.Primitives;
namespace Silverlight40.OutOfBrowser
{
public partial class NotificationWindowDemo : Page
{
public NotificationWindowDemo()
{
InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (App.Current.IsRunningOutOfBrowser)
btnOutOfBrowser.Content = " 解除安裝 " ;
else
btnOutOfBrowser.Content = " 安裝 " ;
}
private void btnOutOfBrowser_Click( object sender, RoutedEventArgs e)
{
if ( ! App.Current.IsRunningOutOfBrowser && App.Current.InstallState == InstallState.NotInstalled)
App.Current.Install();
else
MessageBox.Show( " 已經安裝,使用右鍵解除安裝 " );
}
private void btnShowPopup_Click( object sender, RoutedEventArgs e)
{
Popup popup = new Popup();
Border border = new Border();
border.BorderBrush = new SolidColorBrush(Colors.Black);
border.BorderThickness = new Thickness( 5.0 );
StackPanel container = new StackPanel();
container.Width = 320 ;
container.Height = 240 ;
container.Background = new SolidColorBrush(Colors.Yellow);
TextBlock lblMsg = new TextBlock();
lblMsg.Text = " Popup 資訊 " ;
Button btnClose = new Button();
btnClose.Content = " 關閉 " ;
btnClose.Click += (x, y) => { popup.IsOpen = false ; };
container.Children.Add(lblMsg);
container.Children.Add(btnClose);
border.Child = container;
popup.Child = border;
popup.VerticalOffset = 100 ;
popup.HorizontalOffset = 100 ;
popup.IsOpen = true ;
}
private void btnShowNotificationWindow_Click( object sender, RoutedEventArgs e)
{
if (App.Current.IsRunningOutOfBrowser)
{
NotificationWindow notify = new NotificationWindow();
notify.Width = 320 ;
notify.Height = 80 ;
TextBlock lblMsg = new TextBlock();
lblMsg.Text = " NotificationWindow 資訊 " ;
lblMsg.FontSize = 18 ;
notify.Content = lblMsg;
notify.Show( 3000 );
}
else
{
MessageBox.Show( " 請在浏覽器外運作 " );
}
}
}
}
4、示範如何實作自定義的 NotificationWindow
CustomNotificationWindow.cs 代碼
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace Silverlight40.OutOfBrowser
{
public class CustomNotificationWindow : ContentControl
{
public CustomNotificationWindow()
{
// 設定控件的預設樣式
this .DefaultStyleKey = typeof (CustomNotificationWindow);
}
// toast 的标題
public string Title
{
get { return ( string )GetValue(CustomNotificationWindow.TitleProperty); }
set { SetValue(CustomNotificationWindow.TitleProperty, value); }
}
public static readonly DependencyProperty TitleProperty = DependencyProperty.Register
(
" Title " ,
typeof ( string ),
typeof (CustomNotificationWindow),
new PropertyMetadata(OnTitlePropertyChanged)
);
private static void OnTitlePropertyChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
{
}
// toast 的正文
public string Text
{
get { return ( string )GetValue(CustomNotificationWindow.TextProperty); }
set { SetValue(CustomNotificationWindow.TextProperty, value); }
}
public static readonly DependencyProperty TextProperty = DependencyProperty.Register
(
" Text " ,
typeof ( string ),
typeof (CustomNotificationWindow),
new PropertyMetadata(OnTextPropertyChanged)
);
private static void OnTextPropertyChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
{
}
// UI 元素在應用程式中顯示之前所調用的方法
public override void OnApplyTemplate()
{
base .OnApplyTemplate();
// Control.GetTemplateChild() - 在執行個體化的 ControlTemplate 可視化樹中檢索已命名的元素
Button btnClose = GetTemplateChild( " btnClose " ) as Button;
if (btnClose != null )
{
btnClose.Click += (s, e) =>
{
EventHandler < EventArgs > handler = this .Closed;
if (handler != null )
handler( this , EventArgs.Empty);
};
}
}
public event EventHandler < EventArgs > Closed;
}
}
CustomNotificationWindowDemo.xaml 代碼 < navigation:Page x:Class ="Silverlight40.OutOfBrowser.CustomNotificationWindowDemo"
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"
xmlns:navigation ="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
Title ="CustomNotificationWindowDemo Page" >
< Grid x:Name ="LayoutRoot" >
< StackPanel HorizontalAlignment ="Left" >
< Button Name ="btnOutOfBrowser" Margin ="50" Click ="btnOutOfBrowser_Click" />
<!-- 自定義 NotificationWindow 的 Demo -->
< Button Name ="btnShowCustomNotificationWindow" Margin ="5" Content ="彈出 CustomNotificationWindow" Click ="btnShowCustomNotificationWindow_Click" />
</ StackPanel >
</ Grid >
</ navigation:Page >
CustomNotificationWindowDemo.xaml.cs 代碼
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.Windows.Navigation;
namespace Silverlight40.OutOfBrowser
{
public partial class CustomNotificationWindowDemo : Page
{
public CustomNotificationWindowDemo()
{
InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (App.Current.IsRunningOutOfBrowser)
btnOutOfBrowser.Content = " 解除安裝 " ;
else
btnOutOfBrowser.Content = " 安裝 " ;
}
private void btnOutOfBrowser_Click( object sender, RoutedEventArgs e)
{
if ( ! App.Current.IsRunningOutOfBrowser && App.Current.InstallState == InstallState.NotInstalled)
App.Current.Install();
else
MessageBox.Show( " 已經安裝,使用右鍵解除安裝 " );
}
private void btnShowCustomNotificationWindow_Click( object sender, RoutedEventArgs e)
{
// 執行個體化一個 NotificationWindow,并指定其寬和高
NotificationWindow notify = new NotificationWindow();
notify.Width = 320 ;
notify.Height = 80 ;
// 将 NotificationWindow 的顯示内容設定為自定義的内容
CustomNotificationWindow custom = new CustomNotificationWindow();
custom.Title = " 我是标題 " ;
custom.Text = " 我是内容 " ;
custom.Width = notify.Width;
custom.Height = notify.Height;
custom.Closed += (x, y) => { notify.Close(); };
notify.Content = custom;
notify.Show( 3000 );
}
}
}
OK
[源碼下載下傳] <script type="text/javascript"> if ($ != jQuery) { $ = jQuery.noConflict(); } </script>