我正在使用.NET和我正在创建一个桌面应用程序/服务,当某些事件被触发时,它会在我的桌面角落显示通知.我不想使用一个常规的消息框b/c,这将是太侵扰.我希望通知滑入视图,几秒钟后淡出.我想的是,当一条新消息到达时,Outlook会发出类似的提醒.问题是:我应该为此使用WPF吗?我从来没有用WPF做过任何事情,但如果这是达到目的的最好方法,我会很乐意try .有没有办法做到这一点与定期.网络图书馆?
我正在使用.NET和我正在创建一个桌面应用程序/服务,当某些事件被触发时,它会在我的桌面角落显示通知.我不想使用一个常规的消息框b/c,这将是太侵扰.我希望通知滑入视图,几秒钟后淡出.我想的是,当一条新消息到达时,Outlook会发出类似的提醒.问题是:我应该为此使用WPF吗?我从来没有用WPF做过任何事情,但如果这是达到目的的最好方法,我会很乐意try .有没有办法做到这一点与定期.网络图书馆?
WPF让这件事变得非常简单:它可能需要10分钟或更少的时间.以下是步骤:
这就是全部.
使用Expression Blend,我花了大约8分钟生成以下工作代码:
<Window
x:Class="NotificationWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Notification Popup" Width="300" SizeToContent="Height"
WindowStyle="None" AllowsTransparency="True" Background="Transparent">
<Grid RenderTransformOrigin="0,1" >
<!-- Notification area -->
<Border BorderThickness="1" Background="Beige" BorderBrush="Black" CornerRadius="10">
<StackPanel Margin="20">
<TextBlock TextWrapping="Wrap" Margin="5">
<Bold>Notification data</Bold><LineBreak /><LineBreak />
Something just happened and you are being notified of it.
</TextBlock>
<CheckBox Content="Checkable" Margin="5 5 0 5" />
<Button Content="Clickable" HorizontalAlignment="Center" />
</StackPanel>
</Border>
<!-- Animation -->
<Grid.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(ScaleTransform.ScaleY)">
<SplineDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
<SplineDoubleKeyFrame KeyTime="0:0:0.5" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="0:0:2" Value="1"/>
<SplineDoubleKeyFrame KeyTime="0:0:4" Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Grid.Triggers>
<Grid.RenderTransform>
<ScaleTransform ScaleY="1" />
</Grid.RenderTransform>
</Grid>
</Window>
代码隐藏:
using System;
using System.Windows;
using System.Windows.Threading;
public partial class NotificationWindow
{
public NotificationWindow()
{
InitializeComponent();
Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new Action(() =>
{
var workingArea = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;
var transform = PresentationSource.FromVisual(this).CompositionTarget.TransformFromDevice;
var corner = transform.Transform(new Point(workingArea.Right, workingArea.Bottom));
this.Left = corner.X - this.ActualWidth - 100;
this.Top = corner.Y - this.ActualHeight;
}));
}
}
因为WPF是常规的.NET库,答案是肯定的,使用"常规的.NET库"可以实现这一点.
如果你问是否有不使用WPF的方法,答案仍然是肯定的,但它非常复杂,需要5天而不是5分钟.