Better performance in a WPF system

Asked

Viewed 359 times

2

How to leave a system in high performance?

Follow a MVVM project pattern?

I have the following XAML, but when I run it hangs in time to pass the focus to others textBox. Why?

My system is too slow, I don’t understand why.

<UserControl x:Class="Curriculos_3_Camadas.View.EmpresaView"
         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:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
         xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
         mc:Ignorable="d" 
         Width="auto" Height="auto" Background="White" d:DesignWidth="1232.5" d:DesignHeight="853">
<Grid Margin="0,0,0,77">


    <!-- L I N H A    U M -->
    <TextBlock Text="Código:" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="150,17,0,0"/>
    <TextBox Name="textEmpCodigo" VerticalAlignment="Top" HorizontalAlignment="Left" MinHeight="22" Height="15" Width="170" Margin="196,15,0,0"/>
    <TextBlock VerticalAlignment="Top" HorizontalAlignment="Left" Grid.Row="0" Text="F2" Margin="371,18,0,0" FontWeight="Bold" Foreground="Red"/>
    <TextBlock x:Name="labelUltimoCodigoEmpresa" VerticalAlignment="Top" HorizontalAlignment="Left" Grid.Row="0" Margin="390,18,0,0" Width="177"/>
    <CheckBox x:Name="cbEmpresaInativa" Content="Empresa Inativa" VerticalAlignment="Top" HorizontalAlignment="Left" Grid.Row="0" Margin="600,17,0,0" />

    <TextBlock Text="CNPJ / CPF:" VerticalAlignment="Top" HorizontalAlignment="Left" Grid.Row="0" Margin="130,43,0,0"/>
    <TextBox Name="textEmpCnpjCpf" VerticalAlignment="Top" HorizontalAlignment="Left" Grid.Row="0" MinHeight="22" Height="15" Width="170" Margin="196,40,0,0"/>
    <TextBlock Text="Data de Abertura:" VerticalAlignment="Top" HorizontalAlignment="Left" Grid.Row="0" Margin="390,43,0,0"/>
    <TextBlock x:Name="labelDataAbertura" VerticalAlignment="Top" HorizontalAlignment="Left" Grid.Row="0" Margin="489,43,0,0" Width="111"/>

    <TextBlock Text="Razão Social / Nome:" VerticalAlignment="Top" HorizontalAlignment="Left" Grid.Row="0" Margin="79,67,0,0"/>
    <TextBox Name="textEmpRSNome" VerticalAlignment="Top" HorizontalAlignment="Left" Grid.Row="0" MinHeight="22" Height="15" Width="560" Margin="196,65,0,0"/>

    <!-- L I N H A    D O I S -->
    <Rectangle VerticalAlignment="Top" HorizontalAlignment="Left" Width="1900" Height="25" Fill="#E8E8E8" Margin="0,115,-668,0"/>

    <TabControl Name="tabControl" Margin="150,115,0,38">

        <TabControl.Resources>
            <Style TargetType="TabItem">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="TabItem">
                            <Grid Name="Panel">
                                <Border 
                             Name="Border"
                             BorderBrush="#A9A9A9" 
                             BorderThickness="1,1,1,0" 
                             CornerRadius="3,3,0,0"
                             MinWidth="120">
                                </Border>
                                <ContentPresenter
                                    VerticalAlignment="Center"
                                    HorizontalAlignment="Center"
                                    ContentSource="Header"
                                    Margin="10,2"/>
                            </Grid>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsSelected" Value="True">
                                    <Setter TargetName="Panel" Property="Background" Value="white" />
                                </Trigger>
                                <Trigger Property="IsSelected" Value="False">
                                    <Setter TargetName="Panel" Property="Background" Value="#E8E8E8" />
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </TabControl.Resources>

        <TabItem Name="tiDadosGeraisEmpresas" Header="Dados Gerais" FontSize="12" FontWeight="Bold" IsSelected="True" Margin="70,0,-80,-5" TabIndex="-67">
            <ScrollViewer VerticalScrollBarVisibility="Auto" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2" Margin="-72,0,2,-13">
                <Canvas Grid.Row="1" Height="655">

                    <TextBlock Text="Nome Fantasia:" VerticalAlignment="Top" HorizontalAlignment="Left" FontWeight="Normal" Canvas.Left="26" Canvas.Top="17"/>
                    <TextBox Name="textEmpNomeFantasia" VerticalAlignment="Top" HorizontalAlignment="Left" Width="290" MinHeight="22" Height="15" Canvas.Left="113" Canvas.Top="15"/>
                    <TextBlock Text="Contato:" VerticalAlignment="Top" HorizontalAlignment="Left" FontWeight="Normal" Canvas.Left="448" Canvas.Top="17"/>
                    <TextBox Name="textEmpContato" VerticalAlignment="Top" HorizontalAlignment="Left" Width="240" MinHeight="22" Height="15" Canvas.Left="498" Canvas.Top="15"/>
                    <TextBlock Text="Site:" VerticalAlignment="Top" HorizontalAlignment="Left" FontWeight="Normal" Canvas.Left="784" Canvas.Top="17"/>
                    <TextBox Name="textEmpSite" VerticalAlignment="Top" HorizontalAlignment="Left" Width="240" MinHeight="22" Height="15" Canvas.Left="811" Canvas.Top="15"/>

                    <TextBlock Text="CEP:" VerticalAlignment="Top" HorizontalAlignment="Left" FontWeight="Normal" Canvas.Left="85" Canvas.Top="43"/>
                    <TextBox Name="textEmpCep" VerticalAlignment="Top" HorizontalAlignment="Left" Width="160" MinHeight="22" Height="15" Canvas.Left="113" Canvas.Top="40"/>

                    <TextBlock Text="Código IBGE:" VerticalAlignment="Top" HorizontalAlignment="Left" FontWeight="Normal" Canvas.Left="329" Canvas.Top="43"/>
                    <TextBox Name="textEmpCodigoIbge" VerticalAlignment="Top" HorizontalAlignment="Left" Width="160" MinHeight="22" Height="15" Canvas.Left="403" Canvas.Top="40"/>
                    <TextBlock VerticalAlignment="Top" HorizontalAlignment="Left" Text="F2" FontWeight="Bold" Foreground="Red" Canvas.Left="568" Canvas.Top="43"/>
                    <TextBlock Text="Município:" VerticalAlignment="Top" HorizontalAlignment="Left" FontWeight="Normal" Canvas.Left="343" Canvas.Top="93"/>
                    <TextBox Name="textEmpMunicipio" VerticalAlignment="Top" HorizontalAlignment="Left" Width="210" MinHeight="22" Height="15" Canvas.Left="403" Canvas.Top="90"/>
                    <TextBlock Text="Bairro:" VerticalAlignment="Top" HorizontalAlignment="Left" FontWeight="Normal" Canvas.Left="75" Canvas.Top="93"/>
                    <TextBox Name="textEmpBairro" VerticalAlignment="Top" HorizontalAlignment="Left" Width="190" MinHeight="22" Height="15" Canvas.Left="113" Canvas.Top="90"/>
                    <TextBlock Text="Estado (UF):" VerticalAlignment="Top" HorizontalAlignment="Left" FontWeight="Normal" Canvas.Left="628" Canvas.Top="93"/>
                    <TextBox Name="textEmpUf" VerticalAlignment="Top" HorizontalAlignment="Left" Width="50" MinHeight="22" Height="15" Canvas.Left="696" Canvas.Top="90"/>

                    <TextBlock Text="Tipo de Logradouro:" VerticalAlignment="Top" HorizontalAlignment="Left" FontWeight="Normal" Canvas.Top="68"/>
                    <TextBox Name="textEmpTipoLogradouro" VerticalAlignment="Top" HorizontalAlignment="Left" Width="160" MinHeight="22" Height="15" Canvas.Left="113" Canvas.Top="65"/>
                    <TextBlock Text="Logradouro:" VerticalAlignment="Top" HorizontalAlignment="Left" FontWeight="Normal" Canvas.Left="333" Canvas.Top="68"/>
                    <TextBox Name="textEmpLogradouro" VerticalAlignment="Top" HorizontalAlignment="Left" Width="210" MinHeight="22" Height="15" Canvas.Left="403" Canvas.Top="65"/>
                    <TextBlock Text="Número:" VerticalAlignment="Top" HorizontalAlignment="Left" FontWeight="Normal" Canvas.Left="644" Canvas.Top="68"/>
                    <TextBox Name="textEmpNumero" VerticalAlignment="Top" HorizontalAlignment="Left" Width="115" MinHeight="22" Height="15" Canvas.Left="696" Canvas.Top="65"/>
                    <TextBlock Text="Complemento:" VerticalAlignment="Top" HorizontalAlignment="Left" FontWeight="Normal" Canvas.Left="853" Canvas.Top="63"/>
                    <TextBox Name="textEmpComplemento" VerticalAlignment="Top" HorizontalAlignment="Left" Width="115" MinHeight="22" Height="15" Canvas.Left="936" Canvas.Top="60"/>

                    <TextBlock Text="Inscrição Estadual:" VerticalAlignment="Top" HorizontalAlignment="Left" FontWeight="Normal" Canvas.Top="118" Canvas.Left="12"/>
                    <TextBox Name="textEmpInscEst" VerticalAlignment="Top" HorizontalAlignment="Left" Width="190" MinHeight="22" Height="15" Canvas.Left="113" Canvas.Top="115"/>
                    <TextBlock Text="Cód. Serv.:" VerticalAlignment="Top" HorizontalAlignment="Left" FontWeight="Normal" Canvas.Left="393" Canvas.Top="118"/>
                    <TextBox Name="textEmpCodServ" VerticalAlignment="Top" HorizontalAlignment="Left" Width="160" MinHeight="22" Height="15" Canvas.Left="453" Canvas.Top="115"/>
                    <TextBlock Text="Cód. Ativ.:" VerticalAlignment="Top" HorizontalAlignment="Left" FontWeight="Normal" Canvas.Left="638" Canvas.Top="118"/>
                    <TextBox Name="textEmpCodAtiv" VerticalAlignment="Top" HorizontalAlignment="Left" Width="355" MinHeight="22" Height="15" Canvas.Left="696" Canvas.Top="115"/>

                    <TextBlock Text="Inscrição Municipal:" VerticalAlignment="Top" HorizontalAlignment="Left" FontWeight="Normal" Canvas.Top="143" Canvas.Left="4"/>
                    <TextBox Name="textEmpInscMun" VerticalAlignment="Top" HorizontalAlignment="Left" Width="190" MinHeight="22" Height="15" Canvas.Left="113" Canvas.Top="140"/>
                    <TextBlock Text="Regime Trib.:" VerticalAlignment="Top" HorizontalAlignment="Left" FontWeight="Normal" Canvas.Left="379" Canvas.Top="143"/>
                    <TextBox Name="textEmpRegTrib" VerticalAlignment="Top" HorizontalAlignment="Left" Width="598" MinHeight="22" Height="15" Canvas.Left="453" Canvas.Top="140"/>

                    <TextBlock Text="RNTRC:" VerticalAlignment="Top" HorizontalAlignment="Left" FontWeight="Normal" Canvas.Top="168" Canvas.Left="68"/>
                    <TextBox Name="textEmpRntrc" VerticalAlignment="Top" HorizontalAlignment="Left" Width="190" MinHeight="22" Height="15" Canvas.Left="113" Canvas.Top="165"/>
                    <TextBlock Text="Tipo de Empresa:" VerticalAlignment="Top" HorizontalAlignment="Left" FontWeight="Normal" Canvas.Left="357" Canvas.Top="168"/>
                    <TextBox Name="textEmpTipoEmpresa" VerticalAlignment="Top" HorizontalAlignment="Left" Width="598" MinHeight="22" Height="15" Canvas.Left="453" Canvas.Top="165"/>

                    <TextBlock Text="CNAE - Classificação Nacional de Atividades Econômicas" Canvas.Left="113" Canvas.Top="195"/>

                    <TextBlock Text="Código:" VerticalAlignment="Top" HorizontalAlignment="Left" FontWeight="Normal" Canvas.Top="219" Canvas.Left="68"/>
                    <TextBox Name="textEmpCodigoCnae" VerticalAlignment="Top" HorizontalAlignment="Left" Width="100" MinHeight="22" Height="15" Canvas.Left="113" Canvas.Top="216"/>
                    <TextBlock Text="Descrição:" VerticalAlignment="Top" HorizontalAlignment="Left" FontWeight="Normal" Canvas.Left="243" Canvas.Top="219"/>
                    <TextBox Name="textEmpDescCnae" VerticalAlignment="Top" HorizontalAlignment="Left" Width="644" MinHeight="22" Height="15" Canvas.Left="302" Canvas.Top="216" FontWeight="Normal"/>
                    <Button Name="btnAdicionarCnae" Content="Adicionar" Width="100" Canvas.Left="951" Canvas.Top="214" FontWeight="Bold" TabIndex="-76"/>
                    <DataGrid Name="dataGridCnae" Height="100" Canvas.Left="113" Canvas.Top="243" Width="938" FontWeight="Normal" IsReadOnly="True"
                              AutoGenerateColumns="False" GridLinesVisibility="All">

                        <DataGrid.Columns>
                            <DataGridTextColumn Binding="{Binding Path=codigo}" Header="Código" />
                            <DataGridTextColumn Binding="{Binding Path=descricao}" Header="Descrição" />
                        </DataGrid.Columns>

                    </DataGrid>

                    <TextBlock Text="Contatos" Canvas.Left="113" Canvas.Top="354"/>

                    <TextBlock Text="E-mail:" VerticalAlignment="Top" HorizontalAlignment="Left" FontWeight="Normal" Canvas.Top="378" Canvas.Left="68"/>
                    <TextBox Name="textEmpEmail" VerticalAlignment="Top" HorizontalAlignment="Left" Width="348" MinHeight="22" Height="15" Canvas.Left="114" Canvas.Top="375"/>
                    <TextBlock Text="Observações:" VerticalAlignment="Top" HorizontalAlignment="Left" FontWeight="Normal" Canvas.Left="485" Canvas.Top="378"/>
                    <TextBox Name="textEmpObs1" VerticalAlignment="Top" HorizontalAlignment="Left" Width="386" MinHeight="22" Height="15" Canvas.Left="560" Canvas.Top="375" FontWeight="Normal"/>
                    <Button Name="btnAdicionarEmails" Content="Adicionar" Width="100" Canvas.Left="951" Canvas.Top="372" FontWeight="Bold" TabIndex="-73"/>
                    <DataGrid Name="dataGridEmails" ItemsSource="{Binding}"
                              AutoGenerateColumns="False" HorizontalAlignment="Left" VerticalAlignment="Top" Height="100"
                              Canvas.Left="114" Canvas.Top="414" Width="937" FontWeight="Normal" IsReadOnly="True" GridLinesVisibility="All">

                        <DataGrid.Columns>
                            <DataGridTextColumn Binding="{Binding Path=email}" Header="E-mail (s)" />
                            <DataGridTextColumn Binding="{Binding Path=obs}" Header="Observação" />
                        </DataGrid.Columns>

                    </DataGrid>

                    <TextBlock Text="Prefixo (99):" VerticalAlignment="Top" HorizontalAlignment="Left" FontWeight="Normal" Canvas.Top="520" Canvas.Left="46"/>
                    <TextBox Name="textEmpPrefixo" VerticalAlignment="Top" HorizontalAlignment="Left" Width="100" MinHeight="22" Height="15" Canvas.Left="113" Canvas.Top="517"/>
                    <TextBlock Text="Telefones:" VerticalAlignment="Top" HorizontalAlignment="Left" FontWeight="Normal" Canvas.Left="243" Canvas.Top="520"/>
                    <TextBox Name="textEmpTelefones" VerticalAlignment="Top" HorizontalAlignment="Left" Width="160" MinHeight="22" Height="15" Canvas.Left="302" Canvas.Top="517"/>
                    <TextBlock Text="Observações:" VerticalAlignment="Top" HorizontalAlignment="Left" FontWeight="Normal" Canvas.Left="485" Canvas.Top="520"/>
                    <TextBox Name="textEmpObs2" VerticalAlignment="Top" HorizontalAlignment="Left" Width="386" MinHeight="22" Height="15" Canvas.Left="560" Canvas.Top="517"/>
                    <Button Name="btnAdicionarTelefones" Content="Adicionar" Width="100" Canvas.Left="951" Canvas.Top="515" FontWeight="Bold" TabIndex="-69"/>

                    <DataGrid AutoGenerateColumns="False" HorizontalAlignment="Left" VerticalAlignment="Top"
                              ItemsSource="{Binding}" Name="dataGridTelefones" Height="100" Canvas.Left="114" Canvas.Top="557"
                              Width="937" FontWeight="Normal" GridLinesVisibility="All" IsReadOnly="True">

                        <DataGrid.Columns>
                            <!-- <DataGridTextColumn Binding="{Binding Path=id_empresa}" Header="Código" /> -->
                            <DataGridTextColumn Binding="{Binding Path=prefixo}" Header="Prefixo2" />
                            <DataGridTextColumn Binding="{Binding Path=telefone}" Header="Telefone2" />
                            <DataGridTextColumn Binding="{Binding Path=obs}" Header="Observação2" />
                        </DataGrid.Columns>

                    </DataGrid>

                </Canvas>
            </ScrollViewer>
        </TabItem>

    </TabControl>

    <WindowsFormsHost VerticalAlignment="Top" HorizontalAlignment="Left" Name="wfhSample" Grid.Row="0" Height="50" Width="186" Margin="862,10,0,0">
        <WindowsFormsHost.Child>
            <wf:PictureBox x:Name="picLetras"/>
        </WindowsFormsHost.Child>
    </WindowsFormsHost>
    <Image x:Name="imgCarregaCaptcha" HorizontalAlignment="Left" Height="16" Margin="1051,15,0,0" VerticalAlignment="Top" Width="17" />
    <TextBlock Name="labelCaptcha" Text="Digite os caracteres acima e pressione Enter:" VerticalAlignment="Top" HorizontalAlignment="Left" Grid.Row="0" Foreground="Red" Margin="862,65,0,0"/>
    <TextBox Name="textCaracteres" VerticalAlignment="Top" HorizontalAlignment="Left" MinHeight="22" Height="15" Width="260" Margin="862,86,0,0"/>

    <Rectangle Fill="#E8E8E8" Height="33" VerticalAlignment="Bottom" Width="auto" Margin="0,0,0,-2" />
    <Button x:Name="btnSalvarEmpresa" Content="Salvar (F9)" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="100" Margin="198,0,0,2"/>
</Grid>

1 answer

2


The question is somewhat broad, there is no specific problem but I will try to help, even if in a generic way.

The WPF actually has a lot of details that if not observed will generate performance problems. The mechanism is complex and it is not easy to understand all the implications. Moreover the defaults are not ideas to generate good performance.

I do not possess specific knowledge but there is a lot of material on the subject, I will list some. You will need to read them all, look for new sources, study the subject a lot and profile a lot until you get a good result.

The first thing you need to know is WPF performance analysis tool. Another tool.

That article begins to give a good explanation about the problem.

Here are some tips to improve performance.

Other ways that help improve performance.

One more list of tips.

Important considerations of how improve startup.

One of the articles I like best on the subject.

One more interesting article.

The fact of using MVVM does not help anything, it can even disturb in some cases, but nothing significant. Of course, if you use a bad pattern or do it without a standard, it can get in the way.

  • Mto thanks bigown

Browser other questions tagged

You are not signed in. Login or sign up in order to post.