简体   繁体   中英

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

I am developing a game using wpf and 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.

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. 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;
   }                
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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