我有大约15个属性,每个控件绑定到数据库。
我的应用程序需要提供可以由客户端设置的HorizontalAlignment,VerticalAlignment,Margin,Background,Foreground,...........等属性。因此,我在数据库中创建了一个表,以便在客户端设置它们时保存这些属性的值。我在ViewModel的构造函数中检索该属性的值。
但是当我的应用程序运行时,它需要7到8分钟才能完全启动。
以下是我的代码的一小部分示例:
XAML
<TextBlock Grid.Row="{Binding HaemogramRowHaemoglobinTest}" Grid.Column="{Binding HaemogramColumnHaemoglobinTest}"
Grid.RowSpan="{Binding HaemogramRowSpanHaemoglobinTest}" Grid.ColumnSpan="{Binding HaemogramColumnSpanHaemoglobinTest}"
Text="{Binding HaemogramTextHaemoglobinTest}"
Visibility="{Binding HaemogramVisibilityHaemoglobinTest, Converter={StaticResource booleanToVisibilityConverter}}"
Background="{Binding HaemogramBackgroundHaemoglobinTest, Converter={StaticResource colorNameToSolidColorBrushConverter}}"
Foreground="{Binding HaemogramForegroundHaemoglobinTest, Converter={StaticResource colorNameToSolidColorBrushConverter}}"
FontFamily="{Binding HaemogramFontNameHaemoglobinTest, Converter={StaticResource stringToFontFamilyConverter}}"
FontSize="{Binding HaemogramFontSizeHaemoglobinTest}"
FontWeight="{Binding HaemogramFontBoldHaemoglobinTest, Converter={StaticResource booleanToBoldConverter}}"
FontStyle="{Binding HaemogramFontItalicsHaemoglobinTest, Converter={StaticResource booleanToItalicsConverter}}"
TextDecorations="{Binding HaemogramFontUnderlineHaemoglobinTest, Converter={StaticResource booleanToUnderlineConverter}}"
HorizontalAlignment="{Binding HaemogramHorizontalAlignmentHaemoglobinTest, Converter={StaticResource intToHorizontalAlignmentConverter}}"
VerticalAlignment="{Binding HaemogramVerticalAlignmentHaemoglobinTest, Converter={StaticResource intToVerticalAlignmentConverter}}"
Margin="{Binding HaemogramMarginHaemoglobinTest}" >
视图模型
public MainWindowViewModel()
{
using (Lab_Lite_Entities db = new Lab_Lite_Entities())
{
HaemogramRowTest = db.Designs.Where(d => d.MasterPage.Value == "Haemogram Report" && d.FieldName == "Test").Select(d => d.ParentGridRow).FirstOrDefault();
HaemogramColumnTest = db.Designs.Where(d => d.MasterPage.Value == "Haemogram Report" && d.FieldName == "Test").Select(d => d.ParentGridColumn).FirstOrDefault();
HaemogramRowSpanTest = db.Designs.Where(d => d.MasterPage.Value == "Haemogram Report" && d.FieldName == "Test").Select(d => d.ParentGridRowSpan).FirstOrDefault();
HaemogramColumnSpanTest = db.Designs.Where(d => d.MasterPage.Value == "Haemogram Report" && d.FieldName == "Test").Select(d => d.ParentGridColumnSpan).FirstOrDefault();
HaemogramTextTest = db.Designs.Where(d => d.MasterPage.Value == "Haemogram Report" && d.FieldName == "Test").Select(d => d.TextField).FirstOrDefault();
HaemogramVisibilityTest = db.Designs.Where(d => d.MasterPage.Value == "Haemogram Report" && d.FieldName == "Test").Select(d => d.Visibility).FirstOrDefault();
HaemogramBackgroundTest = db.Designs.Where(d => d.MasterPage.Value == "Haemogram Report" && d.FieldName == "Test").Select(d => d.BackgoundColour).FirstOrDefault();
HaemogramForegroundTest = db.Designs.Where(d => d.MasterPage.Value == "Haemogram Report" && d.FieldName == "Test").Select(d => d.TextColour).FirstOrDefault();
HaemogramFontNameTest = db.Designs.Where(d => d.MasterPage.Value == "Haemogram Report" && d.FieldName == "Test").Select(d => d.FontName).FirstOrDefault();
HaemogramFontSizeTest = db.Designs.Where(d => d.MasterPage.Value == "Haemogram Report" && d.FieldName == "Test").Select(d => d.FontSize).FirstOrDefault();
HaemogramFontBoldTest = db.Designs.Where(d => d.MasterPage.Value == "Haemogram Report" && d.FieldName == "Test").Select(d => d.Bold).FirstOrDefault();
HaemogramFontItalicsTest = db.Designs.Where(d => d.MasterPage.Value == "Haemogram Report" && d.FieldName == "Test").Select(d => d.Italics).FirstOrDefault();
HaemogramFontUnderlineTest = db.Designs.Where(d => d.MasterPage.Value == "Haemogram Report" && d.FieldName == "Test").Select(d => d.Underline).FirstOrDefault();
HaemogramHorizontalAlignmentTest = db.Designs.Where(d => d.MasterPage.Value == "Haemogram Report" && d.FieldName == "Test").Select(d => d.HorizontalAlignment).FirstOrDefault();
HaemogramVerticalAlignmentTest = db.Designs.Where(d => d.MasterPage.Value == "Haemogram Report" && d.FieldName == "Test").Select(d => d.VerticalAlignment).FirstOrDefault();
double? HaemogramMarginLeftTest = db.Designs.Where(d => d.MasterPage.Value == "Haemogram Report" && d.FieldName == "Test").Select(d => d.MarginLeft).FirstOrDefault();
double? HaemogramMarginTopTest = db.Designs.Where(d => d.MasterPage.Value == "Haemogram Report" && d.FieldName == "Test").Select(d => d.MarginTop).FirstOrDefault();
double? HaemogramMarginRightTest = db.Designs.Where(d => d.MasterPage.Value == "Haemogram Report" && d.FieldName == "Test").Select(d => d.MarginRight).FirstOrDefault();
double? HaemogramMarginBottomTest = db.Designs.Where(d => d.MasterPage.Value == "Haemogram Report" && d.FieldName == "Test").Select(d => d.MarginBottom).FirstOrDefault();
HaemogramMarginTest = new Thickness((double)HaemogramMarginLeftTest, (double)HaemogramMarginTopTest, (double)HaemogramMarginRightTest, (double)HaemogramMarginBottomTest);
}
}
...
...
And the properties are declared with implementation of OnPropertyChanged.
现在问题是有超过1500个属性,其值来自ViewModel的构造函数。由于我的申请变得非常慢。
有没有办法让应用程序更快一些?
答案 0 :(得分:8)
您似乎有很多冗余的数据库选择。稍微重写块应该可以提高性能。我不确定您的数据类型,所以我在下面的字段中使用了默认null
;
public MainWindowViewModel()
{
using (Lab_Lite_Entities db = new Lab_Lite_Entities())
{
// A single select from the database
var design = db.Designs.FirstOrDefault(
d => d.MasterPage.Value == "Haemogram Report" &&
d.FieldName == "Test");
// Build your members from that select
HaemogramRowTest = design != null ? design.ParentGridRow : null;
HaemogramColumnTest = design != null ? design.ParentGridColumn : null;
...