折叠后,WPF仍然会呈现UserControl

时间:2016-01-25 10:15:58

标签: c# wpf multithreading xaml animation

我有一个加载动画用户控件LoadingAnimation.xaml,它包含在另一个名为Loading.xaml的用户控件中。

LoadingAnimation.xaml的一部分:

    <UserControl x:Class="Universal_updater._1_Presentation.Control.LoadingAnimation"
         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" 
         mc:Ignorable="d" 
         xmlns:system="clr-namespace:System;assembly=mscorlib"
         xmlns:local="clr-namespace:Universal_updater._1_Presentation.Control"
         Height="Auto" Width="Auto">
      <UserControl.Resources>
        <system:String x:Key="txtLoading">Loading...</system:String>
        <Storyboard x:Key="ProgressAnimation" RepeatBehavior="Forever">
        <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="block" Storyboard.TargetProperty="(UIElement.OpacityMask).(SolidColorBrush.Color)">
            <SplineColorKeyFrame KeyTime="00:00:00" Value="Black"/>
            <SplineColorKeyFrame KeyTime="00:00:00.2290000" Value="#EF000000"/>
            <SplineColorKeyFrame KeyTime="00:00:00.4590000" Value="#E2000000"/>

Loading.xaml:

    <UserControl x:Class="Universal_updater._1_Presentation.Loading"
         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:control="clr-namespace:Universal_updater._1_Presentation.Control"
         mc:Ignorable="d" 
         d:DesignHeight="376.5" Width="736
         ">
      <Grid>
        <Label x:Name="lbLoading" Margin="-101,-57,101,57"></Label>
        <control:LoadingAnimation x:Name="LoadingAni" HorizontalAlignment="Center" VerticalAlignment="Center" Panel.ZIndex="10"/>
        <Rectangle x:Name="rcLock" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="376" Stroke="Black" VerticalAlignment="Top" Width="736" Opacity="0.7"/>       
      </Grid>
    </UserControl>

Loading.xaml在我的窗口中使用如下:

    <local:Loading x:Name="loadingWin" HorizontalAlignment="Center" VerticalAlignment="Center" Panel.ZIndex="10" Visibility="{Binding Path=IsVisible, Source={x:Static local:PropertySettings.Instance}}"  IsEnabled="{Binding Path=IsActive, Source={x:Static local:PropertySettings.Instance}}" />

加载动画由我的代码中的静态变量控制:

    public async void UpdateScreen()
    {
        //Activating Loading animation
        PropertySettings.Instance.IsVisible = Visibility.Visible;
        PropertySettings.Instance.IsActive = true;

        await Task.Run(() =>
        {
            Thread.Sleep(4000);
        });

            //Deactivating Loading animation
            PropertySettings.Instance.IsVisible = Visibility.Collapsed;
            PropertySettings.Instance.IsActive = false;

    }

功能正常。加载动画可见,并在任务完成后崩溃。

然而,在我调用异步方法UpdateScreen()之前,cpu使用率为0%。当加载动画可见并处于活动状态时,cpu使用率将达到19%。在加载和禁用加载动画后,cpu使用率达到9%,应该是0%。

我确实运行了WPF性能套装,这导致渲染线程仍在工作并且使用了很多cpu。

我确实尝试在Application.Current.Dispatcher.BeginInvoke内禁用加载动画,但没有运气。 cpu仍然很高:

     Application.Current.Dispatcher.BeginInvoke(new Action(() =>
        {
            //Deactivating Loading animation
            PropertySettings.Instance.IsVisible = Visibility.Collapsed;
            PropertySettings.Instance.IsActive = false;
        }));

为什么程序崩溃后程序的cpu使用率不会达到零?

0 个答案:

没有答案