我正在使用.NET和我正在创建一个桌面应用程序/服务,当某些事件被触发时,它会在我的桌面角落显示通知.我不想使用一个常规的消息框b/c,这将是太侵扰.我希望通知滑入视图,几秒钟后淡出.我想的是,当一条新消息到达时,Outlook会发出类似的提醒.问题是:我应该为此使用WPF吗?我从来没有用WPF做过任何事情,但如果这是达到目的的最好方法,我会很乐意try .有没有办法做到这一点与定期.网络图书馆?

推荐答案

WPF让这件事变得非常简单:它可能需要10分钟或更少的时间.以下是步骤:

  1. 创建一个窗口,设置AllowTransparency="true"并向其添加网格
  2. 将网格的RenderTransform设置为原点为0,1的ScaleTransform
  3. 在网格上创建一个动画,将ScaleX动画设置为0到1,然后再将不透明度设置为1到0
  4. 在构造函数计算窗口中.顶部和windows .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库,答案是肯定的,使用"常规的.NET库"可以实现这一点.

如果你问是否有不使用WPF的方法,答案仍然是肯定的,但它非常复杂,需要5天而不是5分钟.

.net相关问答推荐

PowerShell 5.1和7在使用证书时的区别

向从 .NET 序列化的对象添加 Xml 属性

.NET 4.5 项目未在 Visual Studio 2022 中编译

代码访问安全是否在任何现实世界中使用?

如何获取 Sql Server 数据库中所有模式的列表

C# 的部分类是糟糕的设计吗?

C# 时间跨度毫秒与 TotalMilliseconds

SubscribeOn 和 ObserveOn 有什么区别

.net 服务总线建议?

如何遍历字典并更改值?

如何结合 ||条件语句中的运算符

在 C# 中使用委托

如何在 C# 中将 null 值设置为 int?

无法使用 Unity 将依赖项注入 ASP.NET Web API 控制器

List 是否保证项目将按照添加的顺序返回?

C# 中的 F# List.map 类似功能?

根据条件从列表中删除项目

如何为 Dapper 查询动态创建参数

如何从 webclient 获取状态码?

Linq Query 不断抛出无法创建 System.Object 类型的常量值......,为什么?