Some time ago, I took the time to use WPF as a mass mailing tool. Next I would like to use two or three blogs to share with you the puzzles and accumulated experiences in the development process, although familiar with winform development, there is a big difference between them after all-from the presentation of the interface to internal control. The title of this blog is clear. What I want to talk about is the implementation of the progress bar (attribute change notification mechanism!
The progress bar is easy to implement in winform,CodeAs follows:
1 Private Void Button#click ( Object Sender, eventargs E) 2 { 3 Int Num = 100 ; 4 This . Progressbar1.maximum = Num; 5 For ( Int I = 0 ; I <= num; I = I + 10 ) 6 { 7 This . Progressbar1.value = I; 8 Thread. Sleep (1000 ); 9 } 10 }
The seemingly normal logic is ineffective in WPF. The reason is tested and analyzed: it should beControls and interfaces in winform are in the same thread, while those in WPF are not in the same thread..
Now let's start with the topic and talk about the implementation of the progress bar (attribute change notification mechanism) in WPF --
Sending result Information Entity class
1 Using System; 2 Using System. Collections. Generic; 3 Using System. LINQ; 4 Using System. text; 5 Using System. componentmodel; 6 7 Namespace Emailbatchsend 8 { 9 /// <Summary> 10 /// Sending result Information Entity 11 /// </Summary> 12 Internal Class Sendresult: inotifypropertychanged 13 { 14 Public Event Propertychangedeventhandler propertychanged; 15 16 Private String _ Progressbarnumshow = String . Empty; 17 /// <Summary> 18 /// Display progress bar numbers 19 /// </Summary> 20 Public String Progressbarnumshow 21 { 22 Get 23 { 24 Return This . _ Progressbarnumshow; 25 } 26 Set 27 { 28 This . _ Progressbarnumshow = Value; 29 Onpropertychanged ( " Progressbarnumshow " ); 30 } 31 } 32 33 Private String _ Sendresultmes = String . Empty; 34 /// <Summary> 35 /// Send result message 36 /// </Summary> 37 Public String Sendresultmes 38 { 39 Get 40 { 41 Return This . _ Sendresultmes; 42 } 43 Set 44 { 45 This . _ Sendresultmes = Value; 46 Onpropertychanged ( " Sendresultmes " ); 47 } 48 } 49 50 Private String _ Sendfailemails = String . Empty; 51 /// <Summary> 52 /// Failed message sending 53 /// </Summary> 54 Public String Sendfailemails 55 { 56 Get 57 { 58 Return This . _ Sendfailemails; 59 } 60 Set 61 { 62 This . _ Sendfailemails = Value; 63 Onpropertychanged ( " Sendfailemails " ); 64 } 65 } 66 67 Private Int _ Currentsendnum; 68 /// <Summary> 69 /// Number of currently sending messages 70 /// </Summary> 71 Public Int Currentsendnum 72 { 73 Get 74 { 75 Return This . _ Currentsendnum; 76 } 77 Set 78 { 79 This . _ Currentsendnum = Value; 80 Onpropertychanged ( " Currentsendnum " ); 81 } 82 } 83 84 Private Bool _ Sendcontrolisenabled = True ; 85 /// <Summary> 86 /// Whether the mail sending control is available 87 /// </Summary> 88 Public Bool Sendcontrolisenabled { 89 Get 90 { 91 Return This . _ Sendcontrolisenabled; 92 } 93 Set 94 { 95 This . _ Sendcontrolisenabled = Value; 96 Onpropertychanged ( " Sendcontrolisenabled " ); 97 } 98 } 99 100 Private Void Onpropertychanged ( String Propertyname) 101 { 102 If ( This . Propertychanged! = Null ) 103 This . Propertychanged ( This , New Propertychangedeventargs (propertyname )); 104 } 105 } 106 }
Use on the Interface
Figure 001
The code above shows that the sendresult class has implementedInotifypropertychanged -- this interface is used to send a notification to the client that a property value has been changed,The interface is defined as follows:
1 Namespace System. componentmodel 2 { 3 // Abstract: 4 // Sends a notification to the client that the property value has been changed. 5 Public Interface Inotifypropertychanged 6 { 7 // Abstract: 8 // Occurs when the attribute value is changed. 9 Event Propertychangedeventhandler propertychanged; 10 } 11 }
This interface has only one propertychanged event definition. As the name suggests, this event is triggered when the property value changes and does not need to be implemented. I'm curious about its internal implementation. Focus on the red and purple areas in Figure 001. The current value of the progress bar and the available status of the send button are set by {binding attribute name, the usage of {binding attribute name} is common in WPF. It is similar to <% # eval ("attribute name")> in the data binding control of webform, similarly, you also need to set a datasource-like dashboard for the control to associate the data binding of the control with a specific data object to implement corresponding functions.
1Sendresult =NewSendresult ();2 This. Progressbar. datacontext = sendresult;
The above code is equivalent to setting the progressbar data source, but in WPF, it has another name: datacontext data context. Almost all WPF controls have datacontext, data Binding is supported.
The progress bar has been implemented as follows:
This is because you are not proficient in WPF. If you have any misunderstandings, I hope you can point out or talk about your own opinions. If you are interested, you can leave a message to exchange ideas and learn from each other!