问题
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