【问题标题】:Xamarin Forms WebView Load embedded in application siteXamarin Forms WebView 加载嵌入应用程序站点
【发布时间】:2020-10-01 15:19:19
【问题描述】:

我在我的程序集中嵌入了站点:

    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            WebView.Navigating += WebView_Navigating;
            var resourceName = Program.GetResourceName("index.html");
            WebView.Source = new UrlWebViewSource()
            {
                Url = "file://" + resourceName,
            };
        }

        public void WebView_Navigating(object sender, WebNavigatingEventArgs args)
        {
            if (args.Url.StartsWith("file://"))
            {
                return;
            }

            Device.OpenUri(new Uri(args.Url));

            args.Cancel = true;
        }
    }

我试图弄清楚如何使用WebView 使用Xamarin.Forms 运行它... 我从我的程序集中获得了嵌入式资源,我需要以某种方式将基于文本的 html 加载到 WebView ...

但我不知道该怎么做...... 是否有一些 API 用于将嵌入式资源加载到 WebView 中?

经过一番研究,我找到了一些解决方案:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BlazorXamarinMobile.Web.Client;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace BlazorXamarinMobile
{
    public partial class MainPage : ContentPage
    {
        Dictionary<string, string> _urlToHtml = new Dictionary<string, string>();
        public MainPage()
        {
            InitializeComponent();
            WebView.Navigating += WebView_Navigating;
            (string embeddedResource, string text) = Program.GetResourceName("index.html");
            embeddedResource = embeddedResource.Replace("wwwroot.index.html", "wwwroot/index.html");
            var embeddedResourceUrl = "file://" + embeddedResource;
            _urlToHtml.Add("file://BlazorXamarinMobile.Web.Client.wwwroot/".ToLower(), text);
            _urlToHtml.Add(embeddedResourceUrl.ToLower(), text);
            WebView.Source = new HtmlWebViewSource()
            {
                BaseUrl = "file://BlazorXamarinMobile.Web.Client.wwwroot/".ToLower(),
                Html = text,
            };
        }

        public void WebView_Navigating(object sender, WebNavigatingEventArgs args)
        {
            if (args.Url.StartsWith("file://"))
            {
                if (args.Url.Contains("wwwroot/"))
                {
                    var html = _urlToHtml[args.Url];
                    WebView.Source = new HtmlWebViewSource()
                    {
                        Html = html,
                    };
                }
                return;
            }

            args.Cancel = true;
        }
    }
}

但是WebView显示Url无效!! 但是,如果我直接加载文本,它会显示初始站点和重新加载应用程序的错误......这是因为 WebView 尝试从自定义路径文件加载,但这些文件在 Assembly 内部,应该使用某种钩子手动加载...

可以吗?

【问题讨论】:

    标签: xamarin xamarin.forms webview blazor


    【解决方案1】:

    如果您想使用 webview 加载嵌入式网站,您可以尝试以下步骤。

    Html: index.hyml

    <!DOCTYPE html>
    
    <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta charset="utf-8" />
    <title>Xamarin Forms</title>
    </head>
    <body>
    <h1>Xamarin.Forms</h1>
    <p>This is an Android web page.</p>
    </body>
    </html>
    

    Xaml:

      <ContentPage.Content>             
            <WebView x:Name="Webview_html" />       
    </ContentPage.Content>
    

    代码背后:XamarinDemo 是我的 Xamarin.Forms 项目。我把html嵌入资源放在Form项目下。

    string fileName = "XamarinDemo.index.html";
    
            var assembly = typeof(XamarinDemo.MainPage).Assembly;
    
            Stream stream = assembly.GetManifestResourceStream(fileName);
            if (stream == null)
            {
                throw new InvalidOperationException(
                    String.Format("Cannot create stream from specified URL: {0}", fileName));
            }
    
            StreamReader reader = new StreamReader(stream);
            string htmlString = reader.ReadToEnd();
    
            HtmlWebViewSource html = new HtmlWebViewSource();
            html.Html = htmlString;
    
            Webview_html.Source = html;       
    

    截图:

    【讨论】:

    • Zand 感谢您的支持,但我也做了同样的事情,并且它适用于没有指向我的程序集中其他资源的链接的页面......我想要以某种方式添加 Url 挂钩WebView 加载资源,如果此 Url 是从我的程序集中加载它的文件
    • Url 挂钩是什么意思?我对此感到困惑,需要更多信息。
    • 当我像你一样加载 html 时,WebView 将尝试加载所有资源:css、javascript、*.dll(用于 Blazor)等等,但所有这些数据都位于 C# 程序集中并且不能轻易加载被加载到 WebView 并且好的事情是当 WebView 将尝试加载此文件以从 Assembly 手动加载它时捕获
    • 是的,Xamarin.forms 中的嵌入资源将像这样加载。我看到另一个评论,你把嵌入的资源放在 dll 文件中。什么是.dll文件?这个dll文件和Xamarin.forms项目有什么关系?为什么要把html放到这个dll文件里?
    • 我有嵌入资源的项目,该项目不是 Android 或 iOS,资源位于 dll 文件内,我需要以某种方式使用 WebView 加载此资源
    【解决方案2】:

    您似乎没有正确引用该位置,甚至可能将您的 html 文件放置在不正确的位置。你在看什么教程,你有没有关注Xamarin Forms Web Views的官方文档?

    • 您必须将文件放置在正确的位置 - Android 中的资产和 iOS 中的资源。
    • 然后您必须使用 DependencyService 以正确的方式为每个平台获取 BaseURL
    • 然后,在 Android 上,您必须使用流阅读器来读取文件

    【讨论】:

    • 是的,你是对的,我的程序集内有我的网站作为嵌入式资源,但是 WebView 尝试打开位于我的程序集内的文件的 Url,我需要一些 Url 挂钩来加载它文件不是来自 Url,而是来自我的程序集
    • 你说的汇编是什么意思?你能分享一张它的位置的照片吗?有什么原因不能像这里显示的那样放置它吗?
    • 我的意思是我的 C# 程序集(dll 文件)中嵌入了资源
    • 是的,您将它存储在哪个文件夹中?
    • 在 wwwroot 和 Blazor WebAssembly 应用程序中一样,并且此文件夹位于 dll 内,但 Android 上的 WebView 无法直接从 C# dll 读取,它只能从 .aar 文件和此文件内的文件夹读取,但我没有这样的文件夹,但我的 dll 中有这个文件夹
    猜你喜欢
    • 2018-03-20
    • 2020-04-26
    • 2021-09-29
    • 2021-11-17
    • 1970-01-01
    • 1970-01-01
    • 2020-10-11
    • 2016-06-25
    • 1970-01-01
    相关资源
    最近更新 更多