导航离开WebView后播放YouTube视频

时间:2018-06-14 21:08:15

标签: c# uwp

我在页面中有一个WebView,我正在播放YouTube视频。当我从页面导航到堆栈中的上一页时,Web视图中的YouTube视频会继续播放YouTube视频。任何人都知道如何防止这种情况并在用户离开页面时终止WebView?

编辑:

以下是我如何导航

private void HtmlViewerPage_BackRequested(object sender, BackRequestedEventArgs e)
{
    if (Viewer.CanGoBack)
    {
        Viewer.GoBack();
        e.Handled = true;
        return;
    }
{

GoBack将导航帧堆栈。然后它允许YouTube视频继续在后台播放。

编辑:我创建了一个演示我的问题的示例项目。

https://github.com/themimcompany/WebViewIssue

3 个答案:

答案 0 :(得分:1)

如何覆盖OnNavigatingFrom方法并停止webview?

protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
    base.OnNavigatingFrom(e);
}

在方法内部尝试将WebView设置为null,或将Source设置为null或其他

答案 1 :(得分:1)

您已在MainPageWebViewPage中注册了BackRequested事件,但在导航到其他网页时尚未取消注册。这将导致您的问题。当您单击WebViewPage上的后退按钮时,BackRequested处理程序将被多次触发。

您可以在OnNavigatingFromMainPage.xaml.cs上的WebViewPage.xaml.cs处理程序中将其删除。

请参阅以下代码:

public sealed partial class WebViewPage : Page
{
    public WebViewPage()
    {
        this.InitializeComponent();
    }
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);
        var currentView = SystemNavigationManager.GetForCurrentView();
        currentView.AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;
        SystemNavigationManager.GetForCurrentView().BackRequested += WebViewPage_BackRequested;
        var site = (e.Parameter as Dictionary<string, string>)["site"];
        WebViewer.Navigate(new Uri(site));
    }
    private void WebViewPage_BackRequested(object sender, BackRequestedEventArgs e)
    {
        //always try to go back within the WebView first, then try the frame!
        if (this.Frame.CanGoBack)
        {
            if (WebViewer.CanGoBack)
            {
                WebViewer.GoBack();
                e.Handled = true;

            }
            else
            {
                WebViewer.NavigateToString("<html>Unloaded.</html>");
                WebViewer.NavigateToString("");
                var source = WebViewer.Source; // is cleared to null
                Frame.GoBack();
                e.Handled = true;
            }
        }
    }
    protected override void OnNavigatedFrom(NavigationEventArgs e)
    {
        base.OnNavigatedFrom(e);
        SystemNavigationManager.GetForCurrentView().BackRequested -= WebViewPage_BackRequested;
    }
}
    //MainPage.xaml.cs
    private void AppBackRequested(object sender, BackRequestedEventArgs e)
    {
        Frame rootFrame = Window.Current.Content as Frame;
        if (rootFrame == null)
            return;

        // If we can go back and the event has not already been handled, do so.
        if (rootFrame.CanGoBack && e.Handled == false)
        {
            e.Handled = true;
            rootFrame.GoBack();
        }
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);
        var currentView = SystemNavigationManager.GetForCurrentView();
        currentView.AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;
        SystemNavigationManager.GetForCurrentView().BackRequested += AppBackRequested;
    }

    protected override void OnNavigatedFrom(NavigationEventArgs e)
    {
        base.OnNavigatedFrom(e);
        SystemNavigationManager.GetForCurrentView().BackRequested -= AppBackRequested;
    }

答案 2 :(得分:0)

这将阻止它:

    protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
    {
        // this will stop any video from playing back.
        WebView.NavigateToString("<html>Unloaded.</html>");

        base.OnNavigatingFrom(e);
    }