Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#双动画平滑度_C#_Wpf - Fatal编程技术网

C#双动画平滑度

C#双动画平滑度,c#,wpf,C#,Wpf,我正在尝试使用故事板和双动画淡出UIElement,该元素确实淡出,但它似乎分两步完成,而不是逐渐改变其持续时间。看起来它从1跳到0.5到0,只做了3次更改,但我希望它能将不透明度更改得比这平滑得多 这是我目前的代码: public void fadeout(UIElement target, int targetopac = 0, int fadetime = 1000) { Storyboard FadeOut = new Storyboard(); DoubleAnimat

我正在尝试使用故事板和双动画淡出
UIElement
,该元素确实淡出,但它似乎分两步完成,而不是逐渐改变其持续时间。看起来它从1跳到0.5到0,只做了3次更改,但我希望它能将不透明度更改得比这平滑得多

这是我目前的代码:

public void fadeout(UIElement target, int targetopac = 0, int fadetime = 1000)
{
    Storyboard FadeOut = new Storyboard();
    DoubleAnimation Fade = new DoubleAnimation();
    Fade.From = target.Opacity;
    Fade.To = targetopac;
    Fade.Duration = TimeSpan.FromMilliseconds(fadetime);
    FadeOut.Children.Add(Fade);
    Storyboard.SetTarget(Fade, target);
    Storyboard.SetTargetProperty(Fade, new PropertyPath("(UIElement.Opacity)"));
    FadeOut.Begin();
}
编辑:该代码似乎在64位系统上运行得很好,但我在跳转时尝试的任何32位系统仍然会发生

我不确定为什么您的C#代码不能按预期工作,但如果XAML代码对您有用,这里是XAML中的一个例子。我觉得XAML更清晰、更简洁。 在这里,我绑定动画以在鼠标单击元素时触发,例如:

<Grid Background="Red" >
    <Grid.Triggers>
        <EventTrigger RoutedEvent="MouseDown">
            <BeginStoryboard>
                <Storyboard x:Key="">
                    <DoubleAnimation To="0" Duration="00:00:01" Storyboard.TargetProperty="Opacity" AutoReverse="True" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Grid.Triggers>
</Grid>

我想你得做些类似的事情。
30
为所需帧速率。我对wpf动画不是很在行,尤其是通过代码完成的时候。当我使用它们时,通常是通过XAML。是的,我试过了,但问题仍然存在,我对XAML不太熟悉,所以我尝试使用c#来实现。你是不是同时用其他代码烧掉了ui线程?顺便说一句,你可以使用一个没有故事板的动画来制作这样的单属性动画。
<Grid Background="Red" x:Name="MyGrid">
    <Grid.Resources>
        <Storyboard x:Key="OpacityStoryboard">
            <DoubleAnimation To="0" Duration="00:00:01"
                             Storyboard.Target="{x:Reference Name=MyGrid}"
                             Storyboard.TargetProperty="Opacity"
                             AutoReverse="True" />
        </Storyboard>
    </Grid.Resources>
</Grid>
(MyGrid.Resources["OpacityStoryboard"] as Storyboard).Begin();