1

我有一个“animateMyWindow”类来用定时器改变打开窗口的不透明度。

    namespace POCentury
{
    class animateMyWindow
    {
        Timer _timer1 = new Timer();
        Window _openedWindow = null;
        public void animationTimerStart(object openedWindow)
        {
            if (openedWindow == null)
            {
                throw new Exception("Hata");
            }
            else
            {
                _openedWindow = (Window)openedWindow;
                _timer1.Interval = 1 * 25;
                _timer1.Elapsed +=  new ElapsedEventHandler(animationStart);
                _timer1.AutoReset = true;
                _timer1.Enabled = true;
                _timer1.Start();
            }
        }
        private void animationStart(object sender, ElapsedEventArgs e)
        {
            if (_openedWindow.Opacity == 1)
                animationStop();
            else
                _openedWindow.Opacity += .1;
        }
        private void animationStop()
        {
            _timer1.Stop();
        }
    }
}

animationStart 函数无法访问我的窗口,因为它正在另一个线程上工作。我已经尝试过 Dispatcher.BeginInvoke 并且无法使其工作。你能帮我做这件事吗?

4

1 回答 1

0

基本上,您无法访问事件的openedWindow内部,animationStart因为它发生在不同的线程中。您需要 Dispatcher 来执行此操作。

Dispatcher.BeginInvoke(new Action(() =>
{
        if (_openedWindow.Opacity == 1)
                animationStop();
            else
                _openedWindow.Opacity += .1;
}));
于 2013-09-19T13:55:06.380 回答