简体   繁体   English

如何通过if-else条件更改WPF窗口的不透明度?

[英]How to change Opacity of WPF window via if-else condition?

I am developing a game using wpf and c#. 我正在使用wpf和c#开发游戏。 I have timer like this: 我有这样的计时器:

    public void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    DispatcherTimer rt = new DispatcherTimer();
    rt.Tick += new EventHandler(rt_tick);
    rt.Interval = new TimeSpan(0, 0, 1);
    rt.Start();
}

Now I am setting the time to 20 seconds, I want that after 20 seconds have passed, the layoutroot should fade(opacity 70%) I tried this but gives error, seems like "public double Opacity{get; set;}" can't be called inside. 现在,我将时间设置为20秒,我希望经过20秒后,layoutroot应该淡入(不透明度70%),我尝试了这个但给出了错误,好像是“ public double Opacity {get; set;}”可以被称为内部。

int i = 120;

        private void rt_tick(object sender, EventArgs e) //round timer
        {
           if(i!=0)
           {
               i--;
               txbTime.Text = "";
               txbTime.Text = Convert.ToString(i) + "s";
           }
           else 
           {  //note*

              public double Opacity
              {
                 get
                    { 
                      return this.Opacity;
                    }
                 set
                    {
                      this.Opacity = 0.7;
                    }
              }
           }                
        }

Note* - it gives me the error " } expected " here. 注意*-在此给我错误“”预期”。

You are declaring a property inside a method, which is invalid C# syntax. 您在方法内部声明了一个属性,该属性是无效的C#语法。 You can simply set the value inside the method: 您可以简单地在方法内部设置值:

private void rt_tick(object sender, EventArgs e) //round timer
{
   if(i!=0)
   {
       i--;
       txbTime.Text = "";
       txbTime.Text = Convert.ToString(i) + "s";
   }
   else 
   { 
        this.Opacity = 0.7;
   }                
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM