无法在 Xamarin.Forms(便携式)中播放 GIF 图像 [英] Unable to play GIF image in Xamarin.Forms (Portable)
问题描述
我正在尝试使用 Xamarin.Forms(便携式)项目播放 GIF 图像.我已使用以下代码实现但无法播放 GIF,我没有看到静态图像.
<块引用>这段代码没有错误或崩溃,但我看不到GIF 图片.
代码有问题吗?
界面:
公共接口IGif{字符串获取();}
iOS 实现:
[程序集:依赖(typeof(GifView_iOS))]命名空间 Project.iOS{公共类 GifView_iOS : IGif{公共字符串 Get(){返回 NSBundle.MainBundle.BundlePath;}}}
Android 实现:
[程序集:依赖(typeof(GifView_Droid))]命名空间 Project.Droid{公共类 GifView_Droid : IGif{公共字符串 Get(){返回 "file:///android_asset/";}}}
GIF 图像类:
公共类 Gif:WebView{公共字符串 GifSource{放{字符串 imgSource = DependencyService.Get.Get() + value;var html = new HtmlWebViewSource();html.Html = String.Format(@"<html><body style='background: #000000;'><img src='{0}'/></body></html>",图片来源);SetValue(SourceProperty, html);}}}
最后,我添加到 StackLayout.
Gif gifimage = new Gif();gifimage.GifSource = "loading.gif";StackGif.Children.Add(gifimage);
<块引用>
我没有在 iPhone 上测试过这段代码,也许它可以在 iPhone 上运行.
提前致谢.
这是一个愚蠢的错误,我必须在 web 视图和 GIF 类中给出图像的大小.
这是更新后的代码.
我的 GIF 类:
公共类 Gif:WebView{公共字符串 GifSource{放{string imgSource = DependencyService.Get().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 中的初始化:
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);
<块引用>
注意:我仍在处理背景部分,因为它看起来像附加图片,我想要网络视图中的透明背景.
图片:
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.
There is no error or crash in this code, but I am not able to see the GIF image.
Is there anything wrong in the code?
Interface:
public interface IGif
{
string Get();
}
iOS Implementation:
[assembly: Dependency(typeof(GifView_iOS))]
namespace Project.iOS
{
public class GifView_iOS : IGif
{
public string Get()
{
return NSBundle.MainBundle.BundlePath;
}
}
}
Android Implementation:
[assembly: Dependency(typeof(GifView_Droid))]
namespace Project.Droid
{
public class GifView_Droid : IGif
{
public string Get()
{
return "file:///android_asset/";
}
}
}
GIF Image class:
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);
}
}
}
And Finally, I have added to the StackLayout.
Gif gifimage = new Gif();
gifimage.GifSource = "loading.gif";
StackGif.Children.Add(gifimage);
I have not tested this code in iPhone, maybe it is working in iPhone.
Thank You in advance.
This was the silly mistake, I have to give the size of Image inside web view and to GIF class also.
Here is the updated code.
My GIF Class:
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);
}
}
}
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);
Note: I am still working on the Background Part, as it is looking like attached image, I want the transparent background in web view.
Image:
这篇关于无法在 Xamarin.Forms(便携式)中播放 GIF 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!