English 中文(简体)
更改不透明度时窗口没有重新绘制
原标题:Window not being redrawn when changing Opacity
  • 时间:2012-05-25 09:25:44
  •  标签:
  • c#
  • wpf
  • xaml

我一直在尝试创建一个通知窗口, 但我正在努力弄清楚为什么在我运行时不发生这种不透明的变化。 相反, 窗口会停留一秒钟, 然后关闭而没有任何明显的变化。 我通过其他方法所做的其他尝试都失败了, 所以这一定是我缺少的一些属性。 感谢您的帮助!

    public void RunForm(string error, MessageBoxIcon icon, int duration)
    {
        lblMessage.Text = error;
        Icon i = ToSystemIcon(icon);
        if (i != null)
        {
            BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(i.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
            imgIcon.Source = bs;
        }
        this.WindowStartupLocation = WindowStartupLocation.Manual;
        this.Show();
        this.Left = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Right - this.RestoreBounds.Width - 20;
        this.Top = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Bottom - this.RestoreBounds.Height - 20;
        while (this.Opacity > 0)
        {
            this.Opacity -= 0.05;
            Thread.Sleep(50);
        }
        this.Close();
    }


<Window Width="225" Height="140" VerticalContentAlignment="Center" HorizontalAlignment="Center" ShowActivated="True" ShowInTaskbar="False"
ResizeMode="NoResize" Grid.IsSharedSizeScope="False" SizeToContent="Height" WindowStyle="None" BorderBrush="Gray" 
BorderThickness="1.5" Background="White" Topmost="True" AllowsTransparency="True" Opacity="1">
<Grid Height="Auto" Name="grdNotificationBox" >
    <Image Margin="12,12,0,0" Name="imgIcon" Stretch="Fill" HorizontalAlignment="Left" Width="32" Height="29" VerticalAlignment="Top" />
    <TextBlock Name="lblMessage" TextWrapping="Wrap" Margin="57,11,17,11"></TextBlock>
</Grid>

最佳回答

This can t work: The complete WPF handling is done in one single thread (technically two but not important). you change the opacity and directly let the ui thread sleep, change it again and send it back to sleep. The ui thread never got any time to process what you did. Even removing the sleep would not help, because than it would be much to fast, and the ui thread could not handle any requests aswell. Its important to understand that your code and WPFs handling is done in the same thread, the longer you need, the less time WPF got and vice versa.

要解决这个问题, 您需要使用 < a href=" "http:// msdn. microsoft. com/ en- us/library/ ms752312.aspx" rel = "nofollow noreferr" > animerations 。 它们正是用于这类事情的。 查看此 < a href= > https:// stackoverflow.com/ questionss/59558508/fading-out- a- window" >thread 。

问题回答

您基本上屏蔽了在您的 < code> 循环 < / code> 中更新的 UI 线索。 使用计时器或使用 < a href=" http:// msdn. microsoft. com/ en- us/library/ cc221403% 28v=vs. 95%29. aspx" rel = "nofollow" > Backroundworker Thread 更合适。

编辑 : 以 < strong > do while for 表示, 您可以为此使用 WPF 动画 。 < a href="http:// tarundotnet.wordpress.com/2011/03/18/wpf-tuative- change- oopacity- useing- animation/" rel= "nofollow" > 此文章 详细讨论此内容 。

DoubleAnimation da = new DoubleAnimation();
da.From = 1;
da.To = 0;
da.Duration = new Duration(TimeSpan.FromSeconds(2));
da.AutoReverse = true;
da.RepeatBehavior = RepeatBehavior.Forever;
rectangle1.BeginAnimation(OpacityProperty, da);




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签