silverlight 做一个QQ式的消息提示窗
消息提示窗英文名叫Toastnotifications,Toast是吐司的意思,就是烤面包片
起初看到这个名字挺费解,这跟吐司有什么关系,后来终于明白,老外烤吐司的时候是这么做的,把很多面包片放进吐司机里,就是专门烤吐司的面包机,待烤好以后,吐司机会把面包弹起来,此时就是弹出的提示窗口一样,这种比喻很形像
下面进入正题,直接上代码
MainPage xaml部分
<UserControl x:Class="NotifyApplication.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="300" d:DesignWidth="400"> <Grid x:Name="LayoutRoot" Background="White"> <Button Height="30" Width="200" Content="Display Notify Window" Click="Button_Click"/> </Grid> </UserControl>
code部分主要是一个用来调出提示框的按钮
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 NotifyApplication { public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { if(App.Current.IsRunningOutOfBrowser) { var notify = new NotificationWindow(); var win = new NotifyWindow { Header = {Text = "Custom Message Header"}, Description = {Text = "This is a custom description."} }; notify.Width = win.Width; notify.Height = win.Height; notify.Content = win; notify.Show(5000); } } } }
提示框是一个用户控件
<UserControl x:Class="NotifyApplication.NotifyWindow" 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" Height="75" Width="300"> <Grid x:Name="LayoutRoot" Background="White"> <Border Background="#DDDDDD" BorderBrush="Black" BorderThickness="2"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="0.113*"/> <RowDefinition Height="0.306*"/> <RowDefinition Height="0.582*"/> </Grid.RowDefinitions> <Rectangle Fill="#FF747474" Stroke="White" StrokeThickness="0" Grid.ColumnSpan="2"/> <TextBlock TextWrapping="Wrap" Text="x" HorizontalAlignment="Right" Margin="0,0,5,0" Grid.Row="1" FontFamily="Verdana" FontWeight="Bold" FontSize="13"/> <TextBlock Name="Header" TextWrapping="Wrap" Text="Header Text" Grid.Row="1" FontWeight="Bold" VerticalAlignment="Bottom" FontSize="13" Margin="5,0,5,0" FontFamily="Tahoma"/> <TextBlock Name="Description" TextWrapping="Wrap" Text="Notification Text" Grid.Row="2" FontSize="11" FontFamily="Verdana" Margin="5,0,5,0"/> </Grid> </Border> </Grid> </UserControl>
用这里还没完,要设置成可在浏览器外运行
右键点silverlight 项目-->silverlight 标签页 -->Enable running application out of the browser 打上勾