uwp 获取自定义媒体控件中视觉状态更改的通知

uwp get notification for visual state changed in Custom Media Controls

在我的 uwp 应用程序中,我有 自定义媒体传输控件,我想在我的控件出现在屏幕上和从屏幕上消失时收到通知,以便我可以匹配光标的出现和消失还有。

this is what I have tried so far :

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 });
}

I wired it all up and further logic is being done on the page, which is irrelevant in this case. I debugged with a break point and found out that ControlsFade_CurrentStateChanged is never firing.

在我的自定义控件中,我在媒体控件的 UnLoaded 事件(包括 ControlsFade_CurrentStateChanged)中取消订阅与我的自定义媒体控件相关的所有事件,并且事实证明,每当控件进入全屏时,都会触发 Unloaded 事件,因此它删除了对此事件的订阅,因此此后它没有触发。所以我注释掉了卸载事件,现在它按预期工作了。

Unsubscription of events is important to prevent memory leaks, please let me know in the comments how I can unsubscribe without causing this issue.