我正在使用 Microsoft Prism 开发 WPF 应用程序。我的 WPF 窗口有 2 个区域。下面是描述我的 shell 窗口的 XAML 代码:
<Window x:Class="Shell.ShellWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cal="http://www.codeplex.com/prism"
xmlns:commonConstants="clr-namespace:Common.Constants;assembly=Common"
Height="479.851" Width="992.164">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*"/>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid Name ="Grid1" Grid.RowSpan="3" Margin="0,0,364,0">
<ItemsControl cal:RegionManager.RegionName=
"{x:Static commonConstants:RegionNames.Region1}"/>
</Grid>
<Grid Name ="Grid2" Margin="662,0,0,0" Grid.RowSpan="3">
<ItemsControl cal:RegionManager.RegionName=
"{x:Static commonConstants:RegionNames.Region2}"/>
</Grid>
</Grid>
</Window>
如您所见,每个区域都位于单独的网格中。每个区域都有与其关联的 WPF 用户控件。第一个区域有一个按钮,第二个区域有一个文本框。我想这样做,以便当用户在第一个区域中按下按钮时,一些文本(例如“Hello world”)会出现在第二个区域中。我不确定这是否可行,因为我无法直接从 region1 访问 region2 中的文本框。如果我在 region2 的用户控件中有一个函数可以将 textBox 的文本设置为我想要的内容,如果在 region1 中我没有 region2/view2 的实例,我将如何调用该函数?
请您参考如下方法:
您可以使用 PRISM 4.0 中的 EventAggregator 在 View 之间进行通信。如果您使用的是 PRISM5.0,则使用 PubSubEvents http://www.codeproject.com/Articles/355473/Prism-EventAggregator-Sample
