Catel冻结用户控制焦点

时间:2017-12-08 07:24:22

标签: c# wpf xaml user-controls catel

我使用嵌套的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>

违反命名约定是后续重新使用此用户控件的原因。在这种情况下,我在App.xaml.cs中注册自定义视图模型:

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

CitizenshipSuggestViewModel是一个带有空构造函数的简单类,一些额外的逻辑(我在测试共振中评论过)并派生自基类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>();
            }    
        }
    }

因此,当应用程序完全加载时,特别是SuggestModule上的ItemsCollection已经填充了来自上下文的数据,在Log中得到了这个信息:

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

但现在,如果我专注于第二个TextBox,它是嵌套用户控件的一部分,我有大约5 ... 6秒冻结。记录此时的信息:

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

我该如何避免这种行为? 提前谢谢!

2 个答案:

答案 0 :(得分:1)

  • 使用InitializeAsync而不是构造函数进行数据检索

  • 使用Orc.EntityFramework6代替过时的Catel.Extensions.EntityFramework6

完全解决了我的问题。

答案 1 :(得分:0)

  1. 我们建议使用InitializeAsync而不是构造函数进行数据检索
  2. 您是否将Catel 5.2与VS 2017结合使用?在未连接调试器的情况下运行时是否仍会出现此问题?
相关问题