Hello,
I need to embed a YouTube video in my Xamarin.Forms app. I have tried various methods with the WebView control, including passing the embed URL and trying to port this code https://gist.github.com/rais38/4683817 etc. However, the WebView doesn't show the webpage. Is there a good way to get a YouTube video in a Xamarin.Forms app?
EDIT: Just to clarify the situation a little bit further. I am trying to add a WebView to a StackLayout under existing content. I have also tried just using <h1>Test</h1>
, and this still doesn't show the content, so it might be that I am using the WebView incorrectly.
Here is my code:
public class XamarinVideoElementViewGenerator : IPageElementViewGenerator
{
public global::Xamarin.Forms.View GenerateElementView(PageElement pageElement)
{
// Check if the page element is null.
if (pageElement == null)
{
return new Label();
}
// Create the video via the page element details.
var element = (VideoPageElement) pageElement;
if (!string.IsNullOrEmpty(element.VideoSource))
{
WebView webView = new WebView();
HtmlWebViewSource htmlWebViewSource = new HtmlWebViewSource();
htmlWebViewSource.Html = BuildFinalHtml("<h1>Hello</h1>");
webView.Source = htmlWebViewSource;
return webView;
}
// If all fails, return a blank label.
return new Label();
}
private string BuildEmbedUrl(string videoSource)
{
var iframeURL = string.Format("<iframe width=\"320\" height=\"600\" src=\"{0}\" frameborder=\"0\" allowfullscreen></iframe>", videoSource);
return iframeURL;
}
private string BuildFinalHtml(string embedUrl)
{
string finalUrl = string.Format("<html><body>{0}</body></html>", embedUrl);
return finalUrl;
}
}
Note: I am writing a library that generates Xamarin pages dynamically from either JSON or HTML page sources (for an educational app that requires easy updates of content/page layout).
Thanks,
Richard Bosworth