Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I use the following codes to navigate between views:

C#
public class MainPageViewModel : ViewModelBase
    {
        #region Properties
        private object _selectedViewModel;

        public object SelectedViewModel
        {
            get { return _selectedViewModel; }
            set { SetProperty(ref _selectedViewModel, value); }
        }

        private string _pageVisibility;

        public string PageVisibility
        {
            get { return _pageVisibility; }
            set { SetProperty(ref _pageVisibility, value); }
        }

        public ICommand MenuCommand
        {
            get { return new DelegateCommand<object>(SwitchViews, (p) => true); }
        }
        #endregion

        #region Methods
        private void SwitchViews(object param)
        {
            switch (param)
            {
                case "Catalogue":
                    SelectedViewModel = new CatalogueViewModel();
                    BackButtonVisibility = "Visible";
                    break;
                case "EquipmentIdentity":
                    EquipmentIdentityViewModel vm = new EquipmentIdentityViewModel();
                    SelectedViewModel = vm;
                    PageVisibility = "Visible";
                    BackButtonVisibility = "Visible";
                    break;
            }
        }
        #endregion

        public MainPageViewModel()
        {
            PageVisibility = "Collapsed";
        }
    }


ANd then the DataTemplate is:

<!--Equipment Identity view-->
    <DataTemplate DataType="{x:Type VM:EquipmentIdentityViewModel}">
        <View:EquipmentIdentity />
    </DataTemplate>
    
    <!--Catalogue view-->
    <DataTemplate DataType="{x:Type VM:CatalogueViewModel}">
        <View:CatalogueView />
    </DataTemplate>


The code-behind for view is only:

public partial class EquipmentIdentity : UserControl
   {
       public EquipmentIdentity()
       {
           InitializeComponent();
       }
   }


When I run the code, it instantiates the selected ViewModel twice. One time by
EquipmentIdentityViewModel vm = new EquipmentIdentityViewModel();
and another tome by view's code-behind.

How can I fix this propblem?

What I have tried:

I provided my code in the question context.
Posted
Updated 20-Jul-22 20:02pm

1 solution

It's suggesting that you have the VM being created twice. To debug and identify the cause:
1. Place a breakpoint in the constructor of the view model
2. Run the app.
3. When the breakpoint is hit the first time, look at the call stack in the debug window. This will show you the first place it is being initiated.
4. continue the app execution
5. As per step 3, trace where it is being initiated.

If it is being initated by the same code, then that code is being initiated twice. So do the same and trace until you identify where it is happening.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900