NonSharedViews-QuickStart applicable to wizard-style navigation

Jul 31, 2011 at 2:57 PM
Edited Jul 31, 2011 at 3:00 PM

Hi forum,

I'm seeking assistance on a slight variation of the scenario illustrated in the NonSharedViews QuickStart.

My setup is quite similar to the QuickStart except that I don't need to display several NonShared Views in parallel inside a ListBox - instead I'd like to display a set of NonShared Views in sequence inside a ContentControl, and I'd like to be able to navigate back and forth in this sequence like in a typical webshop checkout wizard.

The general idea is to replace the ListBox/JounceViewConverter combo bound to a ViewModel-collection from the QuickStart

<ListBox ItemsSource="{Binding ViewModels}">
  <ListBox.ItemTemplate>
    <DataTemplate>
      <ContentControl Content="{Binding Converter={StaticResource ViewConverter}}"/>
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>

with a ContentControl/JounceViewConverter combo bound to a single ViewModel as in

<ContentControl Content="{Binding ViewModel, Converter={StaticResource ViewConverter}}" />

and be done.

Turns out though, that the present JounceViewConverter tries to create a new NonShared View whenever the bound ViewModel changes - specifically on backward navigation a new instance of a previously used View type is created and connected to the ViewModel.

This only works if the bound ViewModel itself is also created anew for each navigation step. If I try to reuse a ViewModel instance from a previous forward step for a backward navigation step the JounceViewConverter effectively tries to associate a second instance of the same View type with the preexisting ViewModel resulting in an Exception from within BaseViewModel.RegisterVisualState().

For my wizard navigation scenario there are a couple of advantages in reusing Views and ViewModels of previous steps during backward navigation - no repeated initializations, less garbage collection, preserved View state such as focus position, etc. - but it seems to me that I can only make this work with a modified ViewConverter that keeps a cache of previously created Views per ViewModel and thus is not stateless.

So, concluding the long introduction: What are your thoughts on implementing wizard-style navigation with NonSharedViews in Jounce? Have you done it yourself? Would you prefer a purely stateless approach with new Views/ViewModels on backward navigation as suggested by the JounceViewConverter, would you find a stateful variant suitable and if so could you make it work with the existing JounceViewConverter?

Cheers and thanks in advance
DrJ