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

本文关键字:位置 不起作用 变化 窗口 不透明度 气泡 | 更新日期: 2023-09-27 18:29:39

我想制作一个气泡窗口,在显示消息后逐渐出现并消失。我希望它的位置在托盘附近,在我用于此的构造函数中:

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

这在其他时候也很有效,但现在不行了。

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

这是代码:

 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#开始动画

您可以从这里参考

关于位置问题,我在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);
}

这是有效的。话虽如此,我愿意改正。