WPF简单通知窗口
在这里记录一下写通知窗口的过程,自己以后可以有所借鉴,表格布局用的不是很熟,如有问题欢迎留言。
页面布局
<Window x:Class="Apps.NotificationWindow"
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:local="clr-namespace:WpfAIOApplication.Pages.Common"
mc:Ignorable="d"
Title="NorificationWindow" Height="60" Width="260" AllowsTransparency="True"
WindowStyle="None" WindowStartupLocation="Manual" Loaded="Window_Loaded" Background="{x:Null}">
<Grid Opacity="1" Background="Transparent" >
<Border Background="White" CornerRadius="10,10,10,10" >
<Grid x:Name="mGrid">
<Grid.RowDefinitions>
<RowDefinition Height="2*"/>
<RowDefinition Height="3*"/>
<RowDefinition Height="8*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="3*"/>
<ColumnDefinition Width="25*"/>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<Grid Grid.Row="1" Grid.RowSpan="2" Grid.Column="1">
<Image Margin="0, -20, 0, 0" Source="/Images/nf_notify.png" Height="16" x:Name="NotificationTypeImg"></Image>
</Grid>
<Grid Grid.Row="1" Grid.Column="2" Background="Transparent" Grid.ColumnSpan="2">
<TextBlock x:Name="titleLb" Text="提示" FontSize="10" HorizontalAlignment="Left" Foreground="#555" Height="20" Margin="10, 0, 0, 0" FontWeight="Bold">
</TextBlock>
</Grid>
<Image Grid.Column="3" Grid.Row="1" Source="/Images/nf_close.png" HorizontalAlignment="Center" Width="10" Height="10" MouseLeftButtonDown="Image_MouseLeftButtonDown">
</Image>
<TextBlock x:Name="contentTxt" Foreground="#AAA" Background="Transparent" FontSize="9" VerticalAlignment="Top" HorizontalAlignment="Left" Grid.Row="2" Grid.Column="2" Text="测试通知内容" TextWrapping="Wrap" Margin="6, 5, 0, 0" >
</TextBlock>
</Grid>
</Border>
</Grid>
</Window>
后台编码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace Apps
{
/// <summary>
/// NorificationWindow.xaml 的交互逻辑
/// </summary>
public partial class NotificationWindow : Window
{
DispatcherTimer timer;
public double EndTop { get; set; }
public NotificationWindow(string title, string content)
{
InitializeComponent();
titleLb.Text = title;
contentTxt.Text = content;
// 处理提示框显示位置
this.Left = SystemParameters.WorkArea.Width - this.Width;
this.EndTop = SystemParameters.WorkArea.Height - this.Height;
this.Top = SystemParameters.WorkArea.Height - this.Height;
timer = new DispatcherTimer();
// 设置提示框自动消失时间
timer.Interval = TimeSpan.FromSeconds(10);
timer.Tick += timer_Tick;
}
private void timer_Tick(object sender, EventArgs e)
{
Image_MouseLeftButtonDown(null, null);
}
private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
this.IsEnabled = false;
this.Close();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
timer.Start();
}
}
}
这个可以实现自动关闭和手动触发关闭。