Xaml.cs file:
1 usingSystem;2 usingsystem.timers;3 usingSystem.Windows;4 usingSystem.Windows.Forms;5 namespacecontrolused6 {7 8 Public Partial classMainWindow9 {Ten One PublicMainWindow () A { -Monitor.partevent + = Onstep;//registers a function in the class with the Partevent event of the Monitor static class. - } the Public voidonstep (Object sender,messageargs message) - { - -Application.Current.Dispatcher.Invoke (NewAction (() = + { -Mytextblock.text = message. Txtmessage;//to set the properties of a control in the corresponding XAML + })); A } at } -}
Messageargs class:
1 Public classMessageargs:eventargs2 {3 PublicMessageargs (stringmessage)4 {5 This. Txtmessage =message;6 }7 8 Public stringTxtmessage {Get;Set; }9 Ten}
Monitor Static class:
1 Public Static classMonitor2 {3 Public Static EventEventhandler<messageargs> partevent;//defines an event in monitor that is the Messageargs object4 5 6 Public Static voidInfunction (stringmessage)7 {8 varMessagearg =NewMessageargs9 {TenTxtmessage=message One }; A if(Partevent! =NULL)//If a function is registered to Partevent in the MainWindow constructor, it is not null - { -Partevent (New Object(), Messagearg);//trigger an event to execute all registered functions the } - } - - Public Static BOOLMonitorcenter () + { -Infunction (" Change");//to modify the text of TextBlock in an external class + } A}
C # uses events to invoke controls in the WPF main window across classes