无法在 Xamarin.Forms 中播放 GIF 图像(便携式)
Unable to play GIF image in Xamarin.Forms (Portable)
我正在尝试使用 Xamarin.Forms (Portable) 项目播放 GIF 图像。我已经使用以下代码实现但无法播放 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 实施:
[assembly: Dependency(typeof(GifView_Droid))]
namespace Project.Droid
{
public class GifView_Droid : IGif
{
public string Get()
{
return "file:///android_asset/";
}
}
}
GIF 图片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);
}
}
}
最后,我添加到 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.
提前谢谢你。
默认情况下,StackLayout 会占用整个可用空间 space,但 WebView 不会。
而只是
Gif gifimage = new Gif();
使用
public WebViewProgrammaticallyPage()
{
var StackGif = new StackLayout()
{
BackgroundColor=Color.Red,
};
Gif gifimage = new Gif()
{
WidthRequest = 120,
HorizontalOptions = LayoutOptions.CenterAndExpand,
HeightRequest = 80,
VerticalOptions = LayoutOptions.CenterAndExpand,
};
gifimage.GifSource = "icon1.png";
StackGif.Children.Add(gifimage);
Content = StackGif;
}
WebView 的透明度是不同的问题。您应该使用自定义渲染器
https://forums.xamarin.com/discussion/34957/creating-a-webview-with-a-transparent-background
这是个愚蠢的错误,我还必须在网络视图和 GIF 中给出图像的大小 class。
这是更新后的代码。
我的 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);
}
}
}
和 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.
图片:
我正在尝试使用 Xamarin.Forms (Portable) 项目播放 GIF 图像。我已经使用以下代码实现但无法播放 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 实施:
[assembly: Dependency(typeof(GifView_Droid))]
namespace Project.Droid
{
public class GifView_Droid : IGif
{
public string Get()
{
return "file:///android_asset/";
}
}
}
GIF 图片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);
}
}
}
最后,我添加到 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.
提前谢谢你。
默认情况下,StackLayout 会占用整个可用空间 space,但 WebView 不会。 而只是
Gif gifimage = new Gif();
使用
public WebViewProgrammaticallyPage()
{
var StackGif = new StackLayout()
{
BackgroundColor=Color.Red,
};
Gif gifimage = new Gif()
{
WidthRequest = 120,
HorizontalOptions = LayoutOptions.CenterAndExpand,
HeightRequest = 80,
VerticalOptions = LayoutOptions.CenterAndExpand,
};
gifimage.GifSource = "icon1.png";
StackGif.Children.Add(gifimage);
Content = StackGif;
}
WebView 的透明度是不同的问题。您应该使用自定义渲染器 https://forums.xamarin.com/discussion/34957/creating-a-webview-with-a-transparent-background
这是个愚蠢的错误,我还必须在网络视图和 GIF 中给出图像的大小 class。
这是更新后的代码。
我的 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);
}
}
}
和 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.
图片: