无法在Xamarin.Forms中播放GIF图像(便携式)
我正在尝试使用Xamarin.Forms(便携式)项目播放GIF图像.我已经用以下代码实现了,但是无法播放GIF,因此我看不到静态图片.
I am trying to play the GIF images with the Xamarin.Forms (Portable) project. I have implemented with the following code but unable to Play GIF, I don't event see the static image.
此代码没有错误或崩溃,但是我看不到 GIF图片.
There is no error or crash in this code, but I am not able to see the GIF image.
代码中有什么问题吗?
接口:
public interface IGif
{
string Get();
}
iOS实现:
[assembly: Dependency(typeof(GifView_iOS))]
namespace Project.iOS
{
public class GifView_iOS : IGif
{
public string Get()
{
return NSBundle.MainBundle.BundlePath;
}
}
}
Android实现:
Android Implementation:
[assembly: Dependency(typeof(GifView_Droid))]
namespace Project.Droid
{
public class GifView_Droid : IGif
{
public string Get()
{
return "file:///android_asset/";
}
}
}
GIF图片类:
public class Gif: WebView
{
public string GifSource
{
set
{
string imgSource = DependencyService.Get<IGif>.Get() + value;
var html = new HtmlWebViewSource();
html.Html = String.Format
(@"<html><body style='background: #000000;'><img src='{0}' /></body></html>",
imgSource);
SetValue(SourceProperty, html);
}
}
}
最后,我已添加到StackLayout.
And Finally, I have added to the StackLayout.
Gif gifimage = new Gif();
gifimage.GifSource = "loading.gif";
StackGif.Children.Add(gifimage);
我尚未在iPhone中测试过此代码,也许它在iPhone中可以正常工作.
I have not tested this code in iPhone, maybe it is working in iPhone.
先谢谢您.
这是一个愚蠢的错误,我必须在Web视图中同时将Image的大小以及GIF类也设置为
This was the silly mistake, I have to give the size of Image inside web view and to GIF class also.
这是更新的代码.
我的GIF课:
public class Gif: WebView
{
public string GifSource
{
set
{
string imgSource = DependencyService.Get<Dependency.IGif>().Get() + value;
var html = new HtmlWebViewSource();
html.Html = String.Format(@"<html><body><img src='{0}' style='width: 100px; height: 100px;' /></body></html>", imgSource);
SetValue(SourceProperty, html);
}
}
}
和ContentPage中的初始化:
and Initialization in ContentPage:
Helpers.Controls.Gif gifImage = new Helpers.Controls.Gif();
gifImage.GifSource = "loading.gif";
gifImage.HorizontalOptions = LayoutOptions.Center;
gifImage.VerticalOptions = LayoutOptions.FillAndExpand;
gifImage.HeightRequest = 120;
gifImage.BackgroundColor = Color.Transparent;
GridLoad.Children.Add(gifImage);
注意:我仍然在研究背景部分,因为它看起来像 附加的图片,我想要Web视图中的透明背景.
Note: I am still working on the Background Part, as it is looking like attached image, I want the transparent background in web view.
图片: