我得到了一个嵌套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上,这是嵌套的usercontrol的一部分,我有大约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条答案
按热度按时间ffscu2ro1#
完全解决了我的问题。
luaexgnf2#
1.我们建议使用InitializeAsync而不是构造函数进行数据检索
1.您是否将Catel 5.2与VS 2017结合使用?当您在没有连接调试器的情况下运行时,问题是否仍然存在?