下面的代码用于在 View 上放置一个小的 WebView,以便用户可以点击它,视频以全屏模式打开并播放。一切正常,但在播放 4 秒后,控件消失并且不会重新出现(点击、旋转......)。视频结束后,控件会重新出现,并且“完成”按钮变为可用。但是,一旦 WebView 被处理并加载了新视图,该新视图最多会在 6 分钟内无响应。
[Preserve (AllMembers=true)]
public class YouTubeViewer : UIWebView
{
public static AppDelegate appDelegate = (AppDelegate) UIApplication.SharedApplication.Delegate;
public YouTubeViewer(string url, RectangleF frame)
{
Log.WriteLog("loading YouTubeView");
appDelegate.firstViewing = true;
this.UserInteractionEnabled = true;
this.BackgroundColor = UIColor.Clear;
this.Frame = frame;
string youTubeVideoHTML = @"<object width=""{1}"" height=""{2}""><param name=""movie""
value=""{0}""></param><embed
src=""{0}"" type=""application/x-shockwave-flash""
width=""{1}"" height=""{2}""</embed></object>";
string html = string.Format(youTubeVideoHTML, url, frame.Size.Width, frame.Size.Height);
this.LoadHtmlString(html, null);
}
}
以下是 WebView 的处理方式:
public void RemoveWebView(UIWebView inView)
{
try
{
Log.WriteLog("RemoveWebView");
NSUrlCache.SharedCache.RemoveAllCachedResponses();
NSUrlCache.SharedCache.DiskCapacity = 0;
NSUrlCache.SharedCache.MemoryCapacity = 0;
inView.LoadHtmlString("",null);
inView.EvaluateJavascript("var body=document.getElementsByTagName('body')[0];body.style.backgroundColor=(body.style.backgroundColor=='')?'white':'';");
inView.EvaluateJavascript("document.open();document.close()");
inView.StopLoading();
inView.Delegate = null;
inView.RemoveFromSuperview();
inView.Dispose();
}
catch(Exception ex)
{
Log.LogError("RemoveWebView",ex);
}
}
谢谢,瑞克