Reputation: 4959
I have a WebView in a page and I'm playing a youtube video. When I navigate away from the page to the previous page on the stack, the youtube video in the web view keeps playing the youtube video. Anyone know how to prevent this and kill the WebView when the user navigates away from the page?
EDIT:
Here is how I'm navigating back
private void HtmlViewerPage_BackRequested(object sender, BackRequestedEventArgs e)
{
if (Viewer.CanGoBack)
{
Viewer.GoBack();
e.Handled = true;
return;
}
{
GoBack will navigate the frame stack. It then allows the youtube video to continue to play in the background.
EDIT: I created a sample project that demonstrates my issue.
https://github.com/themimcompany/WebViewIssue
Upvotes: 0
Views: 421
Reputation: 8591
You've registered BackRequested event in MainPage
and WebViewPage
, but you have not unregistered it when you navigate to other pages. This will cause your issue. When you click back button on WebViewPage
, the BackRequested
handler will be triggered multi times.
You could remove it in OnNavigatingFrom
handler on MainPage.xaml.cs
and WebViewPage.xaml.cs
.
Please see the following code:
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;
}
Upvotes: 1
Reputation: 10785
This will stop it:
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
// this will stop any video from playing back.
WebView.NavigateToString("<html>Unloaded.</html>");
base.OnNavigatingFrom(e);
}
Upvotes: 0
Reputation: 12465
How about overriding the OnNavigatingFrom method and stop the webview?
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
base.OnNavigatingFrom(e);
}
Inside the method try setting the WebView to null, or setting the Source
to null or something else
Upvotes: 1