调用OnDisappearing方法时如何在网络视图中停止youtube视频。
我具有以下XAML Web视图:
<ContentPage.Content>
<StackLayout>
<ActivityIndicator x:Name="Progress" IsEnabled="true" IsVisible="true" IsRunning="true" Color="Blue"/>
<WebView x:Name="webView" VerticalOptions="FillAndExpand" Navigated="Handle_Navigated" Navigating="Handle_Navigating" />
</StackLayout>
</ContentPage.Content>
,并且在我的 .cs 文件中包含以下(修剪后的)代码:
string name = o.Link;
string youtubeLink = "https://www.youtube.com/embed/";
string url = string.Concat(youtubeLink,name);
webView.Source = url;
}
protected override void OnDisappearing()
{
base.OnDisappearing();
// webView.IsEnabled = false;
}
当页面调用OnDisappearing
方法时,我想停止webview youtube视频,但目前不是这样。
答案 0 :(得分:0)
您要停止呈现WebView。您可以将WebView Source设置为随机页面,也可以使用以下方法:
public interface ICustomWebViewRenderer
{
void Pause();
void Resume();
}
Xamarin.Forms
CustomWebView:public class CustomWebView : WebView
{
public ICustomWebViewRenderer Renderer;
public void Pause()
{
if (renderer != null) renderer.Pause();
}
public void Resume()
{
if (renderer != null) renderer.Resume();
}
}
Xamarin.Android
CustomWebViewViewRenderer:public class CustomWebViewRenderer : WebViewRenderer, ICustomWebViewRenderer
{
Android.Webkit.WebView webView;
public CustomWebViewRenderer(Android.Content.Context context) : base(context) { }
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
{
base.OnElementChanged(e);
webView = Control;
if (e.OldElement != null)
(e.OldElement as CustomWebView).Renderer = null;
if (e.NewElement != null)
(e.NewElement as CustomWebView).Renderer = this;
}
public void Pause()
{
if (webView != null) webView.OnPause();
}
public void Resume()
{
if (webView != null) webView.OnResume();
}
}
XAML
:<local:CustomWebView x:Name="webView" Source="https://youtube.com" />
OnDisappearing
/ OnAppearing
:protected override void OnDisappearing()
{
webView.Pause();
base.OnDisappearing();
}
protected override void OnAppearing()
{
base.OnAppearing();
webView.Resume();
}