简体   繁体   中英

How do I pass messages from my main Window to its Pages using WPF?

I have a C# application using WPF consisting of a single Window holding four Pages within Frames within a Grid. The main Window receives messages via a socket. I am attempting to send these messages to the appropriate Page for processing, but am not sure 1) how to correctly reference it and 2) how to either pass the message to the Page or call one of the Page's functions to handle the message. The Window, Grid, Frames and Pages are constructed in xaml, in case that is relevant.

In a similar windows forms application I wrote, I used delegates for callbacks. The main window would receive a message, then Invoke a form's delegate - which looked something like this:

Invoke(myFormInstance.myFormDelegateFunction, myMessage);

Where myFormInstance.MdiParent was set to this window.

I'm basically trying to find the WPF equivalent, or perhaps a better way to do it. I've looked into the WPF dispatcher, which I believe may be a way to access Page components directly - but I want the Page itself to process the message and decide what components update and how.

Any advice? Is the dispatcher the answer to this somehow?

Thanks.

I would try to use MVVM Light Toolkit, specifically the Messenger object. It allows for communication between classes. Essentially the sender would just notify of a message with any data related to the message, then any class registered for that specific message can handle it as it chooses. So the main receiver of your socket message can notify it has received a socket message, then attach the socket message as data.

http://mvvmlight.codeplex.com/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM