Home > Net >  Visibility in the WPF set after the Visible, unable to set the IsEnabled to true
Visibility in the WPF set after the Visible, unable to set the IsEnabled to true

Time:02-28

Imitate bosses @ do whack-a-mole game fish envy in the heaven, with six image image as a mouse, click the start first set up after all does not display the image, then the random set an image to display, at the same time isenabled set to true, click on the image count hit the ground squirrels, after a point, but the real trigger the click event, breakpoint debugging found that isenabled is always false, why?
 
The namespace WpfApplication1
{
Public partial class MainWindow: Window
{

The Visibility Visopen=Visibility. Visible;
The Visibility Visclose=Visibility. Collapsed;
DispatcherTimer dispatchertimer2=new DispatcherTimer ();
//int lv=1000;//difficulty, ground squirrels display disappears after 1000 milliseconds
Int ranknum;//score

Public MainWindow ()
{
InitializeComponent ();


Mouse1. Visibility=Visclose;
Mouse2. Visibility=Visclose;
Mouse3. Visibility=Visclose;
Mouse4. Visibility=Visclose;
Mouse5. Visibility=Visclose;
Mouse6. Visibility=Visclose;
Ranknum=0;
LabRank. The Content="score:" + ranknum;
Mousetest. Visibility=Visclose;
Mousetest. IsEnabled=true;


}

Private void btnStart_Click (object sender, RoutedEventArgs e)
{//mouse click start closed after all, open the timer interval for 2 seconds, random display a ground squirrels
Enclosing IsEnabled=false;
Mouse1. IsEnabled=true;
Mouse2. IsEnabled=true;
Mouse3. IsEnabled=true;
Mouse4. IsEnabled=true;
Mouse5. IsEnabled=true;
Mouse6. IsEnabled=true;

DispatcherTimer DispatcherTimer=new DispatcherTimer ();
Dispatchertimer. Tick +=Dispatchertimer_Tick;
Dispatchertimer. Interval=new TimeSpan (0, 0, 2);
Dispatchertimer. Start ();
}

Private void Dispatchertimer_Tick (object sender, EventArgs e)
{
//6 rats randomly choose one, into the next method
The Random Random=new Random ();
Int num=random. Next (6);
The switch (num)
{
Case: 0 method (mouse1); return;
Case 1: method (mouse2); return;
Case 2: method (mouse3); return;
Case 3: method (mouse4); return;
Case 4: method (mouse5); return;
Case 5: method (mouse6); return;
}
}

Private void method (Image Image)
{//close all ground squirrels, about choice of shrews, open (set the Visibility to the Visible, theoretically this time click the mouse should trigger a click event) at the same time open a second timer, ready to shut down
List Listimage=new List {mouse1, mouse2, mouse3 mouse4, mouse5, mouse6};

The foreach (Image k in listimage)
{
K.V isibility=Visclose;
}
Image. The Visibility=Visopen;
Image. The IsEnabled=true;//the problem here, the breakpoint debugging found is always false, triggers the click event
Dispatchertimer2. Tick +=Dispatchertimer2_Tick;
Dispatchertimer2. Interval=new TimeSpan (0, 1);
Dispatchertimer2. Start ();
}

Private void Dispatchertimer2_Tick (object sender, EventArgs e)
{//the second timer, ground squirrels display disappears after a second
Mouse1. Visibility=Visclose;
Mouse2. Visibility=Visclose;
Mouse3. Visibility=Visclose;
Mouse4. Visibility=Visclose;
Mouse5. Visibility=Visclose;
Mouse6. Visibility=Visclose;
Dispatchertimer2. Stop ();
}
//is followed by 6 to mouse click event

CodePudding user response:

Tried it once, the 33 this change to btnStart and why?
  •  Tags:  
  • C#
  • Related