创建弹出"烤面包机"使用.NET在Windows的通知 [英] Create popup "toaster" notifications in Windows with .NET

查看:181
本文介绍了创建弹出"烤面包机"使用.NET在Windows的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用.NET和正在创建一个桌面应用程序/服务,将在我桌面的一角显示通知,当某些事件被触发。我不想使用常规的消息框B / C,这将是太冒昧。我想通知滑入视图,然后在几秒钟后淡出。我想到的东西,这将起到非常像在Outlook提醒,当有新邮件到达,一个得到。现在的问题是:我应该使用WPF的呢?我从来没有做过与WPF什么,但会很乐意尝试,如果这是最好的手段来结束。有没有办法做到这一点与普通的.NET库?

I am using .NET and am creating a desktop app/service that will display notifications in the corner of my Desktop when certain events are triggered. I don't want to use a regular Message Box b/c that would be too intrusive. I want notifications to slide into view and then fade out after a few seconds. I am thinking of something that will act very much like the Outlook alerts that one gets when a new message arrives. The question is: Should I use WPF for this? I've never done anything with WPF but will happily try it if that's best means to the end. Is there a way to accomplish this with regular .NET libraries?

推荐答案

WPF使这绝对是微不足道的:这将proably休息十分钟以内。具体操作步骤如下:

WPF makes this absolutely trivial: It would proably take ten minutes or less. Here are the steps:

  1. 创建一个窗口,设置AllowsTransparency =true和网格添加到它
  2. 设置网格的的RenderTransform与0.1起源ScaleTransform
  3. 创建上动画的scaleX格动画0比1再后来动画的不透明度从1到0
  4. 在构造函数计算Window.Top和Window.Left放置窗口在屏幕的右下角的角落。

这是所有有给它。

使用防爆pression混合花了约8分钟我产生了以下工作code:

Using Expression Blend it took about 8 minutes me to generate the following working code:

<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>

通过后面code:

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库。

Since WPF is one of the regular .NET libraries, the answer is yes, it is possible to accomplish this with the "regular .NET libraries".

如果你问,如果有一种方法可以做到这一点,而无需使用WPF的答案仍然是肯定的,但它是非常复杂的,需要更多的像5天,比5分钟。

If you're asking if there is a way to do this without using WPF the answer is still yes, but it is extremely complex and will take more like 5 days than 5 minutes.

这篇关于创建弹出&QUOT;烤面包机&QUOT;使用.NET在Windows的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆