【发布时间】:2016-10-10 10:53:31
【问题描述】:
在我们的 MVVM 应用程序中,在 View 中,DataContext 最初为 null,稍后设置。 视图首先在没有设置 DataContext 的情况下呈现,因此对于绑定,使用默认值或 FallbackValues。一旦设置了 DataContext 并更新了所有绑定,这将导致 UI 发生大量更改。 UI 有点“有弹性”,我可以想象相当多的 CPU 周期被浪费了。 有没有办法在设置 DataContext 之前推迟 View 的呈现?
我们为 ViewModel 查找视图的代码:
<ContentControl
DataContext="{Binding Viewodel}"
Content="{Binding}"
Template="{Binding Converter={converters:ViewModelToViewConverter}}"/>
ViewModelToViewConverter.cs:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
ViewModel viewModel = value as ViewModel;
if (viewModel == null)
{
return null;
}
string modelName = viewModel.ToString();
string mappingId = viewModel.MappingId;
if (!string.IsNullOrEmpty(mappingId))
{
modelName += "_" + mappingId;
}
ControlTemplate controlTemplate = new ControlTemplate();
MappingEntry mappingEntry = ApplicationStore.SystemConfig.GetMappingEntryOnModelName(modelName); // lookup View definition for ViewModel
Type type = mappingEntry != null ? mappingEntry.ViewType : null;
if (type != null)
{
controlTemplate.VisualTree = new FrameworkElementFactory(type);
}
else
{
Logger.ErrorFormat("View not found: {0}", modelName);
}
return controlTemplate;
}
【问题讨论】:
-
也许将可见性绑定到上下文,当上下文不为空时显示?
-
谢谢,这是一个不错且简单的解决方案 :)
标签: c# wpf mvvm datacontext