PRISM 用户控制和服务参考

发布于 2024-09-27 17:37:44 字数 802 浏览 0 评论 0原文

我在我的项目中使用 MVVM,这是我的问题。我有一个视图和相应的带有服务引用的视图模型。该视图包含 UserControl,其中还有另一个 UserControl,并且还包含嵌套的 UserControl。最后一个 UserControl 有一个创建弹出窗口的方法。在这个弹出窗口中,我需要来自视图模型的服务引用。每个用户控件都有自己的DataContext。

代码说明。

查看 xaml:

<UserControl DataContext="{Binding ViewModel}">
   <FunctionsList/>
</UserControl>

FunctionsList xaml:

<UserControl>
   <Function1/>
   <Function2/>
   <Function3/>
   <Function4/>
</UserControl>

Function3 xaml:

<UserControl/>

Function3 代码后面包含 CreatePopup 方法,该方法创建以 UserControl Function3Popup 作为内容的对话框。 Function3Popup 应该有 Service 引用。

这里的最佳实践是什么?我有一个糟糕的解决方案来使用绑定传递引用,但这对我来说似乎令人沮丧。

I'm using MVVM in my project and here is my question. I have a View and corresponding view-model with service reference. This view contains UserControl, which have another UserControl and it also contains nested UserControl. Last UserControl have a method which creates a popup. And in this popup i need service reference from view model. Each user control has own DataContext.

Code explanation.

View xaml:

<UserControl DataContext="{Binding ViewModel}">
   <FunctionsList/>
</UserControl>

FunctionsList xaml:

<UserControl>
   <Function1/>
   <Function2/>
   <Function3/>
   <Function4/>
</UserControl>

Function3 xaml:

<UserControl/>

Function3 code behind contains CreatePopup method, which creates dialog with a UserControl Function3Popup as Content. And Function3Popup should have Service reference.

What is the best practice here? I have awful solution to pass reference using binding but it seems discouraging to me.

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

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

发布评论

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

评论(1

你的心境我的脸 2024-10-04 17:37:44

好吧,毕竟我已经使用自己的 ViewModel 实现了弹出窗口,并使用嵌套子控件中的命令绑定从父控件解析了它。我认为这是最好的解决方案。

Well, after all I've implemented popup with own ViewModel and resolved it from parent control using command binding in the nested child control. I think it's the best solution here.

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