我今天来是因为我被困在 android 平台的 GIF 负载上。我知道它有效,因为它适用于UWP和iOS,但是Android不起作用。
我有这个对象:
public class Gif : WebView
{
public string GifSource
{
set
{
var html = new HtmlWebViewSource();
html.Html = String.Format(@"<html><body style='background: #FF0000;'><img src='{0}' style='width:100%;height:100%;'/></body></html>", "https://media.giphy.com/media/UGifMFmx0gERG/giphy.gif");
Debug.WriteLine("Html.Source = '{0}'", html.Html);
this.Margin = -10;
this.Source = html;
}
get { return (string)GetValue(SourceProperty); }
}
}
所以我只是在我的 xaml 方面声明这个Gif
:
<AbsoluteLayout AbsoluteLayout.LayoutBounds="0.5, 0.5, 1, 1" AbsoluteLayout.LayoutFlags="All">
<control:Gif
AbsoluteLayout.LayoutBounds="0.5, 0, 1, 0.9"
AbsoluteLayout.LayoutFlags="All"
BackgroundColor="Red"
GifSource="Gifs/LoginBackground.gif" />
<BoxView
AbsoluteLayout.LayoutBounds="0.5, 0.5, 1, 1"
AbsoluteLayout.LayoutFlags="All"
BackgroundColor="Transparent" />
</AbsoluteLayout>
PS:我声明了一个BoxView
来逃避通过用户手势移动Web视图的可能性。
现在问题是,它完美地适用于UWP,具有以下两个代码:
html.Html = String.Format(@"<html><body style='background: #FF0000;'><img src='{0}' style='width:100%;height:100%;'/></body></html>", "https://media.giphy.com/media/UGifMFmx0gERG/giphy.gif");
this.Source = html;
或
this.Source = "https://media.giphy.com/media/UGifMFmx0gERG/giphy.gif";
在Android上,this.Source = "https://media.giphy.com/media/UGifMFmx0gERG/giphy.gif";
有效,但是第一个解决方案不起作用,这是一个问题。通过创建我自己的 html,它允许我让 gif 填充我的视图,不像链接......在更多点上,如果我删除 html 的style='width:100%;height:100%;'
,它可以工作,但再来一次,不是大小合适。
知道吗?提前感谢!
:)
我找到了一个解决方案,这很奇怪,但我有以下代码:
html.Html = String.Format(@"<html><body style='background: #000000;'><img src='{0}' style='width:{1};height:{2};'/></body></html>",
DependencyService.Get<IBaseUrl>().Get() + value, App.ScreenSize.Width, (App.ScreenSize.Height / 100) * 90);
第一件事很奇怪,100% 的价值有效,但前提是我把它交给WebView
.我的意思是,如果我把 100% 作为 html 文本,它不起作用,不像我给出一个真正的值。也许WebView
无法访问该物业。
现在如何获取屏幕尺寸?
<小时 />人造人:
App.ScreenSize = new Xamarin.Forms.Size((int)(Resources.DisplayMetrics.WidthPixels / Resources.DisplayMetrics.Density), (int)(Resources.DisplayMetrics.HeightPixels / Resources.DisplayMetrics.Density));
将其包含在您的 Android 项目MainActivity.cs
中
UWP:
App.ScreenSize = new Size(Window.Current.Bounds.Width, Window.Current.Bounds.Height);
将其包含在 UWP 项目的MainPage.xaml.cs
中
在 PCL 部件的App.cs
中,声明一个public static Size ScreenSize;
享受!