PRISM 用户控制和服务参考
我在我的项目中使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,毕竟我已经使用自己的 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.