Today we use C # to complete the first Hello World program, but to make the program look less monotonous, the code for automatic discoloration is added.
First, let's put out the program.
When you click Start, Hello World will automatically change color.
It's easier to use here.
The main point of knowledge is the use of delegates.
Throughout the discoloration process, it is not possible to do so in the UI thread, which blocks the UI thread, causing the lag, which is not the effect we want, and we should use the delegate to do
Paste the code below
//Delegate Objectdelegatetest Delegateobject; Delegate voidDelegateTest (inti); /// <summary> ///button click event/// </summary> /// <param name= "Sender" ></param> /// <param name= "E" ></param> Private voidButton_Click (Objectsender, RoutedEventArgs e) {Delegateobject=SetColor; Thread Thread=NewThread (Changecolorthread); Thread. Start (); } /// <summary> ///Modify a color thread/// </summary> Public voidChangecolorthread () { for(inti =0; I < -; i++) { //Invoke Delegate This. Dispatcher.begininvoke (Delegateobject, i); //change once in a secondThread.Sleep ( +); } } Public voidSetColor (inti) {Random R=NewRandom (); //take a random number intCol = R.next (0,5); //call method to update different colors based on different numbersTitle.foreground =NewSolidColorBrush (Getcolorvalue (col)); } Public voidUpdateColor () {//Cycle 100 times for(inti =0; I < -; i++) { This. Dispatcher.begininvoke (Delegateobject, i); Thread.Sleep ( +); //Sleep 1 seconds } } /// <summary> ///This method can be used to get the color/// </summary> /// <param name= "i" ></param> /// <returns></returns> PublicSystem.Windows.Media.Color Getcolorvalue (inti) {Switch(i) { Case 0: returnColors.blue; Case 1: returnColors.brown; Case 2: returnColors.cyan; Case 3: returnColors.green; Case 4: returncolors.red; default: returnColors.orange; } }
Detailed code has been posted two, the content is relatively simple, I personally like to use C # to do development, because C # development speed, VS is known as the universe's first IDE, has a powerful function, can let developers have higher development speed.
Using the first WP program in C #