Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#wpf窗口不透明度更改错误_C#_Wpf_Window_Opacity - Fatal编程技术网

c#wpf窗口不透明度更改错误

c#wpf窗口不透明度更改错误,c#,wpf,window,opacity,C#,Wpf,Window,Opacity,我正在与WPF合作一个c#项目,但我遇到了一个问题,这让我抓狂:) 问题就在这里。我正试图用定时器改变新窗口的不透明度。但是当我运行这个项目时,“this.Opacity+=.1;”代码抛出一个异常,比如“Invalid operation etc…” 我正在使用以下代码从MainWindow.cs文件打开一个窗口: private void MenuItemArchiveClick(object sender, RoutedEventArgs e) { var arch

我正在与WPF合作一个c#项目,但我遇到了一个问题,这让我抓狂:) 问题就在这里。我正试图用定时器改变新窗口的不透明度。但是当我运行这个项目时,“this.Opacity+=.1;”代码抛出一个异常,比如“Invalid operation etc…” 我正在使用以下代码从MainWindow.cs文件打开一个窗口:

private void MenuItemArchiveClick(object sender, RoutedEventArgs e)
    {
        var archiveWindow = new ArchiveWindow();
        var screenSize = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
        archiveWindow.Width = (screenSize.Width * 95) / 100;
        archiveWindow.Height = (screenSize.Height * 90) / 100;
        archiveWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen;
        archiveWindow.Margin = new Thickness(0, 10, 0, 0);
        archiveWindow.AllowsTransparency = true;
        archiveWindow.Opacity = 0.1;
        archiveWindow.Topmost = true;
        archiveWindow.Show();
    }
我的ArchiveWindow代码是

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace POCentury
{
    /// <summary>
    /// Interaction logic for ArchiveWindow.xaml
    /// </summary>
    public partial class ArchiveWindow : Window
    {
        Timer timer1 = new Timer();

        public ArchiveWindow()
        {
            InitializeComponent();
            timer1.Interval = 1 * 1000;
            timer1.Elapsed += new ElapsedEventHandler(opacityChange);
            timer1.Enabled = true;
            timer1.AutoReset = false;
            timer1.Start();

        }
        private void opacityChange(object sender, EventArgs a)
        {
            if (this.Opacity == 1)
            {
                timer1.Stop();
            }
            else
            {
                this.Opacity += .1;
            }

        }
        private void ArchiveWindowClose()
        {
            timer1.Stop();
            this.Close();
        }
        private void btnArchiveWindowClose(object sender, RoutedEventArgs e)
        {
            ArchiveWindowClose();
        }

        private void imgPatternClick(object sender, MouseButtonEventArgs e)
        {
            MessageBox.Show("sd");  
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统计时器;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Shapes;
名称空间POCentury
{
/// 
///ArchiveWindow.xaml的交互逻辑
/// 
公共部分类ArchiveWindow:窗口
{
计时器计时器1=新计时器();
公共归档窗口()
{
初始化组件();
定时器1.间隔=1*1000;
timer1.appead+=新的ElapsedEventHandler(opacityChange);
timer1.Enabled=true;
timer1.AutoReset=false;
timer1.Start();
}
私有void opacityChange(对象发送方,事件参数a)
{
如果(this.Opacity==1)
{
timer1.Stop();
}
其他的
{
这个.不透明度+=.1;
}
}
私有void ArchiveWindowClose()
{
timer1.Stop();
这个。关闭();
}
私有void btnArchiveWindowClose(对象发送方,路由目标)
{
ArchiveWindowClose();
}
私有void imgPatternClick(对象发送器,鼠标按钮ventargs e)
{
MessageBox.Show(“sd”);
}
}
}
你能帮我做那件事吗?
非常感谢你

基本上,您无法访问opacityChange事件中的计时器,因为它发生在不同的线程中。你需要调度员来做这件事

Dispatcher.BeginInvoke(new Action(() =>
{
        if (this.Opacity == 1)
        {
            timer1.Stop();
        }
        else
        {
            this.Opacity += .1;
        }
}));

你能解释一下你为什么这么做吗?我已经解释过了。如果你想知道更多,请首先阅读我想感谢你的快速回答。这对于初学者来说非常好,但是,我已经将所有动画代码移到了新的Calss文件中。现在程序给出了错误1,非静态字段、方法或属性“System.Windows.Threading.Dispatcher.BeginInvoke(System.Delegate,params object[])”需要对象引用。您能帮我吗?您可能需要将方法设置为静态。如果不是这样,那就提出一个新问题,因为这是一个不同的问题。