Question

I have trouble getting navigation working using Prism 4.

I've set up a test solution in which I have a shell with a region called MainRegion:

<Window x:Class="PrismNavigationTest.ShellView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:prism="http://www.codeplex.com/prism"
        Title="ShellView" Height="350" Width="525">
    <Grid>
        <ContentControl prism:RegionManager.RegionName="MainRegion"/>
    </Grid>
</Window>

Next I have a bootstrapper that adds a Module that is located in a different project to my modulecatalog:

protected override void ConfigureModuleCatalog()
{
    Type module = typeof(Module.ModuleInit);
    ModuleCatalog.AddModule(new ModuleInfo("Module", module.AssemblyQualifiedName));

    base.ConfigureModuleCatalog();
}

In the Initialize method on the ModuleInit class I'm trying to change the view of the mainregion.

public class ModuleInit : IModule
{
    private IUnityContainer container;
    private IRegionManager regionManager;

    public ModuleInit(IUnityContainer container, IRegionManager regionManager)
    {
        this.container = container;
        this.regionManager = regionManager;
    }

    public void Initialize()
    {
        // TODO This works
        //regionManager.RegisterViewWithRegion("MainRegion", () => container.Resolve<View>());

        // TODO This doesn't work
        container.RegisterType<object, View>("View1");
        regionManager.Regions["MainRegion"].RequestNavigate(new Uri("View1", UriKind.Relative));
    }
}

Using view discovery works but not view injection.

Was it helpful?

Solution 2

The problem was in my Bootstrapper.

In the InitializeShell method I resolved a Shell from the container using this code:

Application.Current.MainWindow = (Window)ServiceLocator.Current.GetInstance<Shell>();

I changed this to instead use the already initialized shell:

Application.Current.MainWindow = (Window)this.Shell;

Im still a bit confused by this but I think it has to do with unity creating new object and not singletons per default when resolving.

OTHER TIPS

Change the following line;

regionManager.RequestNavigate("MainRegion", "View1");

to this;

var view1 = new Uri("View1", UriKind.Relative);
regionManager.RequestNavigate("MainRegion", view1);

Also, it seems there is another problem, you are calling () => container.Resolve<View>() when displaying the view with RegisterViewWithRegion. Then it seems you have already registered the view. But in the "not working" part, you are registering the View again.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top