【问题标题】:Not able to access html file in xamarin using GetManifestResourceStream无法使用 GetManifestResourceStream 访问 xamarin 中的 html 文件
【发布时间】:2022-01-24 15:51:33
【问题描述】:

我想在 xamarin.forms uwp 项目中使用传单地图套件。为此,我在 pcl 项目中创建了 index.html 文件。但是当我尝试读取该文件时,它总是返回 null。

index.html

<!DOCTYPE html>
<html>
<head>
    <title>LeafletMap</title>

    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />

    <!--leaflet js-->
    <script src="JS/leaflet.js"></script>
    
    <!--leaflet css-->
    <link rel="stylesheet" href="CSS/leaflet.css" />
   
    <style>
        html, body {
            height: 100%;
            margin: 0;
        }

        #map {
            width: 600px;
            height: 400px;
        }
    </style>

    <style>
        body {
            padding: 0;
            margin: 0;
        }

        #map {
            height: 100%;
            width: 100vw;
        }
    </style>

</head>
<body>
    <div id='map'></div>

    <script type="text/javascript">

        var mbAttr = '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
            mbUrl = 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
            mbUrl2 = 'http://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}',
            mbAttr2 = 'Tiles &copy; Esri &mdash; Source: Esri, i-cubed, USDA, USGS, AEX, GeoEye, Getmapping, Aerogrid, IGN, IGP, UPR-EGP, and the GIS User Community';

       var OSM = L.tileLayer(mbUrl, { id: 'OSM', attribution: mbAttr }),
           Sat = L.tileLayer(mbUrl2, { id: 'Sat', attribution: mbAttr2 });

       var baseLayers = {
            "OSM": OSM,
            "Satellite": Sat
       };
        
       var markerGroup = new L.LayerGroup();

        // Map initialization
        var map = L.map('map',{
            center: [-2.1319654, -79.9319396],
            zoom:5,
            layer: [OSM]
        });

        map.addLayer(markerGroup);

        L.control.layers(baseLayers).addTo(map);

       
    </script>
    
</body>
</html>


初始化地图

var source = new HtmlWebViewSource();
source.BaseUrl = DependencyService.Get<IBaseUrl>().Get();
var assembly = typeof(MainPage).GetTypeInfo().Assembly;
var stream = assembly.GetManifestResourceStream("LeafletMap.index.html");

StreamReader reader = null;
if (stream != null) 
{
    try
    {
        reader = new StreamReader(stream);
        source.Html = reader.ReadToEnd();
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine($"Exception \n - {ex.Message}");
    }
    finally 
    {
        if (reader != null) 
        {
            reader.Dispose();
        }
    }
    webView.Source = source;
}

此行始终返回 null。

assembly.GetManifestResourceStream("LeafletMap.index.html"); 

有人知道如何在 xamarin.forms 中使用小册子 mapkit 吗?

我已参考此示例:https://github.com/osekom/LeafletMapXamarinForms。

它运行良好,因为它具有旧版本的 xamarin.forms,但它不适用于最新版本。

【问题讨论】:

  • 文件是嵌入式资源吗?您是否确认您使用的是正确的资源 ID?
  • 是的,我已将操作类型设置为嵌入式资源。正确的资源 id 是什么意思?
  • 资源id是你用来加载资源的字符串。您应该能够查看 VS 中的属性以查看 ID
  • @Jason,不幸的是,对于嵌入式文件,其属性仅列出文件名和路径 - 没有“资源 ID”属性来显示作为资源获取所需的字符串。 (这一定已经改变了;我记得过去在那里编辑过资源 ID。)

标签: xamarin.forms webview leaflet xamarin.uwp


【解决方案1】:

它在原始版本中有效,因为LeafletMap 是程序集名称。据推测,您的项目有不同的名称,例如MyProject.

改变

assembly.GetManifestResourceStream("LeafletMap.index.html");

到

assembly.GetManifestResourceStream("MyProject.index.html"); 

将您的实际项目的程序集名称替换为“MyProject”。默认情况下,这将与项目名称相同。如有疑问,请打开项目的“属性”、“应用程序”选项卡、“程序集名称:”。


如果还是不行,下面是如何查看程序集的所有资源 ID:

var names = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames();

在此之后的一行上放置一个断点。然后在 VS“立即”窗口中,输入“名称”。

【讨论】:

  • 我的帖子中有更新,您可以查看一下,如果您对此有任何想法,请告诉我?
  • 请创建一个新问题,然后在那里提问。
猜你喜欢
  • 1970-01-01
  • 2013-09-28
  • 2021-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-26
  • 1970-01-01
  • 2021-12-15
相关资源
最近更新 更多