如果不是页面,如何获取 NavigationService?

发布于 2024-10-20 04:09:14 字数 138 浏览 1 评论 0原文

这似乎是一个非常天真的问题,但是人们到底如何从页面外部(例如视图模型)获取 NavigationService ?每个人都说导航应该发生在视图上,但我一直在想,这不是一个网页,它是一个应用程序。视图模型和业务逻辑应该控制应用程序流程,而不是视图。这其实是天真吗?

This seems to be a really naive question, but how on earth does one get the NavigationService from outside of a page, like say perhaps a view model? Everybody says that navigation should occur at the view, but I keep thinking, this is not a web page, its an application. The view model and business logic should control application flow, not the view. Is this in fact naive?

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

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

发布评论

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

评论(3

戏蝶舞 2024-10-27 04:09:14

据我了解,涉及View的操作,即UI,应该是View独占的。使用 MVVM 时,UI 不应由 ViewModel 或 BusinessLogic 直接控制(因为它们不应该了解有关视图的具体实现的任何信息),而应使用消息。

这意味着,如果我们想从 ViewModel 打开编辑器窗口,我们会从 ViewModel 发送一条消息,告知我们要打开它并在视图中接收它并在那里打开窗口。这同样适用于浏览不同的页面,您将在主页(或任何包含您想要浏览的页面)中收到消息并处理其中的所有内容。

另一种方法是使用 DialogService 或类似的东西,它可以在中心位置处理打开的窗口。然而,由于NavigationService是Page类的一个属性,所以我们需要在Page中处理消息。

示例代码,使用 MVVM Light Toolkit:(未经测试,部分取自 Shawn Wildermuth 的 RiaXBoxGames 示例):

ViewModel(例如,将其放入按钮的命令中):

Messenger.Default.Send<bool>(true, "GoToNextPage");

View(例如将其放入构造函数中):

Messenger.Default.Register<bool>(this, "GoToNextPage", ignore =>
{
  // your code to go to next page
});

As far as I understand it, operations involving the View, i.e. UI, should be done by the View exclusively. When working with MVVM, the UI should not be controlled by the ViewModel or BusinessLogic directly (since they are not supposed to know anything about the concrete implementation of the View) but work with Messages.

That means, if we want to open an Editor window from the ViewModel we send a Message from the ViewModel that we want to open it and receive it in the View and open the window there. The same is valid for Navigating through different pages, where you would receive the Message in the MainPage (or whatever holds you pages that you want to navigate through) and handle everything there.

An alternative to that would be using a DialogService or something like that, which handles opening windows in a central place. However, since the NavigationService is a property of the Page class, we need to handle the message in the Page.

Example code, using the MVVM Light Toolkit: (not tested, partly taken from Shawn Wildermuth's RiaXBoxGames example):

ViewModel (e.g., put that in a Command for a Button):

Messenger.Default.Send<bool>(true, "GoToNextPage");

View (e.g. put that in a Constructor):

Messenger.Default.Register<bool>(this, "GoToNextPage", ignore =>
{
  // your code to go to next page
});
魔法唧唧 2024-10-27 04:09:14

另一种选择是在 ViewModel 上创建一个事件,当 Command 发生时触发此事件并向 View 订阅此事件。在 EventArgs 中,您可以携带要导航到哪个页面等。我认为简单且可测试的解决方案。

罗伯特

another option is to create an event on ViewModel, fire this event when Command occurs and subscribe View to this event. Inside EventArgs you can carry which page to navigate to etc. I think simple and testable solution.

Robert

雨的味道风的声音 2024-10-27 04:09:14

我只是在创建视图模型时传递对框架的引用。

I just pass a reference to the Frame when I create the View-Model.

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