简体   繁体   English

如何使用WPF将消息从主窗口传递到其页面?

[英]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. 我有一个使用WPF的C#应用​​程序,该应用程序由单个窗口组成,该单个窗口在一个网格的框架内包含四个页面。 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. 我试图将这些消息发送到适当的页面进行处理,但是不确定1)如何正确引用它,以及2)如何将消息传递到页面或调用页面的功能之一来处理消息。 The Window, Grid, Frames and Pages are constructed in xaml, in case that is relevant. 如果相关,则窗口,网格,框架和页面以xaml构造。

In a similar windows forms application I wrote, I used delegates for callbacks. 在我编写的类似Windows窗体应用程序中,我使用委托进行回调。 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. 将myFormInstance.MdiParent设置为此窗口的位置。

I'm basically trying to find the WPF equivalent, or perhaps a better way to do it. 我基本上是想找到与WPF等效的方法,或者也许是一种更好的方法。 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. 我已经研究了WPF调度程序,我认为这可能是直接访问Page组件的一种方法-但我希望Page本身处理消息并确定更新哪些组件以及如何进行更新。

Any advice? 有什么建议吗? Is the dispatcher the answer to this somehow? 调度员是否可以通过某种方式解决这个问题?

Thanks. 谢谢。

I would try to use MVVM Light Toolkit, specifically the Messenger object. 我会尝试使用MVVM Light Toolkit,特别是Messenger对象。 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/ http://mvvmlight.codeplex.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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