C # uses events to invoke controls in the WPF main window across classes

Source: Internet
Author: User

Xaml.cs file:

<textarea style="width: 657px; height: 484px; line-height: 1.5; font-family: Courier New; font-size: 12px;">using system;using system.timers;using system.windows;using system.windows.forms;namespace ControlUsed{Public Partial class MainWindow {public MainWindow () {monitor.partevent + = onstep;//Registers a function in this class with the Mon Itor the static class in the Partevent event. } public void Onstep (Object Sender,messageargs message) {Application.Current.Dispatch Er. Invoke (new Action () = {Mytextblock.text = message. txtmessage;//setting the property of the control in XAML)); } }}</textarea>

Messageargs class:

Public <textarea style="width: 657px; height: 224.8px; line-height: 1.5; font-family: Courier New; font-size: 12px;">class Messageargs:eventargs {public Messageargs (String message) {this. txtmessage = message; public string Txtmessage {get; set;} }</textarea>

Monitor Static class:

Public <textarea style="width: 657px; height: 449.6px; line-height: 1.5; font-family: Courier New; font-size: 12px;">Static class monitor {public static event eventhandler<messageargs> partevent;//defined in monitor parameter is Messageargs object public static void Infunction (String message) {var messagearg = new Message Args {txtmessage=message}; if (partevent! = null)//If the function is registered to Partevent in the MainWindow constructor, it is not null {partevent (New object), Messagea RG);//Trigger event, execute all registered functions}} public static bool Monitorcenter () {infunction ("change");//Outside Modify TextBlock's Text}} in class</textarea>

C # uses events to invoke controls in the WPF main window across classes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.