Activating views in regions in Prism

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 01:08:21

问题


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

回答1:


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);
    }


来源:https://stackoverflow.com/questions/7862824/activating-views-in-regions-in-prism

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!