【发布时间】:2018-11-17 14:15:45
【问题描述】:
在我的 uwp 应用程序中,我有 自定义媒体传输控件,我希望在我的控件出现和从屏幕上消失时收到通知,以便我可以匹配光标的出现和消失。 p>
这是我迄今为止尝试过的:
从 generic.xaml 在我的控件样式中,我发现遵循 VisualStateGroup 控制控件的淡入和淡出。
<VisualStateGroup x:Name="ControlPanelVisibilityStates">
<VisualState x:Name="ControlPanelFadeIn">
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" Storyboard.TargetName="ControlPanel_ControlPanelVisibilityStates_Border">
<EasingDoubleKeyFrame KeyTime="0" Value="0" />
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="1" />
</DoubleAnimationUsingKeyFrames>
<DoubleAnimation Storyboard.TargetProperty="Y" Storyboard.TargetName="TranslateVertical" From="50" To="0.5" Duration="0:0:0.3" />
</Storyboard>
</VisualState>
<VisualState x:Name="ControlPanelFadeOut">
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" Storyboard.TargetName="ControlPanel_ControlPanelVisibilityStates_Border">
<EasingDoubleKeyFrame KeyTime="0" Value="1" />
<EasingDoubleKeyFrame KeyTime="0:0:0.7" Value="0" />
</DoubleAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="IsHitTestVisible" Storyboard.TargetName="ControlPanel_ControlPanelVisibilityStates_Border">
<DiscreteObjectKeyFrame KeyTime="0" Value="False" />
</ObjectAnimationUsingKeyFrames>
<DoubleAnimation Storyboard.TargetProperty="Y" Storyboard.TargetName="TranslateVertical" From="0.5" To="50" Duration="0:0:0.7" />
</Storyboard>
</VisualState>
</VisualStateGroup>
所以我想我应该在我的 OnApplyTemplate 方法中获取这个组,然后将状态更改事件分配给它。
protected override void OnApplyTemplate()
{
//other irrelivent code
ControlsFade = (VisualStateGroup)GetTemplateChild("ControlPanelVisibilityStates");
ControlsFade.CurrentStateChanged +=
ControlsFade_CurrentStateChanged;
base.OnApplyTemplate();
}
public class ControlFadeChangedEventArgs
{
public bool Appeared { get; set; }
}
public event EventHandler<ControlFadeChangedEventArgs> ControlFadeChanged;
private void ControlsFade_CurrentStateChanged(object sender, VisualStateChangedEventArgs e)
{
bool fadein = false;
if (e.NewState.Name == "ControlPanelFadeIn")
fadein = true;
ControlFadeChanged?.Invoke(this, new ControlFadeChangedEventArgs { Appeared = fadein });
}
我把它全部连接起来,并且正在页面上完成进一步的逻辑,这在这种情况下是无关紧要的。我用断点调试,发现 ControlsFade_CurrentStateChanged 永远不会触发。
【问题讨论】:
-
ControlsFade 是否可能为空?要了解发生了什么,我们可能需要完整的样本。
-
你在哪里更改活动的
VisualState? -
我已经发布了答案,请看@MartinZikmund
标签: c# xaml uwp visualstatemanager visualstates