Xamarin Forms - GIF存储到WebView中

时间:2016-07-05 00:03:59

标签: xamarin.forms gif

我在问自己,我在网上找不到有趣的东西。我从未与WebView合作,但我发现了一些关于它的事情,并且似乎可以创建自己的WebPage

那么是否可以创建一个WebPage(在WebView中)和在这个WebView(或WebPage)中添加一个GIF?

我也理解,我认为我可以将WebView链接到网络上的 GIF链接,但是为了进行数据交换,我想将其存储在手机而不是每次都加载它。

那么,有可能吗?

提前感谢!

1 个答案:

答案 0 :(得分:1)

来自Xamarins docs

// define a interface
public interface IBaseUrl { string Get(); }

// load html
var html = new HtmlWebViewSource ();
html.BaseUrl = DependencyService.Get<IBaseUrl> ().Get ();

htmlSource.Html = @"<html>
    <head>
    <link rel=""stylesheet"" href=""default.css"">
    </head>
    <body>
    <h1>Xamarin.Forms</h1>
    <p>The CSS and image are loaded from local files!</p>
    <img src='Images/XamarinLogo.png'/>
    </body>
    </html>";

webView.Source = html;

然后,您需要为您希望支持的每个平台实施IBaseUrl。对于iOS,它看起来像这样:

[assembly: ExportRenderer (typeof (BaseUrlWebView), typeof (BaseUrlWebViewRenderer))]
namespace WorkingWithWebview.iOS {
    public class BaseUrlWebViewRenderer : WebViewRenderer  {
        public override void LoadHtmlString (string s, NSUrl baseUrl)  {
            baseUrl = new NSUrl (NSBundle.MainBundle.BundlePath, true);
            base.LoadHtmlString (s, baseUrl);
        }
    }
}