如何在 PRISM 4 中导航到新视图时传递对象

发布于 2024-11-01 02:13:53 字数 153 浏览 0 评论 0原文

我正在开发一个 PRISM 应用程序,我们可以深入研究数据(以获取更多详细信息)。 在我的实现中,我有一个嵌套的 MVVM,当我沿着树导航时,我想将模型传递给我新创建的视图。

据我所知,目前 PRISM 允许传递字符串,但不允许传递对象。我想知道有哪些方法可以解决这个问题。

I am working on a PRISM application where we drill down into the data (to get more details).
In my implementation I have a nested MVVM and when I navigate down the tree I would like to pass a model to a my newly created view.

As far as I know, currently PRISM allows to pass strings, but doesn't allow to pass objects. I would like to know what are the ways of overcoming this issue.

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

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

发布评论

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

评论(3

手心的海 2024-11-08 02:13:53

我通常使用一个服务来注册我想要通过 guid 传递的对象。这些存储在哈希表中,当在 prism 中导航时,我将 guid 作为参数传递,然后可以使用该参数来检索对象。

希望这对你有意义!

i usually use a service where i register the objects i want to be passed with a guid. these get stored in a hashtable and when navigating in prism i pass the guid as a parameter which can then be used to retrieve the object.

hope this makes sense to you!

胡渣熟男 2024-11-08 02:13:53

我将使用 OnNavigedTo 和 OnNavieratedFrom 方法通过 NavigationContext 传递对象。

首先从 INavigationAware 接口派生视图模型 -

 public class MyViewModel : INavigationAware
 { ...

然后您可以实现 OnNavieratedFrom 并将要传递的对象设置为导航上下文,如下所示 -

void INavigationAware.OnNavigatedFrom(NavigationContext navigationContext)
{
     SharedData data = new SharedData();
     ...
     navigationContext.NavigationService.Region.Context = data;
}

当您想要接收数据时,在第二个视图模型中添加以下代码 -

void INavigationAware.OnNavigatedTo(NavigationContext navigationContext)
{
    if (navigationContext.NavigationService.Region.Context != null)
    {
                if (navigationContext.NavigationService.Region.Context is SharedData)
                {
                    SharedData data = (SharedData)navigationContext.NavigationService.Region.Context;
                    ...
                }
    }
}

ps。如果有帮助,请将此标记为答案。

I would use the OnNavigatedTo and OnNavigatedFrom methods to pass on the objects using the NavigationContext.

First derive the viewmodel from INavigationAware interface -

 public class MyViewModel : INavigationAware
 { ...

You can then implement OnNavigatedFrom and set the object you want to pass as navigation context as follows -

void INavigationAware.OnNavigatedFrom(NavigationContext navigationContext)
{
     SharedData data = new SharedData();
     ...
     navigationContext.NavigationService.Region.Context = data;
}

and when you want to receive the data, add the following piece of code in the second view model -

void INavigationAware.OnNavigatedTo(NavigationContext navigationContext)
{
    if (navigationContext.NavigationService.Region.Context != null)
    {
                if (navigationContext.NavigationService.Region.Context is SharedData)
                {
                    SharedData data = (SharedData)navigationContext.NavigationService.Region.Context;
                    ...
                }
    }
}

ps. mark this as answer if this helps.

萤火眠眠 2024-11-08 02:13:53

PRISM 支持提供参数:

var para = new NavigationParameters { { "SearchResult", result } };
_regionManager.RequestNavigate(ShellRegions.DockedRight, typeof(UI.SearchResultView).FullName, OnNavigationCompleted, para);

并在您的 View、ViewModel 或两者上实现 INavigationAware 接口。

您还可以在此处找到详细信息:https:// msdn.microsoft.com/en-us/library/gg430861%28v=pandp.40%29.aspx

PRISM supports supplying parameters:

var para = new NavigationParameters { { "SearchResult", result } };
_regionManager.RequestNavigate(ShellRegions.DockedRight, typeof(UI.SearchResultView).FullName, OnNavigationCompleted, para);

and implement the INavigationAware interface on your View, ViewModel or both.

you can also find details here: https://msdn.microsoft.com/en-us/library/gg430861%28v=pandp.40%29.aspx

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