How to maintain user control?

I have a wizard project that works with a ContentControl that contains user controls. I create an instance through the XAML file in the main window:

<DataTemplate DataType="{x:Type ViewModel:OpeningViewModel}"> <view:OpeningView/> </DataTemplate> <DataTemplate DataType="{x:Type ViewModel:SecondUCViewModel}"> <view:SecondUCView/> </DataTemplate> 

But when I move between UC, it seems that the UC does not work like "keep alive". Each UC switch creates a new instance. How can i avoid this? I want to create only one instance for each UC and move between these instances only without creating new instances.

I know how to write singleton, but my project is based on MVVM, and I'm completely new in WPF, so I'm not sure if this is the best way to do this.

thanks

Update:

Here's the viewModel code:

In viewModel, I have:

private ObservableCollection _pages = null; private NavigationBaseViewModel _currentPage;

  #endregion #region Properties public int CurrentPageIndex { get { if (this.CurrentPage == null) { return 0; } return _pages.IndexOf(this.CurrentPage); } } public NavigationBaseViewModel CurrentPage { get { return _currentPage; } private set { if (value == _currentPage) return; _currentPage = value; OnPropertyChanged("CurrentPage"); } } 

private ICommand _NavigateNextCommand; public ICommand NavigateNextCommand {get {if (_NavigateNextCommand == null) {_NavigateNextCommand = new RelayCommand (param => this.MoveToNextPage (), param => CanMoveToNextPage); } return _NavigateNextCommand; }}

  private ICommand _NavigateBackCommand; public ICommand NavigateBackCommand { get { if (_NavigateBackCommand == null) { _NavigateBackCommand = new RelayCommand(param => this.MoveToPreviousPage(), param => CanMoveToPreviousPage); } return _NavigateBackCommand; } } private bool CanMoveToNextPage { get { return this.CurrentPage != null && this.CurrentPage.CanMoveNext; } } bool CanMoveToPreviousPage { get { return 0 < this.CurrentPageIndex && CurrentPage.CanMoveBack; } } private void MoveToNextPage() { if (this.CanMoveToNextPage) { if (CurrentPageIndex >= _pages.Count - 1) Cancel(); if (this.CurrentPageIndex < _pages.Count - 1) { this.CurrentPage = _pages[this.CurrentPageIndex + 1]; } } } void MoveToPreviousPage() { if (this.CanMoveToPreviousPage) { this.CurrentPage = _pages[this.CurrentPageIndex - 1]; } } 

And a ContentControl that contains a UC bound to CurrentPage

+4
source share
2 answers

You can do this by hard coding UserControls in XAML instead of using DataTemplates. DataTemplates will create new controls every time they are created. However, since you are using MVVM, you can also transfer all the data you want to keep between changes in ViewModels, and make sure that the ViewModel objects are always the same. Then DataTemplates will still create new controls, but they will contain the same information as before.

+1
source

I recently ran into the same issue with my views in MVVM. Basically, I wanted to cache the views that took some time to render. If you are familiar with ViewModelLocator, this approach should be direct.

In a client project (e.g. WPF), I created a ViewLocator class that looks like this:

 public class ViewLocator : ObservableObject { #region Properties private View _myView; public View MyView { get { return _myView; } set { Set(() => MyView, ref _myView, value); } } #endregion Properties #region Constructors public ViewLocator() { RegisterViews(); } #endregion Constructors #region Private Methods private void RegisterViews() { MyView = new View(); } #endregion Private Methods } 

And to use this in a data template, I was specifying ViewLocator as a static application resource, so only one instance was ever created - in my case I injected it into App.xaml. To use the ViewLocator and "View" properties, I did the following:

 <vl:ViewLocator x:Key="ViewLocator" /> <DataTemplate DataType="{x:Type vm:ViewModel}"> <ContentControl Content="{Binding Source={StaticResource ViewLocator}, Path=MyView}" /> </DataTemplate> 

Thus, each view is created only once and can be reused.

0
source

Source: https://habr.com/ru/post/1436888/


All Articles