下面的代码用于在视图上放置一个小的 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);
}
}
谢谢,
瑞克
我与 Xamarin 交谈,他们建议删除 AppDelegate 中方向管理的覆盖。
public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIApplication application, UIWindow forWindow)
{ /*... code ...*/ }
删除此覆盖后,我的应用程序在加载 YouTube 视频时按预期工作。
这为我解决了这个问题。您仍然可以通过单独的 ViewController 覆盖以及通过 Info.plist 文件全局控制支持的方向。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)