Why is NSBundle.MainBundle.GetUrlForResource returning null

浪子不回头ぞ 提交于 2019-12-25 01:38:03

问题


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));

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?


回答1:


Because you load local html file .So you should use

webView.LoadRequest(new NSUrlRequest(url),false);

isDir:A Boolean value that specifies whether path is treated as a directory path when resolving against relative path components. Pass True if the path indicates a directory, False otherwise.




回答2:


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));

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



来源:https://stackoverflow.com/questions/57093070/why-is-nsbundle-mainbundle-geturlforresource-returning-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!