This project is read-only.

Region Navigation Windows 8 - Change data of Parent Container

May 22, 2014 at 12:42 PM

I´m using Region navigation in my project which has the following set of screens:

Main Screen - It´s a xaml LayoutAwarePage and it´s viewModel is PageViewModel
--HomeRegion - it´s a userControl and it´s viewModel is a RegionAware
--Second Region - it´s another userControl with also a RegionAware viewModel
--ContentView - collection of userControls in a flipView, all viewModels are RegionAware.

The HomeRegion is presentend inside the MainScreen using the same way is presented on the ExampleApp RegionNavigation - in a ContentPresenter.

My question is, can this HomeRegion change any state (viewModel that will reflect on the UI) in my MainScreen?

Any help is very much appreciated.

May 22, 2014 at 2:55 PM
Hi Ralph,

One way would be to send messages back and forth between the ViewModels. The other option you could look at is sharing some class that brokers data changes between the two ViewModels (usually it's just a class that is marked as a Singleton that shares data).

The message route is pretty straight forward so you may want to look at that first.

May 22, 2014 at 6:28 PM
Hi Ian, thanks for your reply.

I was trying something I think is closer to you first suggestion. In my PageViewModel (which serves my MainScreen) I added the attributes Export and Singleton. In my viewModel HomeRegionViewModel, which serves my UserControl (RegionAware) that is loaded in a ContentPresenter in the Main Screen, I then import the viewModel PageViewModel. I was able to change it´s properties, but the changes does not reflect on the UI.

Do you think this is a good aproach or should I make another class to share data between them? Like your second suggestion.

May 22, 2014 at 8:42 PM
Hi Ralph,

That seems as reasonable as any other solution. I think these days I take a far more lenient approach to defining ViewModels and how they interact. I'm sure you could find an MVVM purest that would come up with reasons why each on of these solutions might not be optimal. At the end of the day if it's working well and you are getting software written then that's all that matters.

When I left school I was an academic, these days I'm a pragmatist.

May 23, 2014 at 12:02 AM
Hey Ian, thanks again for your reply.

I agree, code working is what matters. One thing though just to clarify the use of Export Singleton / Import attributes.

My MainViewModel has both Export and Singleton Attributes attached. The HomeRegionViewModel has an import statement to the MainViewModel. Shouldn´t any change made in this injected MainViewModel locally on the HomeRegionViewModel reflect the UI bound to it´s properties?

Like this:

-- binds to string ConnectionStatus, mode=twoWay
-- public string ConnectionStatus {get; set{ SetProperty(ref connectionStatus, value); }

-- [import]
MainViewModel main{get; set;}

public void ChangeConnectionStatus()
main.ConnectionStatus = "Connected";

Am I using Export/Import correctly? I ask because changes made to the main in the context of homeRegion are not propagated to the UI which is bound to the property.
Maybe when I used import I got a new instance of the MainViewModel?
Is there a way to assure that I´m working on the same instance that the UI is?

Thanks a lot.

May 23, 2014 at 1:38 AM
Hi Ralph,

So that should work. I think your binding in the UI would look like {Binding Main.ConnectionStatus} because you are binding to the ConnectionStatus property on the Main property.

For testing purposes sometimes I like to create a new guid inside an instance in a private field that way I can tell if its the same instance or not

May 23, 2014 at 1:20 PM
Hi Ian, thanks again.

I was binding directly to the property in my MainViewModel like this: {Binding ConnectionStatus, Mode=TwoWay}. That´s because the UI in question is served by the MainViewModel.

Like this:

-- has a textblock that shows ConnectionStatus bound to a public property of MainViewModel

-- has a viewModel which does some processing and may change the ConnectionStatus of the MainViewModel

That´s why I needed to export the MainViewModel to HomeRegionViewModel.

I did test the instance guid and turns out I was getting a different instance of my MainViewModel after importing inside HomeRegionViewModel.

After looking into this problem and some troubleshooting I found another user had the same problem here:

In the export of MainViewModel I put [Export(typeof(MainViewModel))] then on the import of the HomeRegionViewModel I put [Import(ImportName="MainViewModel")]

Also, I kept the [Singleton] Attribute on the MainViewModel.

It´s working now!

I appreciate your help! Thanks!