之前做WPF的时候就很想使用silverlight的ChildWindow,因为效果比较好看,而且传值也比较方便。不过开始做silverlight时就发现了ChildWindow的局限性,因为他只能以Modal Window(模式窗口)的形式进行应用,这也就是说同一时间只能有一个子窗体出现在应用程序中;另外,ChildWindow组件不能进行窗体大小的自定义缩放。所以网上看了很多文章,把学习的内容记下来吧。
Tim Heuer提供的Non-Modal Used ChildWindow组件(非模式使用子窗体——Tim Heuer称之为浮动窗体[FloatableWindow]【点击下载源码】【点击下载我的实验项目】
1、把项目FloatableWindow添加到解决方案里,并在主项目里对其引用。
2、新建一个带CS的XAML文件(子窗口、用户控件、页都可以),命名为FloatableWindowDemo.xaml,打开FloatableWindowDemo.xaml,写入下列代码(要和你的项目对应):
<controls:FloatableWindow
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:System.Windows.Controls;assembly=FloatableWindow"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="Silverlight试验.FloatableWindowDemo"
Width="400" Height="300" Title="FloatableWindowDemo">
<Grid x:Name="LayoutRoot" Margin="2">
<Button x:Name="CancelButton" Content="Cancel" Click="CancelButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,230,8,8" d:LayoutOverrides="GridBox" />
<Button x:Name="OKButton" Content="OK" Click="OKButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,230,90,8" d:LayoutOverrides="GridBox" />
</Grid>
</controls:FloatableWindow>
3、打开FloatableWindowDemo.xaml.cs,填入以下代码(要和你的项目对应):
using System;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 Silverlight试验{public partial class FloatableWindowDemo : FloatableWindow { public FloatableWindowDemo() { InitializeComponent(); } private void OKButton_Click(object sender, RoutedEventArgs e) { this.DialogResult = true; } private void CancelButton_Click(object sender, RoutedEventArgs e) { this.DialogResult = false; //this.Close(); }}} 4、然后是主窗口MainPage.xaml:using System; 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 Silverlight试验 { public partial class FloatableWindowDemo : FloatableWindow { public FloatableWindowDemo() { InitializeComponent(); } private void OKButton_Click(object sender, RoutedEventArgs e) { this.DialogResult = true; } private void CancelButton_Click(object sender, RoutedEventArgs e) { this.DialogResult = false; //this.Close(); } } }
<UserControl x:Class="Silverlight试验.MainPage" 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" mc:Ignorable="d" d:DesignHeight="396" d:DesignWidth="492" xmlns:my="clr-namespace:Silverlight试验" xmlns:windows="clr-namespace:System.Windows.Controls;assembly=FloatableWindow"> <Grid x:Name="LayoutRoot" Background="White"> <StackPanel Height="55" Margin="134,0,110,8" Orientation="Horizontal" VerticalAlignment="Bottom" HorizontalAlignment="Center" Width="248"> <Button Content="悬浮窗体" Width="237" Margin="5" Click="Button_Click"/> </StackPanel> </Grid> </UserControl>
5、再接着是后台MainPage.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; namespace Silverlight试验 { public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); } private void Button_Click(object sender, System.Windows.RoutedEventArgs e) { FloatableWindowDemo fw = new FloatableWindowDemo();//创建浮动窗口实例 fw.ParentLayoutRoot = LayoutRoot;//指定承载浮动窗口的父窗口的根布局元素[这里为Grid x:Name="LayoutRoot"] fw.Title = "Test Floatable Window";//浮动窗口标题 //fw.Content = "The time is " + DateTime.Now.ToLongTimeString();//浮动窗体内容 fw.Width = 300;//浮动窗口的宽度 fw.Height = 300;//浮动窗口的高度 fw.ResizeMode = ResizeMode.CanResize;//设置浮动窗口可自定义缩放 fw.Show();//以非模式窗口形式打开窗体 } } }
注意要把fw.Content = "The time is " + DateTime.Now.ToLongTimeString();这一行注释掉,要不就无法显示后面自己加的内容了,我就是一开始没看见,结果弄了好半天....