prism--RequestNavigate when the Module's InitializationMode is ondemand,InteractionRequest can't raised corrected

Topics: Prism v4 - WPF 4
Feb 15, 2012 at 4:03 PM

my goal is : in my wpf application, i have a region -- content, and two module A and B , InitializationMode is set to ondemand. when i open the application, dynamic load the A module , then i click a button, dynamic load the B module and open a subwindow. in B module's view, i define a InteractionRequest. but the interactionrequest.raised is null when the first time .   i have found the view.loaded  event is later than the OnNavigatedTo.

because my english is poor. here is my demo link

https://skydrive.live.com/redir.aspx?cid=c4630016e6dc9cf8&resid=C4630016E6DC9CF8!158&parid=C4630016E6DC9CF8!154&authkey=!ABzgMO42W50cVqs

can someone help me? thanks a lot!

 

Developer
Feb 17, 2012 at 5:42 PM

Hi,

Based on my understanding it´s likely that your problem is caused by a threading issue. If you plan to modify anything that will affect UI state, you will need to dispatch the response to the UI thread, typically using the thread Dispatcher.

You'll find more information regarding that in the following link of the Prism documentation:

I hope you find this useful,

Agustin Adami
http://blogs.southworks.net/aadami

Feb 19, 2012 at 7:57 AM
aadami wrote:

Hi,

Based on my understanding it´s likely that your problem is caused by a threading issue. If you plan to modify anything that will affect UI state, you will need to dispatch the response to the UI thread, typically using the thread Dispatcher.

You'll find more information regarding that in the following link of the Prism documentation:

I hope you find this useful,

Agustin Adami
http://blogs.southworks.net/aadami


hi,i have checked the thread , both the view and viewmodel run in the same thread . but the viewmodel execute earlyer than the view. because i found out that view.loaded event is later executed than the OnNavigatedTo. so the first time interactionrequest object.raised is null. 

the other way, why ImoduleManage.loadModuleCompleted event had executed but the Module's view have not loaded completed?

can you spend a little time check my code above? thanks a lot.

Developer
Feb 29, 2012 at 5:34 PM

Hi,

If I understood correctly, you want to raise an interaction request from the BVM view model when the ModuleB is initialized and the BPage view is added to the region. I have checked your repro-sample application and I believe a possible approach to achieve the aforementioned functionality could be to subscribe to the Loaded event of the BPage view and raise the interaction request after that event is raised. This can be done, for example, doing something like this:

  • Add the following method in the BVM view model:

        public void OnViewReady(object sender, RoutedEventArgs e)
        {
            if (this.executeOnViewReady == true)
            {
                OKCommand.Execute(null);
            }
        }

  • Add the following member in the BVM view model:

        private bool executeOnViewReady = false;

  • Replace the OKCommand.Execute(null); sentence in the OnNavigatedTo method of the BVM view model with this.executeOnViewReady = true;
  • Modify the vm property in the BPage view to include the following line in its setter:

        this.Loaded += value.OnViewReady;

Like this, in the OnNavigatedTo method, we set a flag to inform that an interaction request should be raised when the view is ready. When the Loaded event of the view is raised, if the aforementioned flag is set, the interaction request will be raised correctly.

However, take into account that the implementation details for the aforementioned functionality will mainly depend of your personal preferences and the requirements of your scenario.

I hope you find this useful,

Damian Cherubini
http://blogs.southworks.net/dcherubini