locked
ViewModel Constructor get called twice

    Question

  • Hi , 

    I am working on windows 8.1 store app development using c# and MVVM Light model. 

    My View Model Constructor is getting called twice from some where  ViewModelLocator is not let me hit the constructor after one navigation so i can not use ViewModelLocator as i need to update my page every time navigation .

    I have used my code something like this in my xaml .

    <Page.DataContext>        <vm:CustomersViewModel/></Page.DataContext>

    and nothing from c# , just  

     this.InitializeComponent(); .

    Is any other way to get rid of this . it hitting the performance of project . any suggestion would be highly appreciable .


    Friday, July 25, 2014 7:16 AM

Answers

  • Thanks Dave for support , 

    Actually i found the issue . I was Using Telerik Grid Control , In that They don't have any option to the Column Header Direct binding (For Localization of header). They told me use the key after data context. and told me touse that key while binding the header using  like this. That solution solved my Header Binding Problem but it has created the new one Calling VM Twice .

    I Posted this in case of any one is wondering or facing the same issue .

     Header="{Binding Path=cusnum , Source={StaticResource ViewModel}}"


       

    </Page.Resources> <vm:CustomersViewModel x:Key="ViewModel" />    </Page.Resources>

    I solved this by using the this code first before DataContext and use datacontext after this code .

    Step 1-

    <Page.Resources><vm:CustomersViewModel x:Key="ViewModel" /></Page.Resources>

    Step 2-

    <Page.DataContext>        <Binding Source="{StaticResource ViewModel}"></Binding>    </Page.DataContext>

    I need to mention that i am using source here instead of old code like 

     
     <Page.DataContext>        <vm:ProductDetailViewModel />    </Page.DataContext>

    Note : The Sequence Matter here  because if you will not follow the sequence while Parsing the Xaml code 

    InitializeComponent(); will throw an error that key not found .


    Monday, August 4, 2014 4:00 AM

All replies

  • do you have a repro sample? there should be something more wrong. Or place a breakpoint in the constructor and check the callstacks.

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#


    Friday, July 25, 2014 9:27 AM
  • Thanks for your response . I did how you said , the callstack was giving me same result both the time , the tapped event on which i was navigating -> from there to "this.InitializeComponent();" and the constructor call .  .I don't have any sample to share . 

    Tuesday, July 29, 2014 1:28 PM
  • navigating twice to the same page?

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Friday, August 1, 2014 8:31 AM
  • Thanks Dave for support , 

    Actually i found the issue . I was Using Telerik Grid Control , In that They don't have any option to the Column Header Direct binding (For Localization of header). They told me use the key after data context. and told me touse that key while binding the header using  like this. That solution solved my Header Binding Problem but it has created the new one Calling VM Twice .

    I Posted this in case of any one is wondering or facing the same issue .

     Header="{Binding Path=cusnum , Source={StaticResource ViewModel}}"


       

    </Page.Resources> <vm:CustomersViewModel x:Key="ViewModel" />    </Page.Resources>

    I solved this by using the this code first before DataContext and use datacontext after this code .

    Step 1-

    <Page.Resources><vm:CustomersViewModel x:Key="ViewModel" /></Page.Resources>

    Step 2-

    <Page.DataContext>        <Binding Source="{StaticResource ViewModel}"></Binding>    </Page.DataContext>

    I need to mention that i am using source here instead of old code like 

     
     <Page.DataContext>        <vm:ProductDetailViewModel />    </Page.DataContext>

    Note : The Sequence Matter here  because if you will not follow the sequence while Parsing the Xaml code 

    InitializeComponent(); will throw an error that key not found .


    Monday, August 4, 2014 4:00 AM