在Prism地区激活视图 [英] Activating views in regions in Prism

查看:150
本文介绍了在Prism地区激活视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题,我似乎无法解决。我有一个创建了一个测试项目,使用MEF和Prism4。我创建了一个测试项目,其中我有2个视图,每个注册自己在一个区域内,还有一个按钮在另一个地区。当点击按钮时,我想要更改的视图到正确的视图。我认为错误的代码如下,任何人都有任何想法我在这里做错了什么?

I have problem that I don't seem to be able to solve. I have a created a test project, using MEF and Prism4. I've created a test project where I have 2 views and each of them register themselves inside a region, and also a button in another region. When the button is clicked, I want the view of change to the correct view. The code I think is wrong is below, anyone have any ideas what I am doing wrong here ?

    public void Initialize()
    {
        regionManager.RegisterViewWithRegion(RegionNames.MainRegion, typeof(Views.Module1View));

        Button button = new Button() { Content = "Module1" };
        button.Click += (o, i) =>
        {
            var region = this.regionManager.Regions[RegionNames.MainRegion];
            if (region != null)
            {
                region.Activate(typeof(Views.Module1View));
            }
        };

        regionManager.AddToRegion(RegionNames.NavigationRegion, button);
    }

我收到以下错误...

I get the following error ...

The region does not contain the specified view.
Parameter name: view


推荐答案

惊人什么一个良好的夜晚睡眠会做!我不得不从ServiceLocator获取视图。

Solved it - amazing what a good nights sleep will do! I had to get the view from the ServiceLocator.

    public void Initialize()
    {
        regionManager.RegisterViewWithRegion(RegionNames.MainRegion, () => 
            ServiceLocator.Current.GetInstance<Views.Module2View>());

        Button button = new Button() { Content = "Module2" };
        button.Click += (o, i) =>
        {
            var view = ServiceLocator.Current.GetInstance<Views.Module2View>();

            var region = this.regionManager.Regions[RegionNames.MainRegion];
            if (region != null)
            {
                region.Activate(view);
            }             
        };

        regionManager.AddToRegion(RegionNames.NavigationRegion, button);
    }

这篇关于在Prism地区激活视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆