Оптимизация WPF - PullRequest
       2

Оптимизация WPF

0 голосов
/ 14 октября 2018

Итак, у меня есть приложение, в котором есть страницы кнопок.Каждая страница может иметь до 100 кнопок, и пользователь может перемещаться от страницы к странице.Проблема в том, что каждая кнопка вызывает количество конвертеров, чем больше кнопок на странице, тем больше времени требуется для переключения с одной страницы на другую ....

Я выделил проблему на количество конвертеров(на каждой кнопке) комментирование кода в конвертерах не имеет никакого значения, поэтому дело не в этом .....

В App.xaml определяет "POSButtonContent" определяет и "POSButton" стиль

        <DataTemplate po:Freeze="True" x:Key="POSButtonContent">
            <LocalItemCount:AdornedControl
            HorizontalAdornerPlacement="Outside" VerticalAdornerPlacement="Outside"
            IsAdornerVisible="{Binding First_Product.Local_Item_Count, Converter={Converters:ShowLocalItemCount}}" AdornerOffsetX="17" AdornerOffsetY="15">

            <Button x:Name="POSButton" Style="{StaticResource POS_Button}" >

                <Button.IsEnabled>
                    <MultiBinding Converter="{Converters:DisablePOSButtonConverter}">
                        <MultiBinding.Bindings>
                            <Binding />
                            <Binding Path="DataContext.First_Product.Local_Item_Count" ElementName="POSButton" />
                            <Binding Path="DataContext.SetLocalStockAvailability" ElementName="Base" />
                        </MultiBinding.Bindings>
                    </MultiBinding>
                </Button.IsEnabled>

            </Button>

            <LocalItemCount:AdornedControl.AdornerContent>
                    <StackPanel HorizontalAlignment="Left">

                        <Border Background="Green" CornerRadius="2" Width="auto" BorderThickness="1" BorderBrush="Black">
                            <Label MinWidth="15" Content="{Binding First_Product.Local_Item_Count}" Padding="0.5" FontSize="12" Foreground="White"  HorizontalAlignment="Left" VerticalAlignment="Center" HorizontalContentAlignment="Center"/>

                            <Border.Style>
                                <Style TargetType="{x:Type Border}">
                                    <Setter Property="Background" Value="Green"/>
                                    <Style.Triggers>
                                        <DataTrigger Value="True">
                                            <DataTrigger.Binding>
                                                <MultiBinding Converter="{Converters:ShowLocalItemCountWarning}">
                                                    <Binding Path="First_Product.Local_Item_Count"/>
                                                    <Binding Path="First_Product.Min_Local_Item_Count"/>
                                                </MultiBinding>
                                            </DataTrigger.Binding>
                                            <DataTrigger.EnterActions>
                                                <BeginStoryboard x:Name="LocalItemCountStoryboard">
                                                    <Storyboard FillBehavior="HoldEnd">
                                                        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
                                                  Duration="0:0:1"
                                                  FillBehavior="HoldEnd"
                                                  RepeatBehavior="Forever">
                                                            <DiscreteColorKeyFrame KeyTime="0:0:0.5" Value="Green"/>
                                                            <DiscreteColorKeyFrame KeyTime="0:0:0.5" Value="Red"/>
                                                        </ColorAnimationUsingKeyFrames>
                                                    </Storyboard>
                                                </BeginStoryboard>
                                            </DataTrigger.EnterActions>
                                            <DataTrigger.ExitActions>
                                                <StopStoryboard BeginStoryboardName="LocalItemCountStoryboard"/>
                                            </DataTrigger.ExitActions>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </Border.Style>

                        </Border>
                    </StackPanel>
                </LocalItemCount:AdornedControl.AdornerContent>

            </LocalItemCount:AdornedControl>

        </DataTemplate>


        <Style x:Key="POS_Button" po:Freeze="True" TargetType="{x:Type Button}" x:Name="POSButton">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate po:Freeze="True" TargetType="{x:Type Button}">
                        <Button FontSize="{Binding POS_Button_Style.FontSize}" HorizontalContentAlignment="Center" Padding="2" Foreground="{Binding POS_Button_Style.Foreground}" FontFamily="{Binding POS_Button_Style.TypeFace}" Style="{StaticResource Default}" Command="{Binding DataContext.ButtonCommand, ElementName=Base}" CommandParameter="{Binding}" Margin="3" >
                            <StackPanel>
                                <Grid>
                                    <Grid.CacheMode>
                                        <BitmapCache EnableClearType="False" 
                                        RenderAtScale="1" 
                                        SnapsToDevicePixels="False" />
                                    </Grid.CacheMode>

                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="*" />
                                    </Grid.RowDefinitions>

                                    <Viewbox Grid.Row="1" Margin="10" Visibility="{Binding DataContext.ShowButtonText, ElementName=Base}" MaxWidth="{Binding ActualHeight, ElementName=POSButton}" MaxHeight="{Binding ActualHeight, ElementName=POSButton}" x:Name="vb1">
                                        <Image x:Name="ButtonImage" Grid.Row="1" Margin="0" 
                                           Source="{Binding ImagePath, TargetNullValue={x:Null}}"
                                           RenderOptions.BitmapScalingMode="HighQuality" HorizontalAlignment="Center" VerticalAlignment="Center"
                                           Stretch="Fill" />
                                    </Viewbox>

                                    <Label Padding="0" HorizontalAlignment="Left" VerticalContentAlignment="Center" Background="Transparent" FontSize="{Binding FontSize, ElementName=displayname}" FontFamily="{Binding FontFamily, ElementName=displayname}" Foreground="{Binding Foreground, ElementName=displayname}" Content="{Binding  DataContext.KeyboardBuffer, ElementName=Base}" Grid.Row="1" >
                                        <Label.Visibility>
                                            <MultiBinding Converter="{Converters:KBBufferTextVisibilityConverter}" >
                                                <Binding Path="Source" ElementName="ButtonImage" />
                                                <Binding Path="DataContext.ShowButtonText" ElementName="Base"/>
                                                <Binding Path="LNK_ButtonLinks"/>
                                            </MultiBinding>
                                        </Label.Visibility>
                                    </Label>

                                    <TextBlock x:Name="displayname" Grid.Row="1" TextWrapping="Wrap" TextAlignment="Center" Text="{Binding DisplayName}" >
                                        <TextBlock.Visibility>
                                            <MultiBinding Converter="{Converters:ButtonTextVisibilityConverter}" >
                                                <Binding Path="Source" ElementName="ButtonImage" />
                                                <Binding Path="DataContext.ShowButtonText" ElementName="Base"/>
                                                <Binding Path="LNK_ButtonLinks"/>
                                            </MultiBinding>
                                        </TextBlock.Visibility>
                                    </TextBlock>

                                </Grid>
                                <Thumb Visibility="{Binding DataContext.ResizeButtons, ElementName=Base}"  VerticalAlignment="Bottom" Margin="0,0,0,0"
                                        DragDelta="OnResizeThumbDragDelta" 
                                        DragStarted="OnResizeThumbDragStarted" 
                                        DragCompleted="OnResizeThumbDragCompleted">
                                    <Thumb.Style>
                                        <Style TargetType="{x:Type Thumb}" BasedOn="{x:Null}">
                                            <Setter Property="Template">
                                                <Setter.Value>
                                                    <ControlTemplate>
                                                        <Grid x:Name="resizeVisual" DockPanel.Dock="Right" VerticalAlignment="Bottom">
                                                            <Grid.Style>
                                                                <Style TargetType="{x:Type Grid}">
                                                                    <Style.Triggers>
                                                                        <Trigger Property="IsMouseOver" Value="True">
                                                                            <Setter Property="Cursor" Value="SizeNWSE"/>
                                                                        </Trigger>
                                                                    </Style.Triggers>
                                                                </Style>
                                                            </Grid.Style>
                                                            <Canvas Background="White" Width="35" Height="35">
                                                                <Path Width="35" Height="35" Stretch="Fill" Fill="#FF000000" Data="F1 M 30.25,58L 18,58L 18,45.75L 22,41.75L 22,50.75L 30,42.75L 33.25,46L 25.25,54L 34.25,54L 30.25,58 Z M 58,45.75L 58,58L 45.75,58L 41.75,54L 50.75,54L 42.75,46L 46,42.75L 54,50.75L 54,41.75L 58,45.75 Z M 45.75,18L 58,18L 58,30.25L 54,34.25L 54,25.25L 46,33.25L 42.75,30L 50.75,22L 41.75,22L 45.75,18 Z M 18,30.25L 18,18L 30.25,18L 34.25,22L 25.25,22L 33.25,30L 30,33.25L 22,25.25L 22,34.25L 18,30.25 Z "/>
                                                            </Canvas>
                                                        </Grid>
                                                    </ControlTemplate>
                                                </Setter.Value>
                                            </Setter>
                                        </Style>
                                    </Thumb.Style>
                                </Thumb>
                            </StackPanel>
                        </Button>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

И затем в представлении ..... есть сетка с кнопкой в ​​каждой ячейке (в ContentPresenter)

<Grid RenderOptions.CachingHint="Cache">
    <Grid.CacheMode>
        <BitmapCache EnableClearType="False" 
                                 RenderAtScale="1" 
                                 SnapsToDevicePixels="False" />
    </Grid.CacheMode>
    <Button Width="1" Height="1" Command="{Binding DataContext.DecoyButtonPress, ElementName=Base}" CommandParameter="Decoy Button Pressed" AttachedProperties:FocusExtension.IsFocused="{Binding DataContext.FocusDecoy, ElementName=Base}" ></Button>
    <ItemsControl ItemsSource="{Binding Buttons}"  VirtualizingPanel.VirtualizationMode="Recycling"
          VirtualizingPanel.IsVirtualizing="True"
          ScrollViewer.CanContentScroll="True">
        <!-- ItemsPanelTemplate -->
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*" />
                        <RowDefinition Height="*" />
                        <RowDefinition Height="*" />
                        <RowDefinition Height="*" />
                        <RowDefinition Height="*" />
                        <RowDefinition Height="*" />
                        <RowDefinition Height="*" />
                        <RowDefinition Height="*" />
                        <RowDefinition Height="*" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>

                    <Grid.CacheMode>
                        <BitmapCache EnableClearType="False" 
                                 RenderAtScale="1" 
                                 SnapsToDevicePixels="False"  />
                    </Grid.CacheMode>

                    <Grid.Style>
                        <Style TargetType="{x:Type Grid}">
                            <Setter Property="Background" Value="Transparent"/>
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding DataContext.IsViewSelected, ElementName=ContentMenu_ViewUI}" Value="True">
                                    <DataTrigger.EnterActions>
                                        <BeginStoryboard x:Name="SelectedViewStoryboard">
                                            <Storyboard FillBehavior="Stop">
                                                <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Grid.Background).(SolidColorBrush.Color)"
                                                  Duration="0:0:2"
                                                  FillBehavior="HoldEnd"
                                                  RepeatBehavior="Forever">
                                                    <DiscreteColorKeyFrame KeyTime="0:0:0" Value="Red"/>
                                                    <DiscreteColorKeyFrame KeyTime="0:0:1" Value="Transparent"/>
                                                </ColorAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </BeginStoryboard>
                                    </DataTrigger.EnterActions>
                                    <DataTrigger.ExitActions>
                                        <StopStoryboard BeginStoryboardName="SelectedViewStoryboard"/>
                                    </DataTrigger.ExitActions>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Grid.Style>

                </Grid>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>

        <!-- ItemContainerStyle -->
        <ItemsControl.ItemContainerStyle>
            <Style>
                <Setter Property="Grid.Column" Value="{Binding Column}" />
                <Setter Property="Grid.Row" Value="{Binding Row}" />

                <!-- Can either use a DataTrigger to determine these values, 
                 or store them on the object itself -->
                <Setter Property="Grid.ColumnSpan" Value="{Binding ColumnSpan}" />
                <Setter Property="Grid.RowSpan" Value="{Binding RowSpan}" />
            </Style>
        </ItemsControl.ItemContainerStyle>

        <ItemsControl.ItemTemplate>
            <DataTemplate>

                <ContentPresenter Content="{Binding}" ContentTemplate="{StaticResource POSButtonContent}"/>

            </DataTemplate>
        </ItemsControl.ItemTemplate>

    </ItemsControl>
</Grid>

Буду признателен за любые идеи о том, как использовать XAML...... Спасибо.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...