气泡 window 不透明度变化和位置不起作用

Bubble window opacity variation and position not working

我想制作一个气泡 window,它会在显示消息后逐渐出现并消失。我希望它的位置靠近托盘,并且在我为此使用的构造函数中:

 var screenSize = System.Windows.SystemParameters.WorkArea;
 this.Left = screenSize.Right - this.Width;
 this.Top = screenSize.Bottom - this.Height;

这在其他时候可以正常工作,但现在不行了。

奇怪的是不透明度也没有改变。因此,简而言之,window 突然出现在屏幕中央而不是托盘附近,没有逐渐过渡。

这是代码:

 public BubbleWindow(string strMessage, double milliseconds)
{
  InitializeComponent();

  btYes.Content = strMessage;

  var screenSize = System.Windows.SystemParameters.WorkArea;
  this.Left = screenSize.Right - this.Width;
  this.Top = screenSize.Bottom - this.Height;

  int interval = (int)(milliseconds / 25);
  for (double iii = 0; iii <= 1; iii += .1)
  {
    Thread.Sleep(interval);
    this.Opacity = iii;
    Dispatcher.Invoke((Action)(() => { }), DispatcherPriority.Render);
    this.UpdateLayout();
  }
  this.Opacity = 1;
}

感谢您的帮助

您可以在 XAML

中使用此代码
<EventTrigger RoutedEvent="Rectangle.Loaded">
  <BeginStoryboard>
    <Storyboard>
      <DoubleAnimation
        Storyboard.TargetName="MyRectangle" 
        Storyboard.TargetProperty="Opacity"
        From="1.0" To="0.0" Duration="0:0:5" />
    </Storyboard>
  </BeginStoryboard>
</EventTrigger>

这是纯粹的XAML方式

或者您可以使用

<Storyboard x:Name="anim">
    <DoubleAnimation
        Storyboard.TargetName="MyRectangle" 
        Storyboard.TargetProperty="Opacity"
        From="1.0" To="0.0" Duration="0:0:5" />
</Storyboard>

然后从c#开始动画

可以参考here

至于位置问题,我在 xaml 代码中设置了 WindowPosition=CenterScreen。这覆盖了 .left 和 .top 命令。

为了完整起见,这是完整的淡入 - 等待 - 淡出循环:

private Storyboard myStoryboard;

public MainWindow()
{
  InitializeComponent();

  DoubleAnimation fadeInAnimation = new DoubleAnimation() { From = 0.0, To = 1.0, Duration = new Duration(TimeSpan.FromSeconds(1)) };
  fadeInAnimation.Completed += FadeInAnimation_Completed;
  myStoryboard = new Storyboard();
  myStoryboard.Children.Add(fadeInAnimation);
  Storyboard.SetTargetName(fadeInAnimation, MyRectangle.Name);
  Storyboard.SetTargetProperty(fadeInAnimation, new PropertyPath(Rectangle.OpacityProperty));
  // Use the Loaded event to start the Storyboard.
  MyRectangle.Loaded += new RoutedEventHandler(myRectangleLoaded);
}
private void myRectangleLoaded(object sender, RoutedEventArgs e)
{
  myStoryboard.Begin(this);
}

private void FadeInAnimation_Completed(object sender, EventArgs e)
{
  DispatcherTimer timerFadeOut = new DispatcherTimer();
  timerFadeOut.Interval = TimeSpan.FromSeconds(2);
  timerFadeOut.Tick += TimerFadeOut_Tick;
  timerFadeOut.Start();
}

private void TimerFadeOut_Tick(object sender, EventArgs e)
{
  DoubleAnimation fadeOutAnimation = new DoubleAnimation() { From = 1.0, To = 0.0, Duration = new Duration(TimeSpan.FromSeconds(1)) };
  myStoryboard = new Storyboard();
  myStoryboard.Children.Add(fadeOutAnimation);
  Storyboard.SetTargetName(fadeOutAnimation, MyRectangle.Name);
  Storyboard.SetTargetProperty(fadeOutAnimation, new PropertyPath(Rectangle.OpacityProperty));
  myStoryboard.Begin(this);
}

这行得通。话虽如此,我愿意接受更正。