Can I remove all state of viewmodels somehow?

jNormenjNormen SEMember ✭✭

Hi, I'm using Prism and Xamarin Forms.
I want feature toggle and A/B testing in my application so I have made a Skin service.
This works like this:

I have a folder for the Skin in this folder I have the same structure as all other skins. In Those folders I have xaml-files. ContentPages, ContentViews and so on. In my ViewModel folder, I have the ViewModel all my skins can share. So if I have a CartPage in Skin A I also have a CartPage in Skin B. Skin A and Skin B share the same ViewModel. I can also create a ViewModel for some pages inside a Skin for A/B test of new functions as well if I want to.

I register all my views with Prism.Autofac and I also made an override method to locate my ViewModels when using Autowireup and so on.

The problem I have now is event subscriptions.
When I start Skin A everything is fine. I create for example CartPage inside a tabbed page that subscribes to AddToCartEvent. I also create a ProductPage on my TabbedPage that push AddToCartEvent. All works like a charm.

But as soon I switch to Skin B it creates the ViewModel again (as expected) but now I have two event listeners. That update my cart for both Skins though skin A still exist in mem.
I can add unsubscribe OnDesaperaring, but then I will not have the state on my tabbed page between the active views.

So what I want to do is switch skin and make the old skin instances to be removed as well so I can instead use anonymous event methods. So they wipe out themselves.

But I cant find any solution for this :(

Expected behavior:
When Switch skin I want to send the user to my StartPage and the whole application kind of start from zero with just the new skin name set. And no references to all other contentpages, ContentViews in mem

My navigation is like this in pseudocode:
_navigationSerivce.NavigateAsync(${skinName}.MasterDetailPage);

I register all my pages based on skin and name:
A
Foo
FooPage

Registered as:
"A.Foo.FooPage"

B
Foo
FooPage
"B.Foo.FooPage"

A and B are set in the navigation based on active skin

I tried to use ControlTemplate but because of nesting templates, I got Binding problems. And also problems with resource dictionaries of Templates this way.

Posts

  • BrianLagunasBrianLagunas USInsider ✭✭✭✭

    If you are resetting the navigation stack by using an absolute URI when you change skins, then have your ViewModels implement IDestructible and then unsubscribe from any events there. That will eliminate the subscriptions for the ViewModel instances you don't care about.

  • jNormenjNormen SEMember ✭✭

    @BrianLagunas said:
    If you are resetting the navigation stack by using an absolute URI when you change skins, then have your ViewModels implement IDestructible and then unsubscribe from any events there. That will eliminate the subscriptions for the ViewModel instances you don't care about.

    I will try that, thank you :)

  • jNormenjNormen SEMember ✭✭
    edited January 2018

    It worked :smile: cool :)

  • BrianLagunasBrianLagunas USInsider ✭✭✭✭

    Can you provide a sample I can look at?

  • jNormenjNormen SEMember ✭✭

    @BrianLagunas said:
    Can you provide a sample I can look at?

    It worked now. Think it was the fantastic obj folder that did not update :) I removed and it all works like a charm :)

Sign In or Register to comment.