与控件的所有子级共享上下文数据

发布于 2024-07-29 13:47:17 字数 1474 浏览 3 评论 0原文

我需要视图的所有子级都知道一条上下文信息(他们需要显示的人)。 我还需要与 MVVM 兼容的方法。 我尝试在 Prism 应用程序中使用 RegionContext 来执行此操作。 这是我的方法和问题:

我有一个 TabControl,我使用视图注入来填充视图,以便我可以填充 RegionContext:

来自 Shell.xaml:

<TabControl DockPanel.Dock="Right" cal:RegionManager.RegionName="TabRegion">

这是我的注入

//Create an instance of ContactView.xaml
contactView = CreateContactView(contact);
_regionManager.Regions["TabRegion"].Add(contactView, contactKey, true);

//"contact" being a simple entity of type Contact
RegionManager.SetRegionContext(contactViewb, contact);

期望能够使用附加属性通过 RegionManager.RegionContext 附加属性从作为该 DependencyObject (ContactView.xaml) 子级的任何控件获取 RegionContext 的值:

来自 ContactView.xaml

<TextBlock Text="{Binding 
     RelativeSource={RelativeSource Self}, 
     Path=(cal:RegionManager.RegionContext)}"/>

但这不起作用...我必须找到 ContactView.xaml 使其工作:

<TextBlock Text="{Binding 
     RelativeSource={RelativeSource 
          Mode=FindAncestor, 
          AncestorType={x:Type views:ContactView}}, 
     Path=(cal:RegionManager.RegionContext)}"/>

这很好,但是我将允许模块将视图加载到 ContactView 中,并且我希望他们不必了解有关 ContactView 的任何信息。

我想我的问题实际上是,您希望如何加载包含大量控件和区域的复杂视图,并让它们共享一份上下文数据?

执行此操作的正确方法是什么? 作用域容器? 编写一个新的附加属性,该属性沿着控件层次结构向上运行以查找 RegionContext(呃)? 我没有正确使用 RegionContext 吗? 我对任何事情都持开放态度。 建议走开。

I need all children of a view to know a piece of contextual information (the person they need to show). I also need the approach to be compatible with MVVM. I tried to do this with RegionContext in my Prism application. Here's my approach and problems:

I have a TabControl that I use View Injection to populate with views so that I can populate RegionContext:

From Shell.xaml:

<TabControl DockPanel.Dock="Right" cal:RegionManager.RegionName="TabRegion">

And here's my injection

//Create an instance of ContactView.xaml
contactView = CreateContactView(contact);
_regionManager.Regions["TabRegion"].Add(contactView, contactKey, true);

//"contact" being a simple entity of type Contact
RegionManager.SetRegionContext(contactViewb, contact);

What I expected was to be able to use an attached property to get the value of the RegionContext from any control that is a child of that DependencyObject (ContactView.xaml) via the RegionManager.RegionContext attached property:

From ContactView.xaml

<TextBlock Text="{Binding 
     RelativeSource={RelativeSource Self}, 
     Path=(cal:RegionManager.RegionContext)}"/>

But this doesn't work... I have to find ContactView.xaml to get it to work:

<TextBlock Text="{Binding 
     RelativeSource={RelativeSource 
          Mode=FindAncestor, 
          AncestorType={x:Type views:ContactView}}, 
     Path=(cal:RegionManager.RegionContext)}"/>

This is fine, however I'm going to allow modules to load views into ContactView and I would prefer they didn't have to know anything about ContactView.

I guess my question really is, how are you expected to load a complicated view with lots of controls and regions and have them all share one piece of context data?

What is the right way to do this? Scoped container? Write a new attached property that runs up the control hierarchy to find the RegionContext (ugh)? I'm not using RegionContext correctly? I'm open to anything. Suggest away.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

权谋诡计 2024-08-05 13:47:17

更好的设计完全避免使用 RegionContext。 这是一种穷人的依赖注入,因为它只能托管一个对象。 使用向其中注入联系人的范围化 Unity 容器将提高 ViewModel 的可测试性,并避免在 RegionContext 中需要两个项目时不可避免的冲突。

A better design avoids using RegionContext altogether. That is a sort of poor man's dependency injection in that it can host only one object. Using a scoped Unity container into which you inject the contact would lead to better testability of the ViewModel and will avoid the inevitable conflict when needing two items in RegionContext.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文