I have 性能问题 https://stackoverflow.com/q/38248842/1997232我正在努力挖掘原因。
到目前为止,我不确定问题是什么,我的下一个假设是数据模板。问题:使用数据模板有多昂贵?
让我们看看单个数据模板有多贵。下面是mcve.
xaml:
<Window.Resources>
<DataTemplate DataType="{x:Type local:Item}">
<StackPanel>
<TextBlock Text="{Binding Property1}" />
... we will add here more things to see difference
</StackPanel>
</DataTemplate>
</Window.Resources>
<ContentControl Content="{Binding Content}" />
cs:
class Item : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public string Property1 { get; set; } = "1";
public string Property2 { get; set; } = "2";
public string Property3 { get; set; } = "3";
public string Property4 { get; set; } = "4";
public string Property5 { get; set; } = "5";
}
class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public object Content { get; set; } = new Item();
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new ViewModel();
}
}
以下是一些结果(使用 VS Profiler 测量 - 应用程序时间线以毫秒为单位):
Test Nb Ni Presenter Items Bindings
A 1 1 2.42 (0.57) 1.8
B 5 5 3.37 (1.15) 1.8, 0.07, 0.1, 0.05, 0.08
C 5 10 3.63 (1.27) 0.06, 0.07, 0.08, 0.04, 0.04 1.7, 0.08, 0.05, 0.06, 0.05
D 0 1 3.38 (0.6) 2.7