WPF/Prism:视图返回 System.Object

发布于 2024-11-27 15:51:47 字数 439 浏览 1 评论 0原文

我是 WPF 和 Prism 的新手,但我已经了解到您必须在 Unity 中将 View 注册为对象:

Container.RegisterType<Object,MyView>("My.Assembly.MyView");

不过,当我使用

var RelativeUriToMyView = new Uri("My.Assembly.MyView",UriKind.Relative);    
RegionManager.RequestNavigate(RelativeUriToMyView, RegionName, CallbackResult);

MyView 显示为 System.Object 时,CallbackResult 不包含错误。

我缺少什么?如果需要,我很乐意提供更多信息。

I am new to WPF and Prism, but I already learned that you have to register a View in Unity as an object:

Container.RegisterType<Object,MyView>("My.Assembly.MyView");

Still, when I use

var RelativeUriToMyView = new Uri("My.Assembly.MyView",UriKind.Relative);    
RegionManager.RequestNavigate(RelativeUriToMyView, RegionName, CallbackResult);

the MyView displays as System.Object, and the CallbackResult contains no Error.

What am I missing? I'm happy to provide more information if needed.

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

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

发布评论

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

评论(2

—━☆沉默づ 2024-12-04 15:51:47

您可能需要查看 PRISM 源代码中的 RegionNavigationContentLoader.cs;这是为您加载视图的代码。

    protected virtual object CreateNewRegionItem(string candidateTargetContract)
    {
        object newRegionItem;

        try
        {
            newRegionItem = this.serviceLocator.GetInstance<object>(candidateTargetContract);
        }
        catch (ActivationException e)
        {
            throw new InvalidOperationException(
                string.Format(CultureInfo.CurrentCulture, Resources.CannotCreateNavigationTarget, candidateTargetContract),
                e);
        }
        return newRegionItem;
    }

有几种辅助方法可以获取 URI、提取查询字符串并创建用于查找视图的“名称”并将其转换为对象。

本质上,您用来将具体类作为对象与 Unity 关联起来的名称与您尝试使用 Unity 解析该对象时需要使用的名称相同。这是一些伪代码来解释,

Container.RegisterType<object, ConcreteClass>(typeof(ConcreteClass).FullName);

Locator.GetInstance<object>(UriWithFullName)

如果这些都没有帮助,请发布relativeUriToMyView,以便我可以看到内容。
祝你好运。

You would want to look at the RegionNavigationContentLoader.cs in the PRISM source code; Here is the code that is loading the view for you.

    protected virtual object CreateNewRegionItem(string candidateTargetContract)
    {
        object newRegionItem;

        try
        {
            newRegionItem = this.serviceLocator.GetInstance<object>(candidateTargetContract);
        }
        catch (ActivationException e)
        {
            throw new InvalidOperationException(
                string.Format(CultureInfo.CurrentCulture, Resources.CannotCreateNavigationTarget, candidateTargetContract),
                e);
        }
        return newRegionItem;
    }

There are several helper methods that take the URI, extract the query string, and create the 'name' used to lookup your view and cast it as an object.

Essentially, the name you are using to associate your concrete class as an object with Unity is the same one you'll need to use when you try to resolve the object with Unity. Here is some pesudocode to explain,

Container.RegisterType<object, ConcreteClass>(typeof(ConcreteClass).FullName);

Locator.GetInstance<object>(UriWithFullName)

If none of this helps, post the RelativeUriToMyView so I can see the contents.
Good luck.

一指流沙 2024-12-04 15:51:47

该问题似乎是由于使用全名 (My.Assembly.MyView) 而不是名称 (MyView) 注册视图引起的。

编辑:
更改了问题以更准确地反映问题。

The issue seemed to be caused by registering the view with its FullName (My.Assembly.MyView) instead of its Name (MyView).

Edit:
Changed the question to more accurately reflect the issue.

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