菜鸟教程小白 发表于 2022-12-12 12:54:47

c# - 将本地 CSS 文件加载到 Xamarin IOS WebView 中?


                                            <p><p>我可以像这样将本地 HTML 文件加载到我的 <code>WebView</code> 中(这很好用):</p>

<pre><code>var fileName = &#34;Views/Default.html&#34;;
var localHtmlUrl = Path.Combine(NSBundle.MainBundle.BundlePath, fileName);
var url = new NSUrl(localHtmlUrl, false);
var request = new NSUrlRequest(url);
WebView.LoadRequest(request);
</code></pre>

<p>我想在我的 HTML 文件中引用一个 CSS 文件(也是本地的):</p>

<pre><code>&lt;link href=&#34;Content/style.css&#34; rel=&#34;stylesheet&#34;&gt;
</code></pre>

<p>CSS 文件确实存在于 <code>Content</code> 文件夹中,构建操作设置为该文件的内容。</p>

<p>如何通过我的 html 文件引用/加载有问题的 CSS 文件?可能吗?</p>

<p><strong>更新</strong>:将 css 和 html 放在不同的文件夹中。将它们都放在 <code>Content</code> 文件夹中,然后更新使用 <code>LoadRequest</code> 时解决问题的 hrefs。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>检查此链接
<a href="https://developer.xamarin.com/recipes/ios/content_controls/web_view/load_local_content/" rel="noreferrer noopener nofollow">https://developer.xamarin.com/recipes/ios/content_controls/web_view/load_local_content/</a> </p>

<p>部分附加信息</p>

<p>代码中生成的Html也可以显示出来,这对于自定义内容很有用。要显示 Html 字符串,请使用 LoadHtmlString 方法而不是 LoadRequest。将路径传递给 Content 目录有助于 WebView 解析 Html 中的相关 Url,例如链接、图像、CSS 等。</p>

<pre><code>// assumes you&#39;ve placed all your files in a Content folder inside your app
string contentDirectoryPath = Path.Combine (NSBundle.MainBundle.BundlePath, &#34;Content/&#34;);
string html = &#34;&lt;html&gt;&lt;a href=&#39;Home.html&#39;&gt;Click me&lt;/a&gt;&lt;/html&gt;&#34;;
webView.LoadHtmlString(html, new NSUrl(contentDirectoryPath, true));
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于c# - 将本地 CSS 文件加载到 Xamarin IOS WebView 中?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/42078911/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/42078911/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: c# - 将本地 CSS 文件加载到 Xamarin IOS WebView 中?