在上两篇教程中,讲述了Silverlight的Out of Browser理论知识和基础实践。本节将讲述如何创建自定义的Out of Browser应用以及如何调试Silverlight的Out of Browser应用。
Silverlight Out of Browser的自定义化
从Silverlight 4开始,OOB应用支持信任权限设置和窗口自定义,最典型的自定义窗口应用是Silverlight Facebook客户端。从下图可以看出,OOB应用其运行效果已经基本和Windows应用相似,其专业效果不逊于WinForm和WPF应用。
对于创建自定义窗口应用,微软提供了非常简单的方法,
首先选择“Require elevated trust when running outside the browser”, 在下面“Window Style”中可以选择不同的窗口模式,其中分别是:
1. Default模式,默认模式是使用Windows默认窗口样式;
2. No Border,无边框模式;
3. Borderless Round Corners, 圆角无边框模式;
对比以上三种方模式,第一种默认模式最为简单,因为使用的是Windows默认窗口,其中最大化和最小化以及关闭控件都是继承自Windows窗口,其中窗口的拖拉事件默认的也是使用Windows API进行控制; 而第二种和第三种窗口模式,允许设计开发人员创建个性的OOB窗口应用,但是同时也需要创建自定义的最大化,最小化以及关闭控件。下面看一个简单的实例演示,
这里我们使用第三种窗口模式,圆角无边框窗口作为演示,首先打开上一讲中的例程项目SilverlightOOBDemo,为了演示的清晰明了,我们将重新创建一个OutofBrowserMainPage页面,承载新的自定义窗口页面。
该页面,我们模拟上面Facebook的黑色配色方案,简单实现自定义窗口。由于我们使用的是第三种窗口模式,圆角无边框,这里我们需要为OOB应用创建自定义最大化,最小化和关闭控件,以及拖拽响应事件。
首先创建自定义最大化,最小化和关闭控件,
创建一个新的控件WindowControls,将最大化,最小化和关闭控件归类放入该页面,
 1 <UserControl x:Class="SilverlightOOBDemo.WindowControls"
 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"  Width="80" Height="24">
 8     
 9     <StackPanel x:Name="LayoutRoot" Background="DarkGray" Orientation="Horizontal">
10         <Button x:Name="btMinimize" Height="20" Width="20" Margin="3" >
11             <Image Width="14" Height="14" VerticalAlignment="Center" HorizontalAlignment="Center" Source="/SilverlightOOBDemo;component/Images/min.png" Stretch="None"/>
12         </Button>
13         <Button x:Name="btMaximize" Height="20" Width="20" Margin="3" >
14             <Image Width="14" Height="14" VerticalAlignment="Center" HorizontalAlignment="Center" Source="/SilverlightOOBDemo;component/Images/max.png" Stretch="None"/>
15         </Button>
16         <Button x:Name="btClose" Height="20" Width="20" Margin="3" >
17             <Image Width="14" Height="14" VerticalAlignment="Center" HorizontalAlignment="Center" Source="/SilverlightOOBDemo;component/Images/close.png" Stretch="None"/>
18         </Button>
19     </StackPanel>
20 </UserControl>
21 
然后创建简单的哦OutofBrowserMainPage页面样式,调用上面新创建的WindowControls控件。由于这里基本都是基础布局代码,这里不再赘述,如果对Silverlight项目布局不熟悉的,请看这套系列教程“Expression Blend实例中文教程系列文章汇总 ”。
 1 <UserControl x:Class="SilverlightOOBDemo.OutofBrowserMainPage"
 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     xmlns:local ="clr-namespace:SilverlightOOBDemo"
 7     mc:Ignorable="d"
 8     d:DesignHeight="600" d:DesignWidth="900">
 9 
10     <Border CornerRadius="3" BorderThickness="7" BorderBrush="Black"  Background="Gray" x:Name="lytRoot">
11 
12         <Border CornerRadius="4" BorderBrush="Black" BorderThickness="3" Background="DarkGray" Margin="1">
13             <Grid x:Name="LayoutRoot">
14                 <Grid.ColumnDefinitions>
15                     <ColumnDefinition Width="325*" />
16                     <ColumnDefinition Width="57*" />
17                 </Grid.ColumnDefinitions>
18                 <Grid.RowDefinitions>
19                     <RowDefinition Height="24" />
20                     <RowDefinition Height="*" />
21                 </Grid.RowDefinitions>
22 
23                 <local:WindowControls HorizontalAlignment="Right" VerticalAlignment="Top" Grid.Column="1" />
24 
25                 <StackPanel Orientation="Horizontal" IsHitTestVisible="True" Background="DarkGray" >
26                     <TextBlock Text="Silverlight OOB Demo" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,0,0,0" FontSize="15" Height="24" Width="195"/>
27                 </StackPanel>
28 
29                 <Grid Grid.Row="1" Grid.ColumnSpan="2"  >
30 
31                     <Grid.Background>
32                         <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
33                             <GradientStop Color="#FF000000" Offset="0"/>
34                             <GradientStop Color="#FF585858" Offset="1"/>
35                         </LinearGradientBrush>
36                     </Grid.Background>
37                     
38                 </Grid>
39             </Grid>
40         </Border>
41     </Border>
42 </UserControl>
43 
这样一个自定义的OOB应用窗口已经创建完毕了。运行看看,这里会提示Security Warning,这是由于我们选择了权限信任设置,Silverlight客户端会自动提醒用户是否授权安装该应用。这里微软提供了XAP签名验证,可以优化Security Warning窗口,让用户更容易接受和安装,这将在后文介绍,这里我们直接点击安装即可。
这时会弹出以下自定义OOB应用窗口:
这样基本的Layout算是完成,而我们会发现,创建的自定义最大化,最小化和关闭控件都不能使用而且,窗口无法进行拖动。下面我们来添加一些代码来完善该自定义窗口。
最大化,最小化和关闭的功能实现
首先完善自定义最大化和最小化以及关闭控件的功能,进入WindowControl页面,添加简单代码即可实现上述功能,
 1 public partial class WindowControls : UserControl
 2     {
 3         bool maximized = false;
 4 
 5         public WindowControls()
 6         {
 7             InitializeComponent();
 8         }
 9 
10         /// <summary>
11         /// 窗口最大化控制
12         /// 欢迎访问我的博客:
13         /// http://jv9.cnblogs.com
14         /// </summary>
15         /// <param name="sender"></param>
16         /// <param name="e"></param>
17         private void btMaximize_Click(object sender, RoutedEventArgs e)
18         {
19             if (!maximized)
20             {
21                 Application.Current.MainWindow.WindowState = WindowState.Maximized;
22                 maximized = true;
23             }
24             else
25             {
26                 maximized = false;
27                 Application.Current.MainWindow.WindowState = WindowState.Normal;
28             }
29         }
30 
31         /// <summary>
32         /// 窗口关闭控制
33         /// </summary>
34         /// <param name="sender"></param>
35         /// <param name="e"></param>
36         private void btClose_Click(object sender, RoutedEventArgs e)
37         {
38             Application.Current.MainWindow.Close();
39         }
40 
41         /// <summary>
42         /// 窗口最小化控制
43         /// </summary>
44         /// <param name="sender"></param>
45         /// <param name="e"></param>
46         private void btMinimize_Click(object sender, RoutedEventArgs e)
47         {
48             Application.Current.MainWindow.WindowState = WindowState.Minimized;
49         }
50 
51     }
主窗口位置拖拽功能实现
而对于OOB应用主窗口的拖拽,则需要在OutofBrowserMainPage中添加简单代码即可实现,代码如下:
首先声明实例获取当前主窗口
1 Window OOBWindow = Application.Current.MainWindow;
然后在窗口头部灰色区域创建鼠标响应事件,
1 <StackPanel Orientation="Horizontal" MouseLeftButtonDown="StackPanel_MouseLeftButtonDown" IsHitTestVisible="True" Background="DarkGray" >
2                     <TextBlock Text="Silverlight OOB Demo" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,0,0,0" FontSize="15" Height="24" Width="195"/>
3 </StackPanel>
在StackPanel_MouseLeftButtonDown事件中添加简单的控制代码即可实现OOB应用主窗口位置移动拖拽。
1  private void StackPanel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
2         {
3             OOBWindow.DragMove();
4         }
主窗口尺寸修改实现
对于Windows窗口而言,修改窗口尺寸大小属于基本功能,在Silverlight的Out of Browser中,同样有相对应的API可以实现窗口尺寸修改,方法如下:
和上面相同,我们将创建一个独立的控件页面WindowResize,其中使用Path创建一个简单的拖拽图标
 1 <UserControl x:Class="SilverlightOOBDemo.WindowResize"
 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" MouseEnter="UserControl_MouseEnter" MouseLeave="UserControl_MouseLeave">
 8 
 9     <Border x:Name="LayoutRoot" BorderThickness="0" HorizontalAlignment="Right" VerticalAlignment="Bottom" Width="20" Height="20" Margin="0,0,-2,-2">
10         <Path x:Name="ptResize" Margin="6,6,0,0" Cursor="SizeNWSE" MouseLeftButtonDown="ptResize_MouseLeftButtonDown"
11                 Data="M 8,0 L10,0 L10,2 L8,2 Z M 4,4 L6,4 L6,6 L4,6 Z M 8,4 L10,4 L10,6 L8,6 Z M0,8 L2,8 L2,10 L0,10 Z M4,8 L6,8 L6,10 L4,10 Z M8,8 L10,8 L10,10 L8,10 Z"Fill="Gray"/>
12     </Border>
13 </UserControl>
14 
为了响应拖拽修改窗口尺寸,需要创建鼠标响应事件MouseLeftButtonDown,在事件中,调用Silverlight API,
 1 public partial class WindowResize : UserControl
 2     {
 3         public WindowResize()
 4         {
 5             InitializeComponent();
 6         }
 7 
 8         /// <summary>
 9         /// 修改主窗口尺寸
10         /// </summary>
11         /// <param name="sender"></param>
12         /// <param name="e"></param>
13         private void ptResize_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
14         {
15             Application.Current.MainWindow.DragResize(WindowResizeEdge.BottomRight);
16         }
17 
18         private void UserControl_MouseEnter(object sender, MouseEventArgs e)
19         {
20             this.Cursor = Cursors.SizeNWSE;
21         }
22 
23         private void UserControl_MouseLeave(object sender, MouseEventArgs e)
24         {
25             this.Cursor = Cursors.Arrow;
26         }
27     }
这样该控件已经可以实现修改OOB应用主窗口尺寸大小,将该控件添加到OutofBrowserMainPage即可使用。
1 <local:WindowResize Grid.Row="1" Grid.Column="1" Height="20" Width="20" VerticalAlignment="Bottom" HorizontalAlignment="Right"></local:WindowResize>
通过上面步骤的介绍,我们已经创建了一个完整的简单的Silverlight OOB自定义窗口应用。以上所有功能都是Silverlight 4的API提供。如果你在项目中需要更多的自定义功能,你可以通过继承Silverlight基类创建更为灵活和强大的自定义OOB控件。在微软官方提供了一套开源的创建自定义Out of Browser应用项目,如果感兴趣,大家可以下载该项目参考学习。
今天就写到这了,如果您在阅读中发现问题或者有好的建议,请您给我留言,在这里提前感谢您的支持和帮助。
本篇源代码下载

本文转自 冷秋寒 51CTO博客,原文链接:http://blog.51cto.com/kevinfan/348397 ,如需转载请自行联系原作者

Silverlight实例教程 - Out of Browser的自定义应用相关推荐

  1. 一起谈.NET技术,Silverlight实例教程 - Out of Browser的Debug和Notifications窗口

    Silverlight 实例教程索引 Silverlight 实例教程 - Out of Browser开篇 Silverlight 实例教程 - Out of Browser配置,安装和卸载 Sil ...

  2. Silverlight实例教程 - Out of Browser在线更新和Silent安装

    通过前几篇的学习和实践,相信大家可以很轻松的创建个性化Silverlight Out of Browser应用.上周,在银光中国网论坛有人问到对于Silverlight Out of Browser应 ...

  3. Silverlight实例教程 - Out of Browser开篇

    众所周知,学习一门IT技术,最关键的是实践,无论是软件开发还是网络调试,只要在真实环境下勤于动手和思考,很快就能掌握一门技术,Silverlight也不例外.为了帮助更多朋友快速学习掌握Silverl ...

  4. Silverlight实例教程 - Out of Browser的Debug和Notifications窗口

    熟悉Silverlight的朋友应该知道,Silverlight从1.0版本到现在的4.0版本,其功能性越来越强大,从下图我们可以看出,Silverlight的应用模型的一个转变过程,从Javascr ...

  5. Silverlight实例教程 - Validation数据验证开篇

    Silverlight 4 Validation验证实例系列 Silverlight实例教程 - Validation数据验证开篇 Silverlight实例教程 - Validation数据验证基础 ...

  6. Silverlight实例教程 - Validation验证系列汇总

    转自http://www.cnblogs.com/jv9/archive/2010/09/27/1836394.html Silverlight Validation验证系列教程,详细讲解Silver ...

  7. Silverlight实例教程 - 理解Navigation导航框架Frame类

    通过"Navigation导航框架开篇"的介绍,可以了解到Silverlight导航框架可被应用于多种场合,最为常见的是实现Silverlight项目内部页面切换.在Silverl ...

  8. Silverlight实例教程 - Navigation导航框架系列汇总

    Silverlight实例教程导航框架系列,结合实例讲述Silverlight导航开发基础,该教程不仅仅适用于Silverlight,而且可以被应用于Windows Phone开发领域. 文章列表: ...

  9. Silverlight实例教程 – Datagrid,Dataform数据验证和ValidationSummary

    上一篇,我们讨论了,捕获用户提交数据验证.本篇将集中讨论Datagrid和Dataform两个数据控件的数据验证方法,其中,我们会附加介绍一个新的验证控件ValidationSummary. 概述 D ...

最新文章

  1. R语言data.table进行滚动数据连接,滚动连接通常用于分析涉及时间的数据(例如商业销售活动和对应的广告投放的安排之之间的关系)实战:实战和动画说明滚动数据连接的形式及方法
  2. 点击失效,一层透明的view,
  3. AtCoder Regular Contest 061 E - Snuke‘s Subway Trip(建图 + dijkstra最短路 / 0/1bfs / 并查集)
  4. c语言中字母的定义,c语言字符串定义与初始化 - 且听风吟
  5. 终于去掉了location.reload()弹出的问题。
  6. 分数四则运算法则c语言,△【OJ】(二)---C---分数类的四则运算
  7. 闲谈: 测试报告的插入图片
  8. 在Solaris下安装中文语言包
  9. 思科网络工程师面试题
  10. 创业维艰-公司业务解决方案推荐!
  11. python小游戏:添加武器,查看武器,删除武器
  12. 国内PHP开源建站程序
  13. EXCEL表格常用函数功能汇总
  14. 合肥工业大学宣城校区计算机大赛,合工大宣城校区学子!全国冠军!
  15. go语言 使用MTP协议 通过WPD(windows portable device)读取便携式设备信息并进行文件传输
  16. 【数据挖掘】-模型的评估(四)
  17. windows操作系统安装单机版mongodb
  18. endnote怎么保存上次记录_endnote使用经验总结
  19. 华为原厂预装系统出厂系统全系列恢复镜像
  20. Dell Poweredge R720服务器支持的显卡

热门文章

  1. 电子商务领域的三股势力
  2. 使用Javaweb开发大学生综合素质测评系统
  3. VS2010中获取并调用CRM2011中的工作流
  4. border-raduis 在IE8中的兼容性问题
  5. Django进阶Model篇001 - mysql 数据库的配置
  6. 转Python 标准库 urllib2 的使用细节
  7. java多线程机制2(安全问题)
  8. hdu 5591 ZYB's Game 博弈论
  9. 解决启动httpd报: apr_sockaddr_info_get() failed for错误
  10. Android---组件篇---Handler的使用(1)[转]