稳扎稳打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
代码 /*
* Silverlight 3.0 时代具有如下特性
* Application.InstallStateChanged - 浏览器外运行的相关状态发生改变时所触发的事件
* Application.InstallState - 浏览器外运行的相关状态 [System.windows.InstallState 枚举]
* notinstalled - 在浏览器中运行
* Installing - 安装到桌面中
* Installed - 在浏览器外运行
* InstallFailed - 安装到桌面的过程中发生错误
* Application.IsRunningOutOfbrowser - 当前程序是否是从浏览器外启动的
* Application.Install() - 安装 Silverlight 程序到浏览器外(卸载只能通过右键菜单的方式卸载)
* Application.CheckAndDownloadUpdateAsync, Application.CheckAndDownloadUpdateCompleted - 一对异步方法/事件,用于更新浏览器外运行的 Silverlight 程序(从服务器上下载新的版本)
*
* 启用 OOB:在 Silverlight 项目上单击右键 -> 属性 -> Silverlight -> 选中 Enable running application out of the browser
* 配置 OOB:在 Silverlight 项目上单击右键 -> 属性 -> Silverlight -> Out-of-browser Settings 可以对“浏览器外运行”的相关参数做设置(也可以手动修改 PropertIEs/OutOfbrowserSettings.xml)
*/
/*
* 调试 OOB 模式下的 Silverlight 程序:启用 OOB 模式,将 Silverlight 项目设置为启动项目,然后运行即可
*/
/*
* 本例用于演示 Closing 事件
* Application.Current.MainWindow - OOB 模式下的应用程序窗口。属性值为 System.windows.Window 类型
* Window.Activate() - 激活窗口,使之具有焦点,并置于最前面
* Window.IsActive - 是否为激活窗口(仅 get)
* Window.Close() - 关闭窗口
* Window.Closing - 窗口关闭前触发的事件。事件参数为 System.ComponentModel.ClosingEventArgs 类型
* ClosingEventArgs.IsCancelable - 是否可以取消窗口的关闭事件
* ClosingEventArgs.Cancel - 是否取消窗口的关闭事件
*/
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 代码 /*
* 本例演示 Silverlight 应用程序在 OOB 模式下的自动更新
* 注1:Silverlight 应用程序根据 Assembly Version 来判断是否需要更新
* 注2:程序下载成功后,需要重启 Silverlight 应用程序,这样运行的才是更新后的程序
*
* NetworkInterface.GetIsNetworkAvailable() - 网络是否有效
* NetworkChange.NetworkAddressChanged - 网络状态发生变化时所触发的事件
* Application.CheckAndDownloadUpdateAsync, Application.CheckAndDownloadUpdateCompleted - 一对异步方法/事件,用于更新浏览器外运行的 Silverlight 程序(从服务器上下载新的版本)
* CheckAndDownloadUpdateCompletedEventArgs.UpdateAvailable - 是否已经成功更新了版本
* CheckAndDownloadUpdateCompletedEventArgs.Error - 下载更新时发生了异常
*/
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 ="d出 Popup" Click ="btnShowPopup_Click" />
<!-- NotificationWindow 的 Demo -->
< button name ="btnShowNotificationWindow" margin ="5" Content ="d出 NotificationWindow" Click ="btnShowNotificationWindow_Click" />
</ StackPanel >
</ GrID >
</ navigation:Page >
NotificationWindowDemo.xaml.cs 代码 /*
* NotificationWindow - 在 OOB 模式下显示通知窗口,也就是 toast
*/
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 - 用于在 Silverlight 程序区域之内,Silverlight 内容之上显示内容,其通吃浏览器内外。来自 Silverlight 2.0
* Popup.Child - Popup 上显示的内容
* Popup.IsOpen - 是否显示 Popup
*/
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)
{
/*
* NotificationWindow.Content - toast 所显示的内容
* NotificationWindow.Show(int durationInMilliseconds) - 显示 toast,并在指定时间后隐藏 toast
*
* 注:每个 Silverlight 应用程序在同一时间只能显示一个 toast 。如果在一个 toast 正在显示的同时调用 NotificationWindow.Show() 则会引发异常
*/
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
generic.xaml
代码 < ResourceDictionary
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys ="clr-namespace:System;assembly=mscorlib"
xmlns:vsm ="clr-namespace:System.windows;assembly=System.windows"
xmlns:local ="clr-namespace:Silverlight40.OutOfbrowser" >
< ResourceDictionary.MergedDictionarIEs >
<!--
自定义控件时,将 Style 放到 generic.xaml 之外的注意事项:generic.xaml 和 Style 的 xaml 文件均要设置为资源类型
-->
< ResourceDictionary Source ="/Silverlight40;component/themes/CustomNotificationWindow.xaml" />
</ ResourceDictionary.MergedDictionarIEs >
</ ResourceDictionary >
CustomNotificationWindow.xaml
代码 < ResourceDictionary
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys ="clr-namespace:System;assembly=mscorlib"
xmlns:vsm ="clr-namespace:System.windows;assembly=System.windows"
xmlns:local ="clr-namespace:Silverlight40.OutOfbrowser" >
< Style targettype ="local:CustomNotificationWindow" >
< Setter Property ="IsTabStop" Value ="False" />
< Setter Property ="Background" Value ="Black" />
< Setter Property ="Template" >
< Setter.Value >
< ControlTemplate targettype ="local:CustomNotificationWindow" >
< StackPanel >
<!-- 关闭按钮 -->
< button name ="btnClose" Content ="关闭" VerticalAlignment ="top" HorizontalAlignment ="Right" margin ="5" />
<!-- toast 的标题 -->
< TextBlock Text =" {TemplateBinding Title} " Foreground ="Red" FontWeight ="Bold" FontSize ="12" />
<!-- toast 的正文 -->
< TextBlock Text =" {TemplateBinding Text} " FontSize ="12" textwrapPing ="Wrap" />
</ StackPanel >
</ ControlTemplate >
</ Setter.Value >
</ Setter >
</ Style >
</ ResourceDictionary >
CustomNotificationWindow.cs 代码 /*
* 本例演示如何实现自定的 NotificationWindow
*/
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 " ,
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 ="d出 CustomNotificationWindow" Click ="btnShowCustomNotificationWindow_Click" />
</ StackPanel >
</ GrID >
</ navigation:Page >
CustomNotificationWindowDemo.xaml.cs 代码 /*
* 本例演示如何使用自定的 NotificationWindow
*/
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
[源码下载] 总结
以上是内存溢出为你收集整理的稳扎稳打Silverlight(44) - 4.0浏览器外运行(Out Of Browser)之OOB的增强及其新增的NotificationWindow全部内容,希望文章能够帮你解决稳扎稳打Silverlight(44) - 4.0浏览器外运行(Out Of Browser)之OOB的增强及其新增的NotificationWindow所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)