我想使用ListView和WebView显示Youtube Video的列表。但是未加载视频。
当前,我不知道如何将URL直接绑定到AXML文件中的 WebView ,为此,我改为自定义了 BindableWebView :
public class BindableWebView : WebView
{
public BindableWebView(Context context, IAttributeSet attrs)
: base(context, attrs)
{
}
private string _webViewContent;
public string WebViewContent
{
get { return _webViewContent; }
set
{
_webViewContent = value;
LoadHtmlString();
}
}
private void LoadHtmlString()
{
LoadData(WebViewContent, "text/html", "utf-8");
}
}
这是Layout AXML文件:
<dc.AlphaLinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
local:MvxBind="Click ItemClickCommand">
<!--Video Thumbnail-->
<FrameLayout
android:layout_width="@dimen/ProductImageWidth"
android:layout_height="@dimen/ProductImageHeight"
android:layout_margin="@dimen/ExtraNewsImagePadding">
<BindableWebView
android:id="@+id/playerWebView"
android:layout_width="match_parent"
android:layout_height="match_parent"
local:MvxBind="WebViewContent Video.YoutubeUrl" />
</FrameLayout>
<LinearLayout
.....
</LinearLayout>
</dc.AlphaLinearLayout>
这是视频模型:
namespace ....Shared.Models
{
public class Video
{
public string YoutubeId { get; set; }
public string YoutubeImageUrl => $"https://img.youtube.com/vi/{YoutubeId}/0.jpg";
public string YoutubeUrl => $"<iframe width=\"100%\" height=\"100%\" src=\"https://www.youtube.com/embed/{YoutubeId}\" frameborder=\"0\" allowfullscreen></iframe>";
public Guid NewsId { get; set; }
}
}
在C#中和 VideoListViewm :
namespace DeHeus.Droid.Views
{
public class VideoListView : DetailViewWithShare
{
protected override int LayoutId => Resource.Layout.VideoListView;
private CustomMvxListView _videoListview;
protected override void InitView(View view)
{
_videoListview = view.FindViewById<CustomMvxListView>(Resource.Id.videoList);
_videoListview.ItemTemplateId = Resource.Layout.VideoListItemView;
}
protected override void CreateBinding()
{
var bindingSet = this.CreateBindingSet<VideoListView, VideoListViewModel>();
bindingSet.Bind(_videoListview.Adapter)
.For(v => v.ItemsSource)
.To(vm => vm.VideoItemViewModels);
bindingSet.Bind(_videoListview)
.For(v => v.ScrollToBottom)
.To(vm => vm.ScrollToBottomCommand);
bindingSet.Apply();
}
}
}
运行调试,WebViewContent会获得确切的网址,但我不知道为什么它不起作用,如我所想:"<iframe width=\"100%\" height=\"100%\" src=\"https://www.youtube.com/embed/usoFYAqOMyA\" frameborder=\"0\" allowfullscreen></iframe>"
有人知道吗?
谢谢
答案 0 :(得分:0)
做了很多研究,我终于解决了这个问题。
您需要做的就是将SetWebChromeClient(new WebChromeClient());
放在LoadData之前
public BindableWebView(Context context, IAttributeSet attrs)
: base(context, attrs)
{
WebSettings settings = this.Settings;
settings.JavaScriptEnabled = true;
SetWebChromeClient(new WebChromeClient());
}
private string _webViewContent;
public string WebViewContent
{
get { return _webViewContent; }
set
{
_webViewContent = value;
LoadHtmlString();
}
}
private void LoadHtmlString()
{
LoadData(WebViewContent, "text/html", "utf-8");
}