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的值不变化!请各位高手给予指点!
难道这一条路走不下去吗?在winform上还是可行的啊!各位给个解释!
换一种思路吧:
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/3689809/wpf-window-changing-the-value-of-another-windows-control
不在同一个线程中。用UI线程
Dispatcher.CurrentDispatcher.BeginInvoke