WPF图表更新项目源上的CPU使用量很大

时间:2017-02-09 14:48:22

标签: wpf charts

首先,我想道歉,因为我不知道如何以正确的方式提出问题。 我不确定如何描述我遇到的问题。

我的wpf应用程序中有一个页面,我在图表上绘制了一些lineseries。

<Page x:Class="Zentralvakuum_Archivauswertung.Views.Zentrale"
  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:Zentralvakuum_Archivauswertung.Views"
  xmlns:DV="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit"
  xmlns:DVC="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
  xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
  mc:Ignorable="d" 
  DataContext="{Binding Source={StaticResource ViewModelLocator}, Path=ZentraleVm}"
  d:DesignHeight="300" d:DesignWidth="300"
  Title="Zentrale">

<Grid>

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

    <DVC:Chart Name="Chart" Width="auto" Height="auto" Title="Something" grid.row="0">
        <DVC:Chart.Series>
            <DVC:LineSeries Title="1"
        IndependentValueBinding="{Binding Path=Key}"
        DependentValueBinding="{Binding Path=Value}" ItemsSource="{Binding Istwerte1Liste, UpdateSourceTrigger=PropertyChanged}" >
                <DVC:LineSeries.DataPointStyle>
                    <Style TargetType="DVC:LineDataPoint">
                        <Setter Property="Template" Value="{x:Null}" />
                        <Setter Property="Background" Value="Black"/>
                        <Setter Property="Opacity" Value="0" />
                    </Style>
                </DVC:LineSeries.DataPointStyle>
            </DVC:LineSeries>
            <DVC:LineSeries Title="1a"
        IndependentValueBinding="{Binding Path=Key}"
        DependentValueBinding="{Binding Path=Value}" ItemsSource="{Binding Sollwerte1Liste, UpdateSourceTrigger=PropertyChanged}">
                <DVC:LineSeries.DataPointStyle>
                    <Style TargetType="DVC:LineDataPoint">
                        <Setter Property="Template" Value="{x:Null}" />
                        <Setter Property="Background" Value="Green"/>
                        <Setter Property="Opacity" Value="0" />
                    </Style>
                </DVC:LineSeries.DataPointStyle>
            </DVC:LineSeries>
            <DVC:LineSeries Title="2"
        IndependentValueBinding="{Binding Path=Key}"
        DependentValueBinding="{Binding Path=Value}" ItemsSource="{Binding Istwerte2Liste, UpdateSourceTrigger=PropertyChanged}" >
                <DVC:LineSeries.DataPointStyle>
                    <Style TargetType="DVC:LineDataPoint">
                        <Setter Property="Template" Value="{x:Null}" />
                        <Setter Property="Background" Value="Blue"/>
                        <Setter Property="Opacity" Value="0" />
                    </Style>
                </DVC:LineSeries.DataPointStyle>
            </DVC:LineSeries>
            <DVC:LineSeries Title="2a"
        IndependentValueBinding="{Binding Path=Key}"
        DependentValueBinding="{Binding Path=Value}" ItemsSource="{Binding Sollwerte2Liste, UpdateSourceTrigger=PropertyChanged}">
                <DVC:LineSeries.DataPointStyle>
                    <Style TargetType="DVC:LineDataPoint">
                        <Setter Property="Template" Value="{x:Null}" />
                        <Setter Property="Background" Value="Red"/>
                        <Setter Property="Opacity" Value="0" />
                    </Style>
                </DVC:LineSeries.DataPointStyle>
            </DVC:LineSeries>
        </DVC:Chart.Series>
    </DVC:Chart>


    <Grid Grid.Row="1">
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>

        <StackPanel Grid.Column="0" HorizontalAlignment="Center">
            <Label DockPanel.Dock="Top" HorizontalAlignment="Center" FontWeight="Bold" FontSize="20" Content="Start:"/>
            <xctk:DateTimePicker Value="{Binding Start, UpdateSourceTrigger=PropertyChanged}" Format="FullDateTime" ShowDropDownButton="False" HorizontalContentAlignment="Center"/>
            <Button Content="Actualize" Command="{Binding ActualizeCommand}"/>
        </StackPanel>

        <StackPanel Grid.Column="1" HorizontalAlignment="Center">
            <Label DockPanel.Dock="Top" HorizontalAlignment="Center" FontWeight="Bold" FontSize="20" Content="Ende:"/>
            <xctk:DateTimePicker Value="{Binding End, UpdateSourceTrigger=PropertyChanged}" Format="FullDateTime" ShowDropDownButton="False" HorizontalContentAlignment="Center"/>
            <Button Content="Actualize" Command="{Binding ActualizeCommand}"/>
        </StackPanel>

    </Grid>

</Grid>

这些页面第一次打开时一切正常,页面打开,lineseries被绘制完成。

当我更新lineseries的项目资源时,lineseries被绘制并且...应用程序冻结很长一段时间导致CPU使用率很高。我把这张图片联系起来了。

green square = frist opening of the page, red square = update of itemsource (green bar = chart updated, all my code is done)

更新后的功能:

        public void Actualize()
    {

        WerteZentrale = null;
        WerteZentraleCollection = null;

        //Get values from db
        WerteZentrale = new Vakuumverlauf_ZentraleEntities1();
        WerteZentraleCollection = new ListCollectionView((from a in WerteZentrale.Werte_Zentrale0 where a.TimeString != string.Empty select a).ToList());

        //filter
        WerteZentraleCollection.Filter = (found) =>
        {
            Werte_Zentrale0 a = found as Werte_Zentrale0;
            return (Convert.ToDateTime(a.TimeString) >= Start && Convert.ToDateTime(a.TimeString) <= Ende);
        };

        //sort
        WerteZentraleCollection.CustomSort = new DateTimeComparerWerte();

        //empty observablecollections
        Istwerte1ListeDummy.Clear();
        Sollwerte1ListeDummy.Clear();
        Istwerte2ListeDummy.Clear();
        Sollwerte2ListeDummy.Clear();
        Istwerte1Liste.Clear();
        Sollwerte1Liste.Clear();
        Istwerte2Liste.Clear();
        Sollwerte2Liste.Clear();

        foreach (Werte_Zentrale0 Wert in WerteZentraleCollection)
        {
            //1
            if (Wert.VarName == "VakuumWerte_VK1_Istwert_Istwert")
            {
                Istwerte1ListeDummy.Add(new KeyValuePair<DateTime, double?>(Convert.ToDateTime(Wert.TimeString), Wert.VarValue));
            }

            //1a
            if (Wert.VarName == "VakuumWerte_VK1_Sollwert")
            {
                Sollwerte1ListeDummy.Add(new KeyValuePair<DateTime, double?>(Convert.ToDateTime(Wert.TimeString), Wert.VarValue));
            }

            //2
            if (Wert.VarName == "VakuumWerte_VK2_Istwert_Istwert")
            {
                Istwerte2ListeDummy.Add(new KeyValuePair<DateTime, double?>(Convert.ToDateTime(Wert.TimeString), Wert.VarValue));
            }

            //2a
            if (Wert.VarName == "VakuumWerte_VK2_Sollwert")
            {
                Sollwerte2ListeDummy.Add(new KeyValuePair<DateTime, double?>(Convert.ToDateTime(Wert.TimeString), Wert.VarValue));
            }

        }

        //write to itemsource
        Istwerte1Liste = Istwerte1ListeDummy;
        Sollwerte1Liste = Sollwerte1ListeDummy;
        Istwerte2Liste = Istwerte2ListeDummy;
        Sollwerte2Liste = Sollwerte2ListeDummy;

    }

此函数在viewmodel的构造函数中调用,以及在ActualizeCommand中调用。

所以对我来说,我在两种情况下做同样的事情,但结果不同,我不知道为什么。

0 个答案:

没有答案
相关问题