在WPF中使用WebBrowser关闭窗口后音频继续播放

时间:2016-02-26 21:33:28

标签: c# .net wpf xaml caliburn.micro

我有一个由Caliburn.Micro显示的窗口。此窗口包含可以播放YouTube视频的WebBrowser控件。如果正在播放Youtube视频并且用户关闭窗口,则音频将继续在后台播放。当窗口关闭时,如何让音频停止播放?我认为这个观点由于某种原因没有被处理掉。

显示窗口

public void Handle(VideoButtonClick message)
{
    var manager = new WindowManager();
    Video = new VideoViewModel(message.VideoID);
    dynamic settings = new ExpandoObject();
    settings.ResizeMode = ResizeMode.NoResize;
    settings.Icon = null;

    manager.ShowDialog(Video, null, settings);
}

在窗口中显示的视图

<UserControl .....>
    <Grid>
        <WebBrowser ctrl:BrowserBehavior.Html="{Binding VideoHTML}"
                    ScrollViewer.CanContentScroll="False"
                    ScrollViewer.VerticalScrollBarVisibility="Hidden" />
    </Grid>
</UserControl>

1 个答案:

答案 0 :(得分:0)

您可以在IDeactivate

中实施VideoViewModel
public class VideoViewModel : PropertyChangedBase, IDeactivate
{
    private bool _isClosing;
    public bool IsClosing
    {
        get { return _isClosing; }
        set
        {
            _isClosing = value;
            NotifyOfPropertyChange(() => IsClosing);
        }
    }

    public void Deactivate(bool close)
    {
        IsClosing = close;
    }

    public event EventHandler<DeactivationEventArgs> AttemptingDeactivation;
    public event EventHandler<DeactivationEventArgs> Deactivated;
}

Deactivate方法中设置名为IsClosing的属性。

在您看来:

<UserControl .....
             x:Name="ThisUserControl">
    <i:Interaction.Triggers>
        <ei:DataTrigger Binding="{Binding IsClosing}" Value="True">
            <ei:CallMethodAction MethodName="DisposeBrowser" TargetObject="{Binding ElementName=ThisUserControl}"/>
        </ei:DataTrigger>
    </i:Interaction.Triggers>

    <Grid>
        <WebBrowser x:Name="MyWebBrowser"
                    ctrl:BrowserBehavior.Html="{Binding VideoHTML}"
                    ScrollViewer.CanContentScroll="False"
                    ScrollViewer.VerticalScrollBarVisibility="Hidden" />
    </Grid>
</UserControl>

UserControl

的代码隐藏中
public void DisposeBrowser()
{
    MyWebBrowser.Dispose();
}