我正在使用 .NET 并创建一个桌面应用程序/服务,当触发某些事件时,它将在桌面的一角显示通知。我不想使用常规的消息框 b/c,那样会造成太大的干扰。我希望通知滑入视图,然后在几秒钟后淡出。我正在考虑一种类似于 Outlook 收到新邮件时发出的警报的功能。问题是:我应该为此使用 WPF 吗?我从未使用 WPF 做过任何事情,但如果这是达到目的的最佳方法,我会很乐意尝试。有没有办法使用常规 .NET 库来实现此目的?
WPF 使这变得非常简单:可能需要十分钟或更短的时间。步骤如下:
- 创建一个窗口,设置AllowsTransparency =“true”并向其添加一个网格
- 将 Grid 的 RenderTransform 设置为原点为 0,1 的 ScaleTransform
- 在网格上创建一个动画,将 ScaleX 动画从 0 更改为 1,然后将不透明度动画从 1 更改为 0
- 在构造函数中计算 Window.Top 和 Window.Left 以将窗口放置在屏幕的右下角。
这里的所有都是它的。
使用 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 库之一,所以答案是肯定的,它is可以使用“常规 .NET 库”来完成此任务。
如果您询问是否有一种方法可以在不使用 WPF 的情况下完成此操作,答案仍然是肯定的,但它非常复杂,并且需要大约 5 天而不是 5 分钟。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)