0
votes

I am new to using the Prism framework.

I setup my application, and everything was working fine. However, now I am trying to separate the two regions that I have with a module.

Now once I navigate to any view in my second region, I can no longer navigate to any other view in that region.

Here is the code:

    public void OnInitialized(IContainerProvider containerProvider)
    {
        _regionManager = containerProvider.Resolve<IRegionManager>();

        _regionManager.RequestNavigate("ContentRegion", "ClientWindow");

        _factory.BuildString(
            "Model[System.Mode]",
            OnModeChanged);
    }

    public void RegisterTypes(IContainerRegistry containerRegistry)
    {
        containerRegistry.RegisterForNavigation<ClientWindow>();
        containerRegistry.RegisterForNavigation<AnyGame>();
        containerRegistry.RegisterForNavigation<LockupControl>();
        containerRegistry.RegisterForNavigation<MenuControl>();

        Configuration configuration = new Configuration("TargetApplication", Environment.GetCommandLineArgs());
        _model = new ModelManager();
        Listener listener = new Listener(
            configuration.ModelClient,
            connected => ClientConnectionChanged(connected));
        _factory = new BindingFactory(_model);

        containerRegistry.RegisterInstance(configuration);
        containerRegistry.RegisterInstance(_model);
        containerRegistry.RegisterInstance(listener);
        containerRegistry.RegisterInstance(_factory);
    }

    /// <summary>
    /// Handle a connection changed notification on the main UI thread.
    /// </summary>
    /// <param name="connected">True if connected.</param>
    private void ClientConnectionChanged(bool connected)
    {
        if (connected)
        {
            _model.Reset();
        }
        else
        {
            _model.ProcessDisconnected();
        }
    }

    private void OnModeChanged(string mode)
    {
        switch (mode)
        {
            default:
            case "Game":
                _regionManager.RequestNavigate("ClientRegion", "AnyGame");
                break;
            case "Lockup":
                _regionManager.RequestNavigate("ClientRegion", "LockupControl");
                break;
            case "Menu":
                _regionManager.RequestNavigate("ClientRegion", "MenuControl");
                break;
        }
    }

The OnModeChanged method is where I am trying to navigate to different views as the mode of my game changes. Ive put some break points into my code to make sure that I am reaching the RequestNavigate methods correctly, which I am. The first RequestNavigate works as expected, however any time I call these methods after that they dont do anything.

Any help would be greatly appreciated. :) Thanks!

1

1 Answers

0
votes

I have solved this problem.

The method OnModeChanged is trying to navigate to the view model without having proper access to the thread.

private void OnModeChanged(string mode)
    {
        if (Application.Current.Dispatcher != null)
        {
            Application.Current.Dispatcher.BeginInvoke(
                DispatcherPriority.Background,
                new Action(
                    () =>
                    {
                        switch (mode)
                        {
                            default:
                            case "Game":
                                _regionManager.RequestNavigate("GameRegion", "AnyGame");
                                break;
                            case "Lockup":
                                _regionManager.RequestNavigate("GameRegion", "LockupControl");
                                break;
                            case "Menu":
                                _regionManager.RequestNavigate("GameRegion", "MenuControl");
                                break;
                        }
                    }));
        }
    }

This code displays a simple solution to the problem, I just wrapped the code I wanted to excute into an action on the Dispatcher.