应用程序设计 - 使用 MVVM 弹出身份验证

发布于 2025-01-08 19:25:33 字数 290 浏览 3 评论 0原文

这个问题实际上并不是关于代码,而是关于应用程序设计。 我有一个需要身份验证的应用程序,为此,当用户未登录时,我会在主页上显示一个弹出窗口。

我的问题是如何按照 mvvm 模式做到这一点。我认为弹出控件应该位于视图后面的代码中,但是谁发送命令来显示它?视图模型还是模型?如果用户经过身份验证,后面的代码是否应该检查视图模型(我不认为这是正确的方法)?

我一直在研究 mvvm 模式,并开始使用 mvvm light 作为一种简单干净的方式将业务与表示层分离。

这个问题是第一个我自己无法解决的问题。

谢谢

This questions is not really about code but application design.
I have an app that needs authentication, to do that I have a pop up that appears on the main page when the user is not logged in.

My question is how to make this right, following the mvvm pattern. I think that the popup control should be in the code behind of the view, but who sends the order to show it? The view model or the model? Should the code behind check with the viewmodel if the user is authenticated (I don't think that this is the correct aproach) ?

I've been studying the mvvm pattern and started to use mvvm light as a simple and clean way to separate the business from the presentation layer.

This problem was the first that I couldn't figure it out by myself.

Thanks

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

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

发布评论

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

评论(1

屋顶上的小猫咪 2025-01-15 19:25:33

你可以这样做。将 UserControl 的 DataContext 设置为当前用户:

<UserControl DataContext="{Binding Path=User}">
<Popup IsOpen="{Binding Path=IsAuthenticated, Converter={StaticResource BoolToOpposite}">
  ...
</Popup>
</Control>

您可能需要阅读转换器或调用属性 IsNotAuthenticated 才能更快地前进。

you can do it like this. With the DataContext of the UserControl set to the current user:

<UserControl DataContext="{Binding Path=User}">
<Popup IsOpen="{Binding Path=IsAuthenticated, Converter={StaticResource BoolToOpposite}">
  ...
</Popup>
</Control>

You might have to read up on Converters or call the Property IsNotAuthenticated to move forward quicker.

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