从PRISM开始学WPF(番外)共享上下文 RegionContext-更新至Prism7.1
RegionContext共享上下文#
There are a lot of scenarios where you might want to share contextual information between the view that is hosting a region and a view that is inside a region. For example, a master detail–like view shows a business entity and exposes a region to show additional detail information for that business entity. The Prism Library uses a concept named RegionContext to share an object between the host of the region and any views that are loaded inside the region, as shown in the following illustration.
(⊙﹏⊙)Google一下!
有很多场景可能需要在托管区域的视图和区域内的视图之间共享上下文信息。例如,类似主细节的视图显示一个业务实体并公开一个区域以显示该业务实体的附加详细信息。Prism使用一个名为RegionContext的概念在该区域的主机和该区域内加载的任何视图之间共享一个对象,如下图所示。
大意就是,在父视图中,添加一个Region,用来显示扩展信息,并且指定这个Region的DataContext(但是官方说This approach is somewhat similar to the DataContext, but it does not rely on it.),也就是说,仅仅是像而已!也就是说,不不用再为这个即将加载进来的视图,单独设置DataContext,任何一个加载进来的视图都共享这个RegionContext。
<Grid x:Name="LayoutRoot" Background="White" Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="100"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ListBox x:Name="_listOfPeople" ItemsSource="{Binding People}"/>
<ContentControl Grid.Row="1" Margin="10"
prism:RegionManager.RegionName="PersonDetailsRegion"
prism:RegionManager.RegionContext="{Binding SelectedItem, ElementName=_listOfPeople}"/>
</Grid>
上面代码中即是,将_listOfPeople的SelectedItem,作为即将加载到PersonDetailsRegion中的视图的RegionContext
PersonListViewModel的People的元素类型是Person
PersonDetailViewModel的公开属性是SelectedPerson,类型也是Person
这在一些传统应用,含有列表与详细的页面中非常有用。(这个很像winform里的databindings)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!