Search code examples
c#wpfxamluser-controlscatel

Catel freezing on usercontrol focus


I got a simple application with nested usercontrol.

MainWindow.xaml:

<Grid x:Name="LayoutRoot">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    
    <TextBox Grid.Row="0">Test TextBox</TextBox>

    <userControls:SuggestUserControl Grid.Row="1" Grid.Column="0"
        DataContext="{Binding CitizenshipViewModel}" />        
</Grid>

Violation of the naming convention is reason for subsequent re-use of this usercontrol. In that case I'm registering custom view model in App.xaml.cs:

var viewLocator = ServiceLocator.Default.ResolveType<IViewModelLocator>();
viewLocator.Register(typeof(SuggestUserControl), typeof(CitizenshipSuggestViewModel));

CitizenshipSuggestViewModel is a simple class with empty constructor, some additional logic (which I commented in test reason) and derived from base class SuggestModule:

    public class SuggestModule<TEntity> : ViewModelBase 
        where TEntity : class, ISuggestable, new()
    {
        #region Private fields

        private readonly IDataBaseService _dataBaseService;

        #endregion


        #region Default constructor

        public SuggestModule(IDataBaseService dataBaseService)
        {
            Argument.IsNotNull(() => dataBaseService);
            _dataBaseService = dataBaseService;

            //Loading data from context
            var collection = _dataBaseService.LoadObservableCollectionOf<TEntity>();
            ItemsCollection = new ObservableCollection<TEntity>(collection);
            ItemsCollection.Sort();
        }

        #endregion

        ...Some logic here...
   }

MainWindowViewModel:

    public class MainWindowViewModel : ViewModelBase
        {
            public MainWindowViewModel(Person person)
            {
                Argument.IsNotNull(()=>person);

                Person = person;
            }
    
            [Model]
            public Person Person
            {
                get => GetValue<Person>(PersonProperty);
                set => SetValue(PersonProperty, value);
            }

            public static readonly PropertyData PersonProperty = 
                RegisterProperty<MainWindowViewModel, Person>(model => model.Person);

            [ViewModelToModel("Person")]
            public Citizenship Citizenship
            {
                get => GetValue<Citizenship>(CitizenshipProperty);
                set => SetValue(CitizenshipProperty, value);
            }

            public static readonly PropertyData CitizenshipProperty = 
                RegisterProperty<MainWindowViewModel, Citizenship>(model => model.Citizenship);

            public Citizenship Citizenship
            {
                get => GetValue<Citizenship>(CitizenshipProperty);
                set => SetValue(CitizenshipProperty, value);
            }
    
            public static readonly PropertyData CitizenshipProperty =
                RegisterProperty<MainWindowViewModel, Citizenship>(model => model.Citizenship);
    
            public IViewModel CitizenshipViewModel
            {
                get => GetValue<IViewModel>(CitizenshipViewModelProperty);
                set => SetValue(CitizenshipViewModelProperty, value);
            }
    
            public static readonly PropertyData CitizenshipViewModelProperty = 
                RegisterProperty<MainWindowViewModel, IViewModel>(model => model.CitizenshipViewModel);
    
            protected override async Task InitializeAsync()
            {
                await base.InitializeAsync();
    
                CitizenshipViewModel = this.GetTypeFactory().CreateInstance<CitizenshipSuggestViewModel>();
            }    
        }
    }

So, when application fully loaded, particularly ItemsCollection on SuggestModule is already filled with data from context, in Log got this information:

12:19:34:844 => [DEBUG] [Catel.MVVM.Views.ViewToViewModelMappingHelper] [1] Initializing view model container to manage ViewToViewModel mappings
12:19:34:864 => [DEBUG] [Catel.MVVM.Views.ViewToViewModelMappingHelper] [1] Initializing view model 'CitizenshipSuggestViewModel'
12:19:34:874 => [DEBUG] [Catel.MVVM.Views.ViewToViewModelMappingHelper] [1] Initialized view model 'CitizenshipSuggestViewModel'
12:19:34:895 => [DEBUG] [Catel.MVVM.Views.ViewToViewModelMappingHelper] [1] Initialized view model container to manage ViewToViewModel mappings

But now, if I getting focus on second TextBox, which is part of nested usercontrol I have about 5...6 seconds freeze. Logging information in this time:

12:23:21:212 => [DEBUG] [Catel.IoC.TypeFactory] [1] Cleared type constructor cache
12:23:21:387 => [DEBUG] [Catel.IoC.TypeFactory] [1] Cleared type constructor cache
12:23:21:809 => [DEBUG] [Catel.IoC.TypeFactory] [1] Cleared type constructor cache
12:23:21:901 => [DEBUG] [Catel.IoC.TypeFactory] [1] Cleared type constructor cache
12:23:22:016 => [DEBUG] [Catel.IoC.TypeFactory] [1] Cleared type constructor cache
12:23:22:063 => [DEBUG] [Catel.IoC.TypeFactory] [1] Cleared type constructor cache
12:23:22:221 => [DEBUG] [Catel.IoC.TypeFactory] [1] Cleared type constructor cache
12:23:22:235 => [DEBUG] [Catel.IoC.TypeFactory] [1] Cleared type constructor cache
12:23:22:585 => [DEBUG] [Catel.IoC.TypeFactory] [1] Cleared type constructor cache
12:23:22:654 => [DEBUG] [Catel.IoC.TypeFactory] [1] Cleared type constructor cache
12:23:22:703 => [DEBUG] [Catel.IoC.TypeFactory] [1] Cleared type constructor cache
12:23:23:019 => [DEBUG] [Catel.IoC.TypeFactory] [1] Cleared type constructor cache
12:23:23:319 => [DEBUG] [Catel.IoC.TypeFactory] [1] Cleared type constructor cache
12:23:23:431 => [DEBUG] [Catel.IoC.TypeFactory] [1] Cleared type constructor cache
12:23:24:105 => [DEBUG] [Catel.IoC.TypeFactory] [1] Cleared type constructor cache
12:23:24:566 => [DEBUG] [Catel.IoC.TypeFactory] [1] Cleared type constructor cache
12:23:24:644 => [DEBUG] [Catel.IoC.TypeFactory] [1] Cleared type constructor cache
12:23:25:803 => [DEBUG] [Catel.IoC.TypeFactory] [1] Cleared type constructor cache
12:23:26:115 => [DEBUG] [Catel.IoC.TypeFactory] [1] Cleared type constructor cache

How can I avoid this behavior? Thank you in advance!


Solution

    • Using InitializeAsync instead of the constructor for data retrieval

    • Using Orc.EntityFramework6 instead of obsolete Catel.Extensions.EntityFramework6

    Are solved my problem completely.