Toast弹出菜单只显示一次

本文关键字:一次 显示 菜单 Toast | 更新日期: 2023-09-27 18:00:20

我有以下"烤面包机"弹出窗口的XAML:

<Popup x:Name="popupMessage"
           Width="500"
           Height="100"
           IsOpen="False"
           Placement="Top"
           PlacementTarget="{Binding ElementName=statusBarMain}"
           StaysOpen="True">
        <Popup.Style>
            <Style>
                <Style.Triggers>
                    <Trigger Property="Popup.IsOpen" Value="True">
                        <Trigger.EnterActions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(ScaleTransform.ScaleY)">
                                        <SplineDoubleKeyFrame KeyTime="0:0:0" Value="0" />
                                        <SplineDoubleKeyFrame KeyTime="0:0:0.5" Value="1" />
                                    </DoubleAnimationUsingKeyFrames>
                                    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)">
                                        <SplineDoubleKeyFrame KeyTime="0:0:0" Value="0" />
                                        <SplineDoubleKeyFrame KeyTime="0:0:0.5" Value="1" />
                                        <SplineDoubleKeyFrame KeyTime="0:0:2" Value="1" />
                                        <SplineDoubleKeyFrame KeyTime="0:0:4" Value="0" />
                                    </DoubleAnimationUsingKeyFrames>
                                    <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="IsOpen">
                                        <DiscreteBooleanKeyFrame KeyTime="0:0:0" Value="True" />
                                        <DiscreteBooleanKeyFrame KeyTime="0:0:4" Value="False" />
                                    </BooleanAnimationUsingKeyFrames>
                                </Storyboard>
                            </BeginStoryboard>
                        </Trigger.EnterActions>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Popup.Style>
        <Popup.RenderTransform>
            <ScaleTransform ScaleY="1" />
        </Popup.RenderTransform>
        <Border Width="504"
                Height="104"
                BorderBrush="#FF0F3D5C"
                BorderThickness="2">
            <Border Width="500"
                    Height="100"
                    BorderBrush="White"
                    BorderThickness="2">
                <Border.Background>
                    <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                        <GradientStop Offset="0" Color="#FFD3CCF5" />
                        <GradientStop Offset="1" Color="#FF0F3D5C" />
                    </LinearGradientBrush>
                </Border.Background>
                <TextBlock x:Name="textBlockMessage"
                           HorizontalAlignment="Center"
                           VerticalAlignment="Center"
                           FontSize="18"
                           Foreground="White"
                           Text="{Binding NotificationMessage}" />
            </Border>
        </Border>
    </Popup>

这个弹出菜单的问题是它似乎只工作一次。我设置了popupMessage.IsOpen = true,这显示了一次弹出窗口。所有后续调用都不会出现弹出窗口。我检查了一下,IsOpen属性在动画结束时确实设置为false。

很明显,我在这里错过了什么,但什么?

Toast弹出菜单只显示一次

您需要在触发器的ExitActions中停止故事板。

试试这个:

<Trigger Property="Popup.IsOpen" Value="True">
   <Trigger.EnterActions>
      <BeginStoryboard Name="OpenStoryboard">
          <Storyboard>
              <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(ScaleTransform.ScaleY)">
                  <SplineDoubleKeyFrame KeyTime="0:0:0" Value="0" />
                  <SplineDoubleKeyFrame KeyTime="0:0:0.5" Value="1" />
              </DoubleAnimationUsingKeyFrames>
              <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)">
                  <SplineDoubleKeyFrame KeyTime="0:0:0" Value="0" />
                  <SplineDoubleKeyFrame KeyTime="0:0:0.5" Value="1" />
                  <SplineDoubleKeyFrame KeyTime="0:0:2" Value="1" />
                  <SplineDoubleKeyFrame KeyTime="0:0:4" Value="0" />
              </DoubleAnimationUsingKeyFrames>
              <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="IsOpen">
                  <DiscreteBooleanKeyFrame KeyTime="0:0:0" Value="True" />
                  <DiscreteBooleanKeyFrame KeyTime="0:0:4" Value="False" />
              </BooleanAnimationUsingKeyFrames>
          </Storyboard>
      </BeginStoryboard>
   </Trigger.EnterActions>
   <Trigger.ExitActions>
      <StopStoryboard BeginStoryboardName="OpenStoryboard"/>
   </Trigger.ExitActions>
</Trigger>

我在使用动画更改控件的背景颜色时也遇到了类似的问题,而且它也只能工作一次。