为什么 NSBundle.MainBundle.GetUrlForResource 返回 null [英] Why is NSBundle.MainBundle.GetUrlForResource returning null
问题描述
我正在使用 UIWebView 查看本地 HTML 文件.我的文件名为 NoWiFi.html 并具有以下路径 Resources/NoWiFi.html.我还将构建操作设置为 BundleResource,并尝试:
I am using UIWebView to view a local HTML file. My file is named NoWiFi.html and has the following path Resources/NoWiFi.html. I have also set the Build Action to BundleResource, and tried:
string name = "Resources/NoWiFI.html"
NSURl url;
url = new NSUrl(Path.Combine(NSBundle.MainBundle.BundlePath, name), false);
// and
url = NSBundle.MainBundle.GetUrlForResource("NoWiFi", "html"); // returns null
webView.LoadRequest(new NSUrlRequest(url));
如 xamarins 文档中所述一>但后来我收到一个错误:在此服务器上找不到请求的 URL
as in the xamarins documentation but then i get an error: The requested URL was not found on this server
为什么我的代码找不到文件?
Why does not my code find the file?
推荐答案
由于某些有线原因,一旦我重命名文件,我的问题就自行解决了,如果其他人试图加载本地 HTML 文件,我的代码如下所示:
For some wired reason my problem solved it self once i renamed the file, if anyone else is tying to load a local HTML file my code looks like this:
NSUrl url = NSBundle.MainBundle.GetUrlForResource("NoWiFi", "html");
webView.LoadRequest(new NSUrlRequest(url));
如果您在使用调试器检查 url 对象时为空,请确保本地文件(在我的情况下为 NoWiFi.html)将构建操作设置为:BundleResource,如果 NSUrl 对象仍然为空,请尝试重命名文件
And if your url object is null when you are inspecting it with the debugger make sure the local file (in my case NoWiFi.html) has Build Action set to: BundleResource and if the NSUrl object is still null, try renaming the file
这篇关于为什么 NSBundle.MainBundle.GetUrlForResource 返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!