首页 新闻 赞助 找找看

WPF DispatcherTimer无法改变Opacity的值

0
悬赏园豆:30 [已解决问题] 解决于 2013-04-25 14:10

 private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            if (this.Opacity > 0)
            {   
                dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
                dispatcherTimer.Interval =TimeSpan.FromSeconds(1);
                dispatcherTimer.Start();
            }
        }

        void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            this.Opacity -= 0.01;
            if (this.Opacity <= 0)
            {
                ((DispatcherTimer)sender).Stop();
                this.Close();
            }

        }

我用的是VS2010,在调试过程中,我发现红色标记的地方,Opacity的值不变化!请各位高手给予指点!

WPF
问题补充:

难道这一条路走不下去吗?在winform上还是可行的啊!各位给个解释!

jobrk的主页 jobrk | 初学一级 | 园豆:110
提问于:2012-07-27 14:24
< >
分享
最佳答案
0
换一种思路吧:

private
void GreyOverlay() { // make the overlay window visible - the effect is to grey out the display if (_greyOverlay == null) _greyOverlay = LogicalTreeHelper.FindLogicalNode(this, "overlay") as System.Windows.Shapes.Rectangle; if (_greyOverlay != null) { _greyOverlay.Visibility = Visibility.Visible; DoEvents(); } } private void DoEvents() { // Allow UI to Update... DispatcherFrame f = new DispatcherFrame(); Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new Action<object>((arg)=> { DispatcherFrame fr = arg as DispatcherFrame; fr.Continue= false; }), f); Dispatcher.PushFrame(f); }

http://stackoverflow.com/questions/2342468/wpf-how-to-apply-a-change-to-an-opacity-background-immediately-wpf-analog-to

 

http://stackoverflow.com/questions/3689809/wpf-window-changing-the-value-of-another-windows-control

收获园豆:30
悟行 | 专家六级 |园豆:12559 | 2012-07-27 16:19
其他回答(1)
0

不在同一个线程中。用UI线程 

 Dispatcher.CurrentDispatcher.BeginInvoke
Lee's Blog | 园豆:530 (小虾三级) | 2012-09-22 22:09
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册