简体   繁体   English

实例化特定用户控件时,性能显着下降

[英]Significant performance drop when instantiating specific usercontrols

I am developing a WPF app from 2 locations, one has a beefy PC on windows 10, the other has a much older PC with worse hardware, running Windows 7. 我正在从2个位置开发WPF应用程序,一个在Windows 10上拥有强大的PC,另一个在运行Windows 7上具有较差硬件的旧PC。

on the windows 10 PC, I never notice a problem, this is only reproducible on windows 7 machine. 在Windows 10 PC上,我从来没有注意到任何问题,这仅在Windows 7计算机上可以重现。

In my Window, I have a ContentControl which is bound to a UserControl property in the window's viewmodel. 在我的窗口中,我有一个ContentControl,它绑定到窗口的viewmodel中的UserControl属性。

I can show different usercontrols there, and most behave fine. 我可以在那里显示不同的用户控件,并且大多数都可以正常运行。 I have 2 however, which when I attempt to load them, they take literally 3-5 seconds, during which the UI thread hangs. 但是我有2个,当我尝试加载它们时,它们实际上需要3-5秒,在此期间UI线程将挂起。

They don't have much going on in xaml, I have more complex stuff in xaml in other controls so I don't think the problem lies there. 他们在xaml中没有太多的事情,在其他控件中我在xaml中还有更复杂的内容,因此我认为问题不存在于此。

The one thing these 2 controls are doing differently, which other controls which don't have this issue aren't doing is in the constructor for the usercontrol in the .xaml.cs file, they set their datacontext. 这两个控件的作用不同,而其他没有此问题的控件没有做的事情是在.xaml.cs文件中usercontrol的构造函数中,它们设置了datacontext。

InitializeComponent();
DataContext = new CreateGroupPanelViewModel();

The contructor for the viewmodel, just sets some properties to values. 视图模型的构造函数只是将一些属性设置为值。 Mainly some string to string.Empty, instantiates 2 ObservableCollections, sets a bool to true and sets a string to "Create Game". 主要是一些字符串到string.Empty,实例化2个ObservableCollections,将bool设置为true,并将字符串设置为“ Create Game”。

The first time I open this control, I have the problem but the second time it poses no delay at all. 第一次打开此控件时,我遇到了问题,但第二次完全没有延迟。 Also, if I run the program for a minute or so and THEN try to open it for the first time it will also be fine. 另外,如果我运行该程序一分钟左右,然后尝试第一次打开它,那也没问题。

I cannot understand why instantiating this usercontrol would create such a massive performance hit, or why it would act so differently on the 2 different machines. 我不明白为什么实例化此用户控件会造成如此大的性能损失,或者为什么它在两台不同的计算机上的作用如此不同。

EDIT The slowdown occurs within the InitializeComponent(); 编辑减速发生在InitializeComponent()中; of the usercontrol. 用户控件的

Here is the XAML of such a control: 这是此类控件的XAML:

<UserControl x:Class="CasinoDB.UserControls.ModifyPanels.UCModifyGamePanel"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:CasinoDB.UserControls.ModifyPanels"
             xmlns:vm="clr-namespace:CasinoDB.ViewModels"
             mc:Ignorable="d"
             d:DesignHeight="500" d:DesignWidth="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition />
        </Grid.RowDefinitions>
        <TextBlock Grid.Row="0" Text="{Binding Header}" HorizontalAlignment="Center" Margin="5" FontWeight="Bold" FontSize="20" />

        <StackPanel Grid.Row="1" HorizontalAlignment="Center">
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="5">
                <TextBlock Text="Name:" Margin="5" />
                <TextBox Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}" Margin="5" MinWidth="150" />
            </StackPanel>
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="5">
                <TextBlock Text="RTP:" Margin="5" />
                <TextBox Text="{Binding RTP, UpdateSourceTrigger=PropertyChanged}" Margin="5" MinWidth="50" />
            </StackPanel>
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="5">
                <TextBlock Text="Variance:" Margin="5" />
                <TextBox Text="{Binding Variance, UpdateSourceTrigger=PropertyChanged}" Margin="5" MinWidth="150" />
            </StackPanel>
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="5">
                <TextBlock Text="Lines:" Margin="5" />
                <TextBox Text="{Binding Lines, UpdateSourceTrigger=PropertyChanged}" Margin="5" MinWidth="100" />
            </StackPanel>
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="5">
                <TextBlock Text="Min Bet:" Margin="5" />
                <TextBox Text="{Binding MinBet, UpdateSourceTrigger=PropertyChanged}" Margin="5" MinWidth="50" />
            </StackPanel>
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="5">
                <TextBlock Text="Max Win:" Margin="5" />
                <TextBox Text="{Binding MaxWin, UpdateSourceTrigger=PropertyChanged}" Margin="5" MinWidth="75" />
            </StackPanel>
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="5">
                <TextBlock Text="Vendor:" Margin="5" />
                <TextBox Text="{Binding Vendor, UpdateSourceTrigger=PropertyChanged}" Margin="5" MinWidth="150" />
            </StackPanel>
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="5">
                <TextBlock Text="Notes:" Margin="5" />
                <TextBox Text="{Binding Notes}" Margin="5" Width="250" TextWrapping="Wrap" AcceptsReturn="True" AcceptsTab="True" SpellCheck.IsEnabled="True" Height="75" VerticalScrollBarVisibility="Auto" ScrollViewer.CanContentScroll="True" />
            </StackPanel>
            <CheckBox Content="Create Another" IsChecked="{Binding CreateAnother}" Margin="5" Visibility="{Binding ShowCreateAnother, Converter={StaticResource BooleanToVisibilityConverter}}" HorizontalAlignment="Center" HorizontalContentAlignment="Center" />
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="5">
                <Button Content="SAVE" Command="{Binding Save}" Margin="5" Style="{StaticResource ConfirmButton}" />
                <Button Content="CLOSE" Command="{Binding Close}" Margin="5" />
            </StackPanel>
        </StackPanel>
    </Grid>
</UserControl>

OK, so after attempting to isolate the issue, I finally found my problem. 好的,因此在尝试找出问题之后,我终于找到了问题。 It lies in SpellCheck.IsEnabled="True" on TextBoxes. 它位于TextBoxes上的SpellCheck.IsEnabled SpellCheck.IsEnabled="True"中。 I found this by commenting out large sections, testing performance, then uncommenting small amounts until it was acting slow on the machine it acts slow on. 我发现这是通过注释掉较大的部分,测试性能,然后取消注释少量内容直到它在运行缓慢的计算机上运行缓慢而进行的。

After figuring this was the cause, I found a similar post on SO with information relating to a registry entry being filled up with dictionairies. 在弄清楚原因之后,我在SO上找到了类似的帖子,其中包含与词典条目有关的信息,这些信息被字典填充。 I don't appear to have an entry in that location in the registry, so maybe my problem was similar, but that it was looking and couldn't find anything at all. 我似乎在注册表中的那个位置没有条目,所以也许我的问题是相似的,但是它一直在寻找并且根本找不到任何东西。

Anyway, I can live without spellcheck in those fields, I'll just remove it. 无论如何,我可以不用在这些字段中进行拼写检查就可以生活,我将其删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM