Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.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_Xaml_Mvvm - Fatal编程技术网

C# 消除代码隐藏中的情节提要已完成处理程序

C# 消除代码隐藏中的情节提要已完成处理程序,c#,wpf,xaml,mvvm,C#,Wpf,Xaml,Mvvm,一点背景: 我正在构建一个窗口,我有一个故事板,我用它来在用户启动的任务完成后短暂显示一个弹出窗口,无需用户确认。看起来是这样的: <UserControl.Resources> <Storyboard x:Name="FadingFeedback" x:Key="FadingFeedback" Completed="FadingFeedback_Completed"> <DoubleAnimation

一点背景:

我正在构建一个窗口,我有一个故事板,我用它来在用户启动的任务完成后短暂显示一个弹出窗口,无需用户确认。看起来是这样的:

<UserControl.Resources>
    <Storyboard x:Name="FadingFeedback" x:Key="FadingFeedback" Completed="FadingFeedback_Completed">
        <DoubleAnimation  
                         Storyboard.TargetProperty="Opacity" 
                         From="0.5" 
                         To="0" 
                         BeginTime="0:0:0" 
                         Duration="0:0:2.0">
            <DoubleAnimation.EasingFunction>
                <ExponentialEase Exponent="10" EasingMode="EaseIn" />
            </DoubleAnimation.EasingFunction>
        </DoubleAnimation>
    </Storyboard>
</UserControl.Resources>
如果合适,ViewModel将设置
DoShowMessage
属性,XAML中的绑定将显示弹出窗口并开始故事板

现在,我已经在弹出窗口的代码隐藏中完成了
处理程序,它只执行以下操作:

    void StatusFader_Completed(object sender, EventArgs e)
    {
        popup.IsOpen = false;
    }
这就足够了,因为
popup.IsOpen
绑定到ViewModel
DoShowMessage
上的底层属性,它将重置该布尔值

我的问题是:


情节提要完成后,是否有“更好”的方法来处理DoShowMessage属性的重置?或者换言之,在XAML本身中是否有这样做的方法?我已经阅读了关于代码隐藏应该(大部分)没有视图代码的观点/约定,但是(至少对我来说)这(事件处理程序)似乎是一种合理的方法。想知道在XAML中是否有这样做的方法。

是的,当然可以用另一种方法。通过使用关键帧的对象动画设置
popup.IsOpen=“False”

例如:

情节提要

<Storyboard x:Name="FadingFeedback" x:Key="FadingFeedback"> 
    <DoubleAnimation Storyboard.TargetProperty="Opacity" From="0.5" 
                             To="0" BeginTime="0:0:0" Duration="0:0:2.0">

        <DoubleAnimation.EasingFunction>
            <ExponentialEase Exponent="10" EasingMode="EaseIn" />
        </DoubleAnimation.EasingFunction>
    </DoubleAnimation>

    <ObjectAnimationUsingKeyFrames BeginTime="0:0:2.0" Storyboard.TargetProperty="(Popup.IsOpen)">
        <DiscreteObjectKeyFrame KeyTime="0:0:0">
            <DiscreteObjectKeyFrame.Value>
                <sys:Boolean>False</sys:Boolean>
            </DiscreteObjectKeyFrame.Value>
        </DiscreteObjectKeyFrame>
    </ObjectAnimationUsingKeyFrames>
</Storyboard>

<Popup Name="popup" Placement="Center" PopupAnimation="Fade" AllowsTransparency="True" IsOpen="{Binding DoShowMessage}">
    <Popup.Style>
        <Style>
            <Style.Triggers>
                <DataTrigger Binding="{Binding DoShowMessage}" Value="True">
                <!--<Setter Property="Popup.IsOpen" Value="True" />--> <!-- not necessarily, because we have Popup.IsOpen = True -->

                    <DataTrigger.EnterActions>
                        <BeginStoryboard Storyboard="{StaticResource FadingFeedback}" />
                    </DataTrigger.EnterActions>
                </DataTrigger>
            </Style.Triggers>
            </Style>
    </Popup.Style>  
    ...
</Popup>        
代码隐藏

private void OpenButton_Click(object sender, RoutedEventArgs e)
{
    MyPopup.BeginAnimation(Popup.IsOpenProperty, null);
    MyPopup.IsOpen = true;
}     
有一个选择,或者只需在事件
情节提要_Completed
中拖放
弹出窗口.IsOpen
为False,或者在动画中执行,但要访问代码,必须将其删除


对于我自己,我通常这样做,创建一个附加的依赖属性(布尔值),将触发动画绑定到此属性。如果它的值为true,则启动动画,但要重新运行,在事件
情节提要\u Completed
中,我必须将属性折叠为false。就我个人而言,这是一种更简单的方法

感谢安纳托利-我正在阅读你写的东西,并试图决定什么对我有意义。
<Storyboard x:Name="FadingFeedback" x:Key="FadingFeedback"> 
    <DoubleAnimation Storyboard.TargetProperty="Opacity" From="0.5" 
                             To="0" BeginTime="0:0:0" Duration="0:0:2.0">

        <DoubleAnimation.EasingFunction>
            <ExponentialEase Exponent="10" EasingMode="EaseIn" />
        </DoubleAnimation.EasingFunction>
    </DoubleAnimation>

    <ObjectAnimationUsingKeyFrames BeginTime="0:0:2.0" Storyboard.TargetProperty="(Popup.IsOpen)">
        <DiscreteObjectKeyFrame KeyTime="0:0:0">
            <DiscreteObjectKeyFrame.Value>
                <sys:Boolean>False</sys:Boolean>
            </DiscreteObjectKeyFrame.Value>
        </DiscreteObjectKeyFrame>
    </ObjectAnimationUsingKeyFrames>
</Storyboard>

<Popup Name="popup" Placement="Center" PopupAnimation="Fade" AllowsTransparency="True" IsOpen="{Binding DoShowMessage}">
    <Popup.Style>
        <Style>
            <Style.Triggers>
                <DataTrigger Binding="{Binding DoShowMessage}" Value="True">
                <!--<Setter Property="Popup.IsOpen" Value="True" />--> <!-- not necessarily, because we have Popup.IsOpen = True -->

                    <DataTrigger.EnterActions>
                        <BeginStoryboard Storyboard="{StaticResource FadingFeedback}" />
                    </DataTrigger.EnterActions>
                </DataTrigger>
            </Style.Triggers>
            </Style>
    </Popup.Style>  
    ...
</Popup>        
<Grid>
    <Grid.Triggers>
        <EventTrigger SourceName="CloseButton" RoutedEvent="Button.Click">
            <BeginStoryboard>
                <Storyboard>
                    <BooleanAnimationUsingKeyFrames Storyboard.TargetName="MyPopup"
                                                    Storyboard.TargetProperty="(Popup.IsOpen)" 
                                                    Duration="0:0:0">

                        <DiscreteBooleanKeyFrame KeyTime="0:0:0" Value="False" />
                    </BooleanAnimationUsingKeyFrames>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Grid.Triggers>

    <Popup x:Name="MyPopup" Width="200" Height="200" IsOpen="True">
        <Grid Background="Azure">
            <Label Content="Test label" />
        </Grid>
    </Popup>

    <Button Name="OpenButton" Content="OpenButtonFromCode" Width="140" Height="30" Click="OpenButton_Click" />
    <Button Name="CloseButton" Content="CloseButtonfromEventTrigger" Width="180" Height="30" Margin="0,80,0,0" />
</Grid>
private void OpenButton_Click(object sender, RoutedEventArgs e)
{
    MyPopup.BeginAnimation(Popup.IsOpenProperty, null);
    MyPopup.IsOpen = true;
}