Activation of views in the regions of Prism

I have a problem that I seem to be unable to solve. I created a test project using MEF and Prism4. I created a test project in which I have 2 views, and each of them is registered inside the region, as well as a button in another region. When the button is pressed, I want the view of the change to be correct. The code that I consider to be incorrect is below, who has ideas, what am I 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
+2
source share
1 answer

Decided this - it is amazing what a good night's sleep will do! I needed to get a view from 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);
    }
+1
source

Source: https://habr.com/ru/post/1709727/


All Articles