【问题标题】:Xamarin Forms - WebView Html Source issueXamarin 表单 - WebView Html 源问题
【发布时间】:2017-06-15 03:16:31
【问题描述】:

我今天来是因为我被 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%;',它可以工作,但是再一次,不是很好的大小。 .

有什么想法吗?提前致谢!

【问题讨论】:

    标签: android webview xamarin.forms gif


    【解决方案1】:

    :)

    我找到了一个解决方案,这很奇怪,但我有这个代码:

    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;

    享受吧!

    【讨论】:

      猜你喜欢
      • 2017-12-19
      • 1970-01-01
      • 1970-01-01
      • 2016-11-26
      • 2017-10-16
      • 1970-01-01
      • 1970-01-01
      • 2016-12-24
      • 1970-01-01
      相关资源
      最近更新 更多